@etsoo/appscript
Version:
Applications shared TypeScript framework
94 lines (93 loc) • 2.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlutterHost = void 0;
const shared_1 = require("@etsoo/shared");
const IBridgeHost_1 = require("./IBridgeHost");
/**
* Flutter JavaScript Host
* https://inappwebview.dev/docs/javascript/communication/
*/
class FlutterHost {
/**
* Constructor
* @param callHandler Call handler
*/
constructor(host) {
this.host = host;
/**
* Name
*/
this.name = IBridgeHost_1.BridgeHostName.Flutter;
/**
* Cached commands
*/
this.cachedCommands = {};
window.addEventListener("flutterInAppWebViewPlatformReady", (_event) => {
if (this.host.callHandler == null)
return;
for (const key in this.cachedCommands) {
// Args
const args = this.cachedCommands[key];
// Execute
this.host.callHandler(key, ...args);
// Remove the key
delete this.cachedCommands[key];
}
});
}
cacheCommand(name, ...args) {
this.cachedCommands[name] = args;
}
changeCulture(locale) {
if (this.host.callHandler)
this.host.callHandler("changeCulture", locale);
else
this.cacheCommand("changeCulture", locale);
}
closable() {
return false;
}
exit() {
if (this.host.callHandler)
this.host.callHandler("exit");
else
this.cacheCommand("exit");
}
async getLabels(...keys) {
// Try 500 miliseconds
let count = 5;
while (this.host.callHandler == null) {
count--;
await shared_1.ExtendUtils.sleep(100);
if (count === 0)
break;
}
const init = {};
if (this.host.callHandler == null)
return init;
const result = (await this.host.callHandler("getLabels")) ?? {};
return keys.reduce((a, v) => ({
...a,
[v]: result[v] ?? ""
}), init);
}
getStartUrl() {
return this.startUrl;
}
loadApp(name, startUrl) {
this.startUrl = startUrl;
if (this.host.callHandler)
this.host.callHandler("loadApp", name, startUrl);
else
this.cacheCommand("loadApp", name, startUrl);
}
userAuthorization(authorized) {
if (this.host.callHandler)
this.host.callHandler("userAuthorization", authorized);
else
this.cacheCommand("userAuthorization", authorized);
}
onUpdate(func) { }
setTitle(title) { }
}
exports.FlutterHost = FlutterHost;