UNPKG

@dooboostore/dom-render

Version:
181 lines 8.34 kB
import { ComponentBase } from '../ComponentBase'; import { DomRender } from '../../DomRender'; import { RawSet } from '../../rawsets/RawSet'; import { ValidUtils } from '@dooboostore/core/valid/ValidUtils'; import { Promises } from '@dooboostore/core/promise'; export var PromiseSwitch; (function (PromiseSwitch_1) { PromiseSwitch_1.selector = 'dr-promise-switch'; class ChildBase extends ComponentBase { constructor() { super(...arguments); this.hidden = true; } } // @Component({ // template: '<div dr-if="!@this@.hidden" dr-strip="true">#innerHTML#</div>', // selector: `${selector}.Default` // }) class Default extends ChildBase { } PromiseSwitch_1.Default = Default; // @Component({ // template: '<div dr-if="!@this@.hidden" dr-strip="true">#innerHTML#</div>', // selector: `${selector}.Pending` // // selector: `System-PromiseSwitch.Pending` // }) class Pending extends ChildBase { } PromiseSwitch_1.Pending = Pending; // @Component({ // template: '<div dr-if="!@this@.hidden" dr-strip="true">#innerHTML#</div>', // selector: `${selector}.Fulfilled` // }) class Fulfilled extends ChildBase { setData(data) { this.data = data; } } PromiseSwitch_1.Fulfilled = Fulfilled; // @Component({ // template: '<div dr-if="!@this@.hidden" dr-strip="true">#innerHTML#</div>', // selector: `${selector}.Rejected` // }) class Rejected extends ChildBase { setData(data) { this.data = data; } } PromiseSwitch_1.Rejected = Rejected; // @Component({ // template: '#innerHTML#', // selector: `${selector}` // }) class PromiseSwitch extends ComponentBase { onCreatedThisChild(child, data) { super.onCreatedThisChild(child, data); this.childStateChange(); } childStateChange() { const defaults = this.getChildren(Default); const pendings = this.getChildren(Pending); const fulfilleds = this.getChildren(Fulfilled); const rejecteds = this.getChildren(Rejected); const full = [...fulfilleds, ...rejecteds, ...pendings, ...defaults]; // console.log('----!!!!!!-------', this.promiseState, full) if (this.promiseState === undefined) { full.filter(it => !it.hidden).forEach(it => it.hidden = true); defaults.filter(it => it.hidden).forEach(it => it.hidden = false); pendings.filter(it => it.getAttribute('defaultView') && it.hidden).forEach(it => it.hidden = false); } else if (this.promiseState.status === 'pending') { full.filter(it => !it.hidden).forEach(it => it.hidden = true); pendings.filter(it => it.hidden).forEach(it => it.hidden = false); } else if (this.promiseState.status === 'fulfilled') { const value = this.promiseState.value; // console.log('-----------fulfilled promises', value); full.filter(it => !it.hidden).forEach(it => it.hidden = true); fulfilleds.forEach(it => { it.setData(value); }); fulfilleds.filter(it => it.hidden).forEach(it => it.hidden = false); } else if (this.promiseState.status === 'rejected') { const value = this.promiseState.reason; full.filter(it => !it.hidden).forEach(it => it.hidden = true); rejecteds.forEach(it => { it.setData(value); }); rejecteds.filter(it => it.hidden).forEach(it => it.hidden = false); } // console.log('----!!!!!!-end------', this.promiseState, full.map(it=>it.hidden)) } onChangeAttrRender(name, value, other) { var _a; super.onChangeAttrRender(name, value, other); // console.log('-----------changer promises', name, value, this.getChildren(Default)); if (this.equalsAttributeName(name, 'data') && value) { // this.childStateChange(); this.promiseState = undefined; this.promiseState = Promises.Result.wrap(typeof value === 'function' ? value() : value); (_a = this.getAttribute('change')) === null || _a === void 0 ? void 0 : _a({ status: 'pending' }); this.childStateChange(); // console.log('----?', this.promiseState) this.promiseState.then(data => { var _a; // console.log('------------then', data); this.childStateChange(); (_a = this.getAttribute('change')) === null || _a === void 0 ? void 0 : _a({ status: 'fulfilled', data: data }); return data; }).catch((e) => { var _a; // console.log('catch', e) this.childStateChange(); (_a = this.getAttribute('change')) === null || _a === void 0 ? void 0 : _a({ status: 'rejected', data: e }); throw e; }).finally(() => { var _a; // console.log('-------------finally--------rrrrrrrrrrr', this.promiseState) // this.childStateChange(); (_a = this.getAttribute('change')) === null || _a === void 0 ? void 0 : _a({ status: 'finally' }); }); // await this.promiseState; // console.log('---------------------rrrrrrrrrrr', this.promiseState) } else if (this.equalsAttributeName(name, 'data') && ValidUtils.isNullOrUndefined(value)) { this.promiseState = undefined; this.childStateChange(); } } } PromiseSwitch_1.PromiseSwitch = PromiseSwitch; })(PromiseSwitch || (PromiseSwitch = {})); export default { promiseDefault: (config) => { return RawSet.createComponentTargetElement({ name: `${PromiseSwitch.selector}-default`, template: '<div dr-if="!@this@.hidden" dr-option-strip="true">#innerHTML#</div>', objFactory: (e, o, r2, counstructorParam) => { return DomRender.run({ rootObject: new PromiseSwitch.Default(...counstructorParam), config: config }); } }); }, promisePending: (config) => { return RawSet.createComponentTargetElement({ name: `${PromiseSwitch.selector}-pending`, template: '<div dr-if="!@this@.hidden" dr-option-strip="true">#innerHTML#</div>', objFactory: (e, o, r2, counstructorParam) => { return DomRender.run({ rootObject: new PromiseSwitch.Pending(...counstructorParam), config: config }); } }); }, promiseFulfilled: (config) => { return RawSet.createComponentTargetElement({ name: `${PromiseSwitch.selector}-fulfilled`, template: '<div dr-if="!@this@.hidden" dr-option-strip="true">#innerHTML#</div>', objFactory: (e, o, r2, counstructorParam) => { return DomRender.run({ rootObject: new PromiseSwitch.Fulfilled(...counstructorParam), config: config }); } }); }, promiseRejected: (config) => { return RawSet.createComponentTargetElement({ name: `${PromiseSwitch.selector}-rejected`, template: '<div dr-if="!@this@.hidden" dr-option-strip="true">#innerHTML#</div>', objFactory: (e, o, r2, counstructorParam) => { return DomRender.run({ rootObject: new PromiseSwitch.Rejected(...counstructorParam), config: config }); } }); }, promiseSwitch: (config) => { return RawSet.createComponentTargetElement({ name: `${PromiseSwitch.selector}`, template: '#innerHTML#', objFactory: (e, o, r2, counstructorParam) => { return DomRender.run({ rootObject: new PromiseSwitch.PromiseSwitch(...counstructorParam), config: config }); } }); }, }; //# sourceMappingURL=PromiseSwitch.js.map