UNPKG

@web-atoms/core

Version:
321 lines (320 loc) • 9.32 kB
System.register(["tslib", "../App", "../Atom", "../core/AtomBinder", "../core/AtomDisposableList", "../core/AtomWatcher", "../core/BindableProperty", "../di/Inject", "./baseTypes"], function (_export, _context) { "use strict"; var __awaiter, __decorate, __metadata, __param, App, Atom, AtomBinder, AtomDisposableList, AtomWatcher, BindableProperty, Inject, registerInit, AtomViewModel; function runDecoratorInits() { const v = this.constructor.prototype; if (!v) { return; } const ris = v._$_inits; if (ris) { for (const ri of ris) { ri.call(this, this); } } } function privateInit() { return __awaiter(this, void 0, void 0, function* () { try { yield Atom.postAsync(this.app, () => __awaiter(this, void 0, void 0, function* () { runDecoratorInits.apply(this); })); yield Atom.postAsync(this.app, () => __awaiter(this, void 0, void 0, function* () { yield this.init(); this.onReady(); })); if (this.postInit) { for (const i of this.postInit) { i(); } this.postInit = null; } } finally { const pi = this.pendingInits; this.pendingInits = null; for (const iterator of pi) { iterator(); } } }); } function waitForReady(vm) { return __awaiter(this, void 0, void 0, function* () { while (vm.pendingInits) { yield Atom.delay(100); } }); } function Receive(...channel) { return (target, key) => { registerInit(target, vm => { const fx = vm[key]; const a = (ch, d) => { const p = fx.call(vm, ch, d); if (p && p.then && p.catch) { p.catch(e => { console.warn(e); }); } }; const ivm = vm; for (const c of channel) { ivm.registerDisposable(ivm.app.subscribe(c, a)); } }); }; } function BindableReceive(...channel) { return (target, key) => { const bp = BindableProperty(target, key); registerInit(target, vm => { const fx = (cx, m) => { vm[key] = m; }; const ivm = vm; for (const c of channel) { ivm.registerDisposable(ivm.app.subscribe(c, fx)); } }); return bp; }; } function BindableBroadcast(...channel) { return (target, key) => { const bp = BindableProperty(target, key); registerInit(target, vm => { const fx = t => { const v = vm[key]; for (const c of channel) { vm.app.broadcast(c, v); } }; const d = new AtomWatcher(vm, [key.split(".")], fx); d.init(); vm.registerDisposable(d); }); return bp; }; } function Watch(target, key, descriptor) { registerInit(target, vm => { const ivm = vm; if (descriptor && descriptor.get) { ivm.setupWatch(descriptor.get, () => { vm.refresh(key.toString()); }); return; } ivm.setupWatch(vm[key]); }); } function CachedWatch(target, key, descriptor) { const getMethod = descriptor.get; descriptor.get = () => null; registerInit(target, vm => { const ivm = vm; const fieldName = `_${key}`; Object.defineProperty(ivm, key, { enumerable: true, configurable: true, get() { const c = ivm[fieldName] || (ivm[fieldName] = { value: getMethod.apply(ivm) }); return c.value; } }); ivm.setupWatch(getMethod, () => { ivm[fieldName] = null; AtomBinder.refreshValue(ivm, key); }); }); } function Validate(target, key, descriptor) { const getMethod = descriptor.get; descriptor.get = () => null; registerInit(target, vm => { const initialized = { i: false }; const ivm = vm; Object.defineProperty(ivm, key, { enumerable: true, configurable: true, get() { if (vm.mShouldValidate && initialized.i) { return getMethod.apply(this); } return null; } }); ivm.setupWatch(getMethod, () => { initialized.i = true; vm.refresh(key.toString()); }, true, key.toString()); return; }); } _export({ waitForReady: waitForReady, Receive: Receive, BindableReceive: BindableReceive, BindableBroadcast: BindableBroadcast, Watch: Watch, CachedWatch: CachedWatch, Validate: Validate }); return { setters: [function (_tslib) { __awaiter = _tslib.__awaiter; __decorate = _tslib.__decorate; __metadata = _tslib.__metadata; __param = _tslib.__param; }, function (_App) { App = _App.App; }, function (_Atom) { Atom = _Atom.Atom; }, function (_coreAtomBinder) { AtomBinder = _coreAtomBinder.AtomBinder; }, function (_coreAtomDisposableList) { AtomDisposableList = _coreAtomDisposableList.AtomDisposableList; }, function (_coreAtomWatcher) { AtomWatcher = _coreAtomWatcher.AtomWatcher; }, function (_coreBindableProperty) { BindableProperty = _coreBindableProperty.BindableProperty; }, function (_diInject) { Inject = _diInject.Inject; }, function (_baseTypes) { registerInit = _baseTypes.registerInit; }], execute: function () { _export("AtomViewModel", AtomViewModel = class AtomViewModel { get isReady() { return this.pendingInits === null; } get errors() { const e = []; if (!this.mShouldValidate) { return e; } for (const v of this.validations) { if (!v.initialized) { return e; } const error = this[v.name]; if (error) { e.push({ name: v.name, error }); } } return e; } get parent() { return this.mParent; } set parent(v) { if (this.mParent && this.mParent.mChildren) { this.mParent.mChildren.remove(this); } this.mParent = v; if (v) { const c = v.mChildren || (v.mChildren = []); c.add(this); this.registerDisposable({ dispose: () => { c.remove(this); } }); } } get isValid() { let valid = true; const validateWasFalse = this.mShouldValidate === false; this.mShouldValidate = true; for (const v of this.validations) { if (!v.initialized) { v.watcher.init(true); v.initialized = true; } if (this[v.name]) { if (validateWasFalse) { AtomBinder.refreshValue(this, v.name); } valid = false; } } if (this.mChildren) { for (const child of this.mChildren) { if (!child.isValid) { valid = false; } } } AtomBinder.refreshValue(this, "errors"); return valid; } constructor(app) { this.app = app; this.disposables = null; this.validations = []; this.pendingInits = []; this.mShouldValidate = false; this.app.runAsync(() => privateInit.apply(this)); } resetValidations(resetChildren = true) { this.mShouldValidate = false; for (const v of this.validations) { this.refresh(v.name); } if (resetChildren && this.mChildren) { for (const iterator of this.mChildren) { iterator.resetValidations(resetChildren); } } } runAfterInit(f) { if (this.pendingInits) { this.pendingInits.push(f); return; } f(); } refresh(name) { AtomBinder.refreshValue(this, name); } init() { return __awaiter(this, void 0, void 0, function* () {}); } dispose() { if (this.disposables) { this.disposables.dispose(); } } registerDisposable(d) { this.disposables = this.disposables || new AtomDisposableList(); return this.disposables.add(d); } onReady() {} setupWatch(ft, proxy, forValidation, name) { const d = new AtomWatcher(this, ft, proxy, this); if (forValidation) { this.validations = this.validations || []; this.validations.push({ name, watcher: d, initialized: false }); } else { d.init(); } return this.registerDisposable(d); } onPropertyChanged(name) {} }); _export("AtomViewModel", AtomViewModel = __decorate([__param(0, Inject), __metadata("design:paramtypes", [App])], AtomViewModel)); } }; }); //# sourceMappingURL=AtomViewModel.js.map