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