Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -79,7 +79,13 @@ export class Entity {
(t.initExisting as Event<Type, EntityInitExistingEventArgs>).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);
});
});
}
Expand Down Expand Up @@ -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);
});
}
}
Expand Down Expand Up @@ -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);
}
Expand Down
23 changes: 16 additions & 7 deletions src/initilization-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Property } from "./property";
export class InitializationContext {
private newDocument = false;
private tasks = new Set<Promise<any>>();
private waiting: (() => void)[] = [];
private waiting: { onfullfilled: (() => void), onrejected?: (error: Error) => void }[] = [];

constructor(newDocument: boolean) {
this.newDocument = newDocument;
Expand Down Expand Up @@ -35,27 +35,36 @@ 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);
});
});
}

get canProcessQueue() {
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) {
Expand Down