From 85f4a492c837c2ffd1206f633841e79f092eb24d Mon Sep 17 00:00:00 2001 From: Bryan Matthews Date: Tue, 27 Feb 2024 15:26:33 -0500 Subject: [PATCH] WIP --- src/entity.ts | 14 ++++++++++++-- src/initilization-context.ts | 23 ++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/entity.ts b/src/entity.ts index 8b50baa..7b13477 100644 --- a/src/entity.ts +++ b/src/entity.ts @@ -66,7 +66,7 @@ export class Entity { } // Raise the initNew or initExisting event on this type and all base types - this.initialized = new Promise(resolve => { + this.initialized = new Promise((resolve, reject) => { context.whenReady(() => { // Set values of new entity for provided properties if (isNew && properties) @@ -79,7 +79,13 @@ export class Entity { (t.initExisting as Event).publish(t, { entity: this }); } - context.whenReady(resolve); + context.whenReady(resolve, e => { + console.warn(`Inner whenReady call was rejected: `, e); + reject(e); + }); + }, e => { + console.warn(`Initial whenReady call was rejected: `, e); + reject(e); }); }); } @@ -155,6 +161,9 @@ export class Entity { if (context !== null && !hadContext) { context.whenReady(() => { this._context = null; + }, e => { + console.warn(`Initial whenReady call was rejected: `, e); + // reject(e); }); } } @@ -209,6 +218,7 @@ export class Entity { // call markPersistedWhenIdAssigned using whenReady and after the promise resolves to ensure models with no async // behavior produce the correct outcome upon returning from update() + // TODO: onrejected? context.whenReady(markPersistedWhenIdAssigned); return context.ready.then(markPersistedWhenIdAssigned); } diff --git a/src/initilization-context.ts b/src/initilization-context.ts index c7ca606..75f23f4 100644 --- a/src/initilization-context.ts +++ b/src/initilization-context.ts @@ -4,7 +4,7 @@ import { Property } from "./property"; export class InitializationContext { private newDocument = false; private tasks = new Set>(); - private waiting: (() => void)[] = []; + private waiting: { onfullfilled: (() => void), onrejected?: (error: Error) => void }[] = []; constructor(newDocument: boolean) { this.newDocument = newDocument; @@ -35,6 +35,12 @@ export class InitializationContext { this.tasks.delete(task); this.processWaitingQueue(); }); + }).catch(e => { + console.error("Task failed with error: ", e); + Promise.resolve().then(() => { + this.tasks.delete(task); + this.processWaitingQueue(false, e); + }); }); } @@ -42,20 +48,23 @@ export class InitializationContext { return this.tasks.size === 0; } - private processWaitingQueue() { + private processWaitingQueue(fullfilled: boolean = true, error: Error = null) { if (this.canProcessQueue) { while (this.waiting.length > 0 && this.canProcessQueue) { - const done = this.waiting.shift(); - done(); + const waiting = this.waiting.shift(); + if (fullfilled) + waiting.onfullfilled(); + else if (waiting.onrejected) + waiting.onrejected(error); } } } - whenReady(callback: () => void) { + whenReady(onfullfilled: () => void, onrejected?: (error: Error) => void) { if (this.canProcessQueue) - callback(); + onfullfilled(); else - this.waiting.push(callback); + this.waiting.push({ onfullfilled, onrejected }); } tryResolveValue(instance: Entity, property: Property, value: any) {