UNPKG

@btfuse/core

Version:

A native-first framework for building hybdrid web-native applications

153 lines (150 loc) 5.07 kB
"use strict"; /* 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.FusePlugin = void 0; const FuseSerializer_1 = require("./FuseSerializer"); /** * Base class for Fuse Plugins */ class FusePlugin { constructor(context) { this.$context = context; this.$apiFactory = this._createAPIFactory() || context.getDefaultAPIFactory(); } /** * Creates the API bridge * @param platform - The runtime platform * @returns */ _createAPI(platform) { return this._getAPIFactory().create(platform); } /** * @virtual * * @remarks * * Create a concrete {@link FuseAPI} factory capable of creating FuseAPI * instance for the current runtime. * * @returns A concrete {@link FuseAPI} Factory */ _createAPIFactory() { return null; } /** * * @returns The concrete API factory */ _getAPIFactory() { return this.$apiFactory; } /** * TAPIOpts is a plugin generic type declaring options. * User may use this to declare a path on how to get a particular FuseAPI. * * This API may be overridden by subclasses to utilise the given options. * The default implementation is to simply return a standard FuseAPI. * * @param opts - API options * @returns */ _getAPI(opts) { return this.$getAPI(); } /** * Returns a standard FuseAPI * @returns */ $getAPI() { return this._getAPIFactory().create(this.getContext().getPlatform()); } /** * Creates a callback context that can be passed to native * The native code can use the callbackID to callback to the JS code. * * The callback can be used several times. * * Release the callback using _releaseCallback with the given callbackID. * These API usages should be part of your plugin API. When releasing a callback, * a standard API call should be made to your plugin to tell the native side that * the callback is no longer usable, and it should clean up the native resources surrounding * the callback context. * * Note that callback data payloads only supports strings. * * @param cb - The callback function * @returns String - callbackID */ _createCallback(cb, apiOpts) { return this._getAPI(apiOpts).createCallbackContext(cb); } /** * Releases a created callback. * * @param id - callbackID */ _releaseCallback(id, apiOpts) { this._getAPI(apiOpts).releaseCallback(id); } /** * Returns the FuseContext * * @returns The current context */ getContext() { return this.$context; } /** * Returns the plugin ID */ getID() { return this._getID(); } /** * The execution API. Concrete classes can call this to perform calls to the native side. * * The concrete class should expose public methods with type information exposed. * * @param method - The method link, this should match the endpoint defined in the native API. * @param contentType - the MIME type of the data you are passing in. * @param data - The data to pass to the native environment * @returns The response body from native. FuseResponseReader has some utility methods to read the data in common formats (e.g. text or JSON) */ async _exec(method, contentType, data, apiOpts) { return await this._getAPI(apiOpts).execute(this.getID(), method, contentType, data); } /** * @remarks * This is useful when you want to use an API as a callback, without exposing * the plugin implementation. The returned function is a bounded function. * When invoked, it will call on the API endpoint and returns a {@link FuseAPIResponse} * asynchronously. * * @sealed * @param route - The API end point * @param serializer - The serializer to use. Defaults to {@link FuseSerializer} which is a sensible serializer. * @returns A context-binding function that can be given to another object. */ _createAPIBridge(route, serializer) { if (!serializer) { serializer = new FuseSerializer_1.FuseSerializer(); } return async (type, data) => { return await this._exec(route, type, serializer.serialize(data)); }; } } exports.FusePlugin = FusePlugin; //# sourceMappingURL=FusePlugin.js.map