@web-atoms/core
Version:
251 lines (250 loc) • 8.54 kB
JavaScript
System.register(["tslib", "./core/AtomBinder", "./core/AtomDispatcher", "./core/types", "./di/RegisterSingleton", "./di/ServiceProvider", "./services/BusyIndicatorService", "./core/AtomList", "@web-atoms/date-time/dist/DateTime", "@web-atoms/date-time/dist/TimeSpan"], function (_export, _context) {
"use strict";
var __decorate, __metadata, AtomBinder, AtomDispatcher, CancelToken, RegisterSingleton, ServiceProvider, BusyIndicatorService, AtomHandler, AtomMessageAction, App_1, App;
_export("AtomMessageAction", void 0);
return {
setters: [function (_tslib) {
__decorate = _tslib.__decorate;
__metadata = _tslib.__metadata;
}, function (_coreAtomBinder) {
AtomBinder = _coreAtomBinder.AtomBinder;
}, function (_coreAtomDispatcher) {
AtomDispatcher = _coreAtomDispatcher.AtomDispatcher;
}, function (_coreTypes) {
CancelToken = _coreTypes.CancelToken;
}, function (_diRegisterSingleton) {
RegisterSingleton = _diRegisterSingleton.RegisterSingleton;
}, function (_diServiceProvider) {
ServiceProvider = _diServiceProvider.ServiceProvider;
}, function (_servicesBusyIndicatorService) {
BusyIndicatorService = _servicesBusyIndicatorService.BusyIndicatorService;
}, function (_coreAtomList) {}, function (_webAtomsDateTimeDistDateTime) {}, function (_webAtomsDateTimeDistTimeSpan) {}],
execute: function () {
AtomHandler = class AtomHandler {
constructor(message) {
this.message = message;
this.list = new Array();
}
};
_export("AtomMessageAction", AtomMessageAction = class AtomMessageAction {
constructor(msg, a) {
this.message = msg;
this.action = a;
}
});
_export("App", App = App_1 = class App extends ServiceProvider {
static installStyleSheet(ssConfig) {
if (typeof ssConfig !== "object") {
ssConfig = {
href: ssConfig
};
}
ssConfig.href = UMD.resolvePath(ssConfig.href);
const links = document.getElementsByTagName("link");
for (let index = 0; index < links.length; index++) {
const element = links[index];
const href = element.getAttribute("href");
if (href === ssConfig.href) {
return;
}
}
const ss = document.createElement("link");
ss.rel = "stylesheet";
ss.href = ssConfig.href;
if (ssConfig.crossOrigin) {
ss.crossOrigin = ssConfig.crossOrigin;
}
if (ssConfig.integrity) {
ss.integrity = ssConfig.integrity;
}
document.head.appendChild(ss);
}
static installScript(location) {
location = UMD.resolvePath(location);
const links = document.getElementsByTagName("script");
for (let index = 0; index < links.length; index++) {
const element = links[index];
const href = element.getAttribute("src");
if (href === location) {
return element.loaderPromise;
}
}
const script = document.createElement("script");
const p = new Promise((resolve, reject) => {
script.type = "text/javascript";
script.src = location;
const s = script;
script.onload = s.onreadystatechange = () => {
if (s.readyState && s.readyState !== "complete" && s.readyState !== "loaded") {
return;
}
script.onload = s.onreadystatechange = null;
resolve();
};
document.body.appendChild(script);
});
script.loaderPromise = p;
return p;
}
static authorize(authorize = true) {
const detail = {
authorize,
authorized: true
};
const ce = new CustomEvent("authorize", {
bubbles: true,
detail
});
document.body.dispatchEvent(ce);
if (!ce.detail.authorized) {
return false;
}
return true;
}
get url() {
return this.mUrl;
}
set url(v) {
this.mUrl = v;
AtomBinder.refreshValue(this, "url");
}
get contextId() {
return "none";
}
constructor() {
super(null);
this.appReady = false;
this.readyHandlers = [];
this.onError = error => {
console.log(error);
};
App_1.current = this;
this.screen = {};
this.bag = {};
this.put(App_1, this);
this.dispatcher = new AtomDispatcher();
this.dispatcher.start();
this.put(AtomDispatcher, this.dispatcher);
setTimeout(() => {
this.invokeReady();
}, 5);
}
createBusyIndicator(taskInfo) {
this.busyIndicatorService = this.busyIndicatorService || this.resolve(BusyIndicatorService);
return this.busyIndicatorService.createIndicator(taskInfo);
}
syncUrl() {}
callLater(f) {
this.dispatcher.callLater(f);
}
installStyleSheet(ssConfig) {
App_1.installStyleSheet(ssConfig);
}
installScript(location) {
return App_1.installScript(location);
}
updateDefaultStyle(content) {
throw new Error("Platform does not support StyleSheets");
}
waitForPendingCalls() {
return this.dispatcher.waitForAll();
}
setTimeoutAsync(task, timeInMS = 1, previousToken) {
if (previousToken !== void 0) {
clearTimeout(previousToken);
}
return setTimeout(() => {
try {
const p = task();
if (p === null || p === void 0 ? void 0 : p.then) {
p.catch(error => {
if (CancelToken.isCancelled(error)) {
return;
}
console.error(error);
});
}
} catch (e) {
if (CancelToken.isCancelled(e)) {
return;
}
console.error(e);
}
}, timeInMS);
}
runAsync(tf) {
try {
const p = tf();
if (p && p.then && p.catch) {
p.catch(error => {
this.onError("runAsync");
this.onError(error);
});
}
} catch (e) {
this.onError("runAsync");
this.onError(e);
}
}
broadcast(channel, data) {
const ary = this.bag[channel];
if (!ary) {
return;
}
for (const entry of ary.list) {
entry.call(this, channel, data);
}
}
subscribe(channel, action) {
let ary = this.bag[channel];
if (!ary) {
ary = new AtomHandler(channel);
this.bag[channel] = ary;
}
ary.list.push(action);
return {
dispose: () => {
ary.list = ary.list.filter(a => a !== action);
if (!ary.list.length) {
this.bag[channel] = null;
}
}
};
}
main() {}
onReady(f) {
if (this.readyHandlers) {
this.readyHandlers.push(f);
} else {
this.invokeReadyHandler(f);
}
}
invokeReady() {
if (this.invokeReadyHandler) {
for (const iterator of this.readyHandlers) {
this.invokeReadyHandler(iterator);
}
}
this.readyHandlers = null;
}
invokeReadyHandler(f) {
const indicator = this.createBusyIndicator();
const a = f();
if (a && a.then && a.catch) {
a.then(r => {
indicator[Symbol.dispose]();
});
a.catch(e => {
indicator[Symbol.dispose]();
console.error(typeof e === "string" ? e : JSON.stringify(e));
});
return;
}
indicator[Symbol.dispose]();
}
});
_export("App", App = App_1 = __decorate([RegisterSingleton, __metadata("design:paramtypes", [])], App));
}
};
});
//# sourceMappingURL=App.js.map