@btfuse/core
Version:
A native-first framework for building hybdrid web-native applications
60 lines (57 loc) • 2.14 kB
JavaScript
;
/*
Copyright 2023 Breautek
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FuseCallbackManager = void 0;
const tslib_1 = require("tslib");
const UUID = tslib_1.__importStar(require("uuid"));
window.__btfuse_callbacks = new Map();
window.__btfuse_doCallback = function (callbackID, data) {
if (callbackID && window.__btfuse_callbacks.has(callbackID)) {
window.__btfuse_callbacks.get(callbackID)(data);
}
};
/**
* A singleton manager to manage native callbacks.
*
* Create a callback context and pass the return context id to native clients,
* in which they can use to respond back.
*
* Note that plugin APIs are far more efficient and can handle a diverse set of data,
* including large payloads, so when possible it's best to use a plugin API instead of a
* callback API.
*
* This callback API is however, useful for building listener kind of services where the native
* needs to continously callback to the webview with small data packets.
*/
class FuseCallbackManager {
constructor() { }
static getInstance() {
if (!FuseCallbackManager.$instance) {
FuseCallbackManager.$instance = new FuseCallbackManager();
}
return FuseCallbackManager.$instance;
}
createCallback(cb) {
const id = UUID.v4();
window.__btfuse_callbacks.set(id, (data) => {
cb(data);
});
return id;
}
releaseCallback(id) {
window.__btfuse_callbacks.delete(id);
}
}
exports.FuseCallbackManager = FuseCallbackManager;
//# sourceMappingURL=FuseCallbackManager.js.map