Do ModelView initialization actions before others (#11512)

This commit is contained in:
Charles Gagnon
2020-07-27 10:40:40 -07:00
committed by GitHub
parent 6d77176b92
commit 67ddf81562
3 changed files with 32 additions and 24 deletions

View File

@@ -19,6 +19,7 @@ export class ModelStore implements IModelStore {
private _descriptorMappings: { [x: string]: IComponentDescriptor } = {};
private _componentMappings: { [x: string]: IComponent } = {};
private _componentActions: { [x: string]: Deferred<IComponent> } = {};
private _componentInitializationActions: { [x: string]: Deferred<IComponent> } = {};
private _validationCallbacks: ((componentId: string) => Thenable<boolean>)[] = [];
constructor() {
}
@@ -51,12 +52,12 @@ export class ModelStore implements IModelStore {
return this._componentMappings[componentId];
}
eventuallyRunOnComponent<T>(componentId: string, action: (component: IComponent) => T): Promise<T> {
eventuallyRunOnComponent<T>(componentId: string, action: (component: IComponent) => T, isInitialization: boolean = false): Promise<T> {
let component = this.getComponent(componentId);
if (component) {
return Promise.resolve(action(component));
} else {
return this.addPendingAction(componentId, action);
return this.addPendingAction(componentId, action, isInitialization);
}
}
@@ -69,13 +70,18 @@ export class ModelStore implements IModelStore {
return Promise.all(this._validationCallbacks.map(callback => callback(componentId))).then(validations => validations.every(validation => validation === true));
}
private addPendingAction<T>(componentId: string, action: (component: IComponent) => T): Promise<T> {
private addPendingAction<T>(componentId: string, action: (component: IComponent) => T, isInitialization: boolean): Promise<T> {
// We create a promise and chain it onto a tracking promise whose resolve method
// will only be called once the component is created
let deferredPromise = this._componentActions[componentId];
// If this is an initialization action we want to run it before the other actions that may have come in
// after initialization but before the component was finished being created or we hit race conditions with
// setting properties
const actionsStore = isInitialization ? this._componentInitializationActions : this._componentActions;
let deferredPromise = actionsStore[componentId];
if (!deferredPromise) {
deferredPromise = new Deferred();
this._componentActions[componentId] = deferredPromise;
actionsStore[componentId] = deferredPromise;
}
let promise = deferredPromise.promise.then((component) => {
return action(component);
@@ -84,9 +90,10 @@ export class ModelStore implements IModelStore {
}
private runPendingActions(componentId: string, component: IComponent) {
let promiseTracker = this._componentActions[componentId];
if (promiseTracker) {
promiseTracker.resolve(component);
}
// If we have initialization actions to run start those first
const initializationActionsPromise = this._componentInitializationActions[componentId];
initializationActionsPromise?.resolve(component);
const actionsPromise = this._componentActions[componentId];
actionsPromise?.resolve(component);
}
}