@web-atoms/core
Version:
94 lines (93 loc) • 2.98 kB
JavaScript
System.register(["tslib", "../core/ExpressionParser", "../core/types", "../services/NavigationService", "./baseTypes"], function (_export, _context) {
"use strict";
var __awaiter, parsePath, CancelToken, NavigationService, registerInit;
function Load({
init,
showErrorOnInit,
watch,
watchDelayMS = 100
}) {
return function (target, key) {
registerInit(target, vm => {
const oldMethod = vm[key];
const app = vm.app;
let showError = init ? showErrorOnInit ? true : false : true;
let ct;
const ns = app.resolve(NavigationService);
const m = ctx => __awaiter(this, void 0, void 0, function* () {
try {
const pe = oldMethod.call(vm, ctx);
if (pe === null || pe === void 0 ? void 0 : pe.then) {
return yield pe;
}
} catch (e) {
if (CancelToken.isCancelled(e)) {
return;
}
if (!showError) {
console.error(e);
return;
}
yield ns.alert(e, "Error");
} finally {
ct = null;
showError = true;
}
});
if (watch) {
let timeout;
const fx = c1 => {
if (ct && !ct.cancelled) {
ct.cancel();
}
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
timeout = undefined;
ct = c1 !== null && c1 !== void 0 ? c1 : c1 = new CancelToken();
m(c1).catch(e => console.error(e));
}, watchDelayMS);
};
const pathList = parsePath(oldMethod.toString(), true);
if (pathList.length === 0) {
throw new Error("Nothing to watch !!");
}
vm[key] = fx;
const watchedFunction = () => fx();
if (init) {
app.runAsync(() => __awaiter(this, void 0, void 0, function* () {
try {
yield m(new CancelToken());
} catch (error) {} finally {
vm.setupWatch(pathList, watchedFunction);
}
}));
return;
}
vm.setupWatch(pathList, watchedFunction);
return;
}
if (init) {
app.runAsync(() => m(new CancelToken()));
}
});
};
}
_export("default", Load);
return {
setters: [function (_tslib) {
__awaiter = _tslib.__awaiter;
}, function (_coreExpressionParser) {
parsePath = _coreExpressionParser.parsePath;
}, function (_coreTypes) {
CancelToken = _coreTypes.CancelToken;
}, function (_servicesNavigationService) {
NavigationService = _servicesNavigationService.NavigationService;
}, function (_baseTypes) {
registerInit = _baseTypes.registerInit;
}],
execute: function () {}
};
});
//# sourceMappingURL=Load.js.map