1e484b35bSopenharmony_ci/*
2e484b35bSopenharmony_ci * Licensed to the Apache Software Foundation (ASF) under one
3e484b35bSopenharmony_ci * or more contributor license agreements.  See the NOTICE file
4e484b35bSopenharmony_ci * distributed with this work for additional information
5e484b35bSopenharmony_ci * regarding copyright ownership.  The ASF licenses this file
6e484b35bSopenharmony_ci * to you under the Apache License, Version 2.0 (the
7e484b35bSopenharmony_ci * "License"); you may not use this file except in compliance
8e484b35bSopenharmony_ci * with the License.  You may obtain a copy of the License at
9e484b35bSopenharmony_ci *
10e484b35bSopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
11e484b35bSopenharmony_ci *
12e484b35bSopenharmony_ci * Unless required by applicable law or agreed to in writing,
13e484b35bSopenharmony_ci * software distributed under the License is distributed on an
14e484b35bSopenharmony_ci * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15e484b35bSopenharmony_ci * KIND, either express or implied.  See the License for the
16e484b35bSopenharmony_ci * specific language governing permissions and limitations
17e484b35bSopenharmony_ci * under the License.
18e484b35bSopenharmony_ci */
19e484b35bSopenharmony_ci
20e484b35bSopenharmony_ciexport interface callbackObjInterface {
21e484b35bSopenharmony_ci  [key: string]: Function;
22e484b35bSopenharmony_ci}
23e484b35bSopenharmony_ci
24e484b35bSopenharmony_ci/**
25e484b35bSopenharmony_ci * <p>Callback management of a certain page.</p>
26e484b35bSopenharmony_ci * <p>We can get the real callback that called from native by callback id which is unique for a callback.</p>
27e484b35bSopenharmony_ci */
28e484b35bSopenharmony_ciexport default class CallbackManager {
29e484b35bSopenharmony_ci  public callbackMap: callbackObjInterface;
30e484b35bSopenharmony_ci  public instanceId: string;
31e484b35bSopenharmony_ci  public currCallbackId: number;
32e484b35bSopenharmony_ci
33e484b35bSopenharmony_ci  constructor(instanceId: string) {
34e484b35bSopenharmony_ci    this.instanceId = String(instanceId);
35e484b35bSopenharmony_ci    this.currCallbackId = 0;
36e484b35bSopenharmony_ci    this.callbackMap = {};
37e484b35bSopenharmony_ci  }
38e484b35bSopenharmony_ci
39e484b35bSopenharmony_ci  /**
40e484b35bSopenharmony_ci   * Add a callback to callbacks object.
41e484b35bSopenharmony_ci   * @param {*} callback - The callback from native.
42e484b35bSopenharmony_ci   * @return {number} Last cllback id in object.
43e484b35bSopenharmony_ci   */
44e484b35bSopenharmony_ci  public add(callback: Function): number {
45e484b35bSopenharmony_ci    this.currCallbackId++;
46e484b35bSopenharmony_ci    this.callbackMap[this.currCallbackId] = callback;
47e484b35bSopenharmony_ci    return this.currCallbackId;
48e484b35bSopenharmony_ci  }
49e484b35bSopenharmony_ci
50e484b35bSopenharmony_ci  /**
51e484b35bSopenharmony_ci   * Remove a callback by callback id.
52e484b35bSopenharmony_ci   * @param {number} callbackId - Callback id.
53e484b35bSopenharmony_ci   * @return {Function} Callback that removed.
54e484b35bSopenharmony_ci   */
55e484b35bSopenharmony_ci  public remove(callbackId: number): Function {
56e484b35bSopenharmony_ci    const callback: Function = this.callbackMap[callbackId];
57e484b35bSopenharmony_ci    delete this.callbackMap[callbackId];
58e484b35bSopenharmony_ci    return callback;
59e484b35bSopenharmony_ci  }
60e484b35bSopenharmony_ci
61e484b35bSopenharmony_ci  /**
62e484b35bSopenharmony_ci   * Consume a callback by callback id.
63e484b35bSopenharmony_ci   * @param {number} callbackId - Callback id.
64e484b35bSopenharmony_ci   * @param {Object} data - Data that needed.
65e484b35bSopenharmony_ci   * @param {boolean} ifKeepAlive - If keepAlive is false, delete this callback.
66e484b35bSopenharmony_ci   * @return {*}
67e484b35bSopenharmony_ci   */
68e484b35bSopenharmony_ci  public consume(callbackId: number, data: object, ifKeepAlive: boolean): any | Error {
69e484b35bSopenharmony_ci    const callback: Function = this.callbackMap[callbackId];
70e484b35bSopenharmony_ci    if (typeof ifKeepAlive === 'undefined' || ifKeepAlive === false) {
71e484b35bSopenharmony_ci      delete this.callbackMap[callbackId];
72e484b35bSopenharmony_ci    }
73e484b35bSopenharmony_ci    if (typeof callback === 'function') {
74e484b35bSopenharmony_ci      return callback.call(null, data);
75e484b35bSopenharmony_ci    }
76e484b35bSopenharmony_ci    return new Error(`Invalid callback id '${callbackId}'.`);
77e484b35bSopenharmony_ci  }
78e484b35bSopenharmony_ci
79e484b35bSopenharmony_ci  /**
80e484b35bSopenharmony_ci   * Clean all callbacks in callbackMap.
81e484b35bSopenharmony_ci   */
82e484b35bSopenharmony_ci  public destroy(): void {
83e484b35bSopenharmony_ci    this.callbackMap = {};
84e484b35bSopenharmony_ci  }
85e484b35bSopenharmony_ci
86e484b35bSopenharmony_ci  /**
87e484b35bSopenharmony_ci   * Check whether the callbacks object is empty.
88e484b35bSopenharmony_ci   * @return {boolean} If callbacks object is empty, return true. Otherwise return false.
89e484b35bSopenharmony_ci   */
90e484b35bSopenharmony_ci  public isEmpty(): boolean {
91e484b35bSopenharmony_ci    if (Object.keys(this.callbackMap).length === 0) {
92e484b35bSopenharmony_ci      return true;
93e484b35bSopenharmony_ci    }
94e484b35bSopenharmony_ci    return false;
95e484b35bSopenharmony_ci  }
96e484b35bSopenharmony_ci}
97