UNPKG

@web-atoms/core-docs

Version:
141 lines 6.66 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "../core/ExpressionParser", "../core/types", "../services/NavigationService", "./baseTypes"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ExpressionParser_1 = require("../core/ExpressionParser"); const types_1 = require("../core/types"); const NavigationService_1 = require("../services/NavigationService"); const baseTypes_1 = require("./baseTypes"); /** * Loads given method on based on init/watch properties. * If init is true, method will be executed when view model is initialized. * If watch is true, method will be executed when any of `this.*.*` properties are * modified. This method can be asynchronous. Watch will ignore all assignment * changes within the method. * * Every execution will be delayed by parameter specified in {@link ILoadOptions#watchDelayMS}, * so multiple calls can be accumulated and only one final execution will proceed. This is useful * when you want to load items from API when user is continuously typing in search box. * * Method will have an input parameter for cancelToken {@link CancelToken} which you * can pass it to any REST Api call, before executing next method, cancelToken will * cancel previous execution. * * Either init or watch has to be true, or both can be true as well. */ function Load({ init, showErrorOnInit, watch, watchDelayMS }) { // tslint:disable-next-line: only-arrow-functions return function (target, key) { baseTypes_1.registerInit(target, (vm) => { // tslint:disable-next-line: ban-types const oldMethod = vm[key]; const app = vm.app; let showError = init ? (showErrorOnInit ? true : false) : true; let ct = new types_1.CancelToken(); /** * For the special case of init and watch both are true, * we need to make sure that watch is ignored for first run * * So executing is set to true for the first time */ let executing = init; const m = (ctx) => __awaiter(this, void 0, void 0, function* () { const ns = app.resolve(NavigationService_1.NavigationService); try { const pe = oldMethod.call(vm, ctx); if (pe && pe.then) { return yield pe; } } catch (e) { if (/^(cancelled|canceled)$/i.test(e.toString().trim())) { // tslint:disable-next-line: no-console console.warn(e); return; } if (!showError) { // tslint:disable-next-line: no-console console.error(e); return; } yield ns.alert(e, "Error"); } finally { showError = true; executing = false; } }); if (watch) { const fx = (c1) => __awaiter(this, void 0, void 0, function* () { if (ct) { ct.cancel(); } const ct2 = ct = (c1 || new types_1.CancelToken()); if (executing) { return; } executing = true; try { yield m(ct2); } catch (ex1) { if (/^(cancelled|canceled)$/i.test(ex1.toString().trim())) { // tslint:disable-next-line: no-console console.warn(ex1); } else { // tslint:disable-next-line: no-console console.error(ex1); } } finally { executing = false; ct = null; } }); let timeout = null; // get path stripped as we are passing CancelToken, it will not // parse for this. expressions.. const pathList = ExpressionParser_1.parsePath(oldMethod.toString(), true); if (pathList.length === 0) { throw new Error("Nothing to watch !!"); } vm.setupWatch(pathList, () => { if (executing) { return; } if (timeout) { clearTimeout(timeout); } timeout = setTimeout(() => { timeout = null; fx(); }, watchDelayMS || 100); }); vm[key] = fx; } if (init) { app.runAsync(() => m.call(vm, ct)); } }); }; } exports.default = Load; }); //# sourceMappingURL=Load.js.map