Introduces event queue processor to create a consistent UI (#11780)

This commit is contained in:
Amir Omidi
2020-08-12 14:06:49 -07:00
committed by GitHub
parent e40a81e8e1
commit 6e306461d7
3 changed files with 65 additions and 15 deletions

View File

@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import * as azdata from 'azdata';
import { MigrationStateModel } from './stateMachine';
import { MigrationStateModel, StateChangeEvent } from './stateMachine';
export abstract class MigrationWizardPage {
constructor(protected readonly wizardPage: azdata.window.WizardPage, protected readonly migrationStateModel: MigrationStateModel) { }
@@ -16,5 +16,37 @@ export abstract class MigrationWizardPage {
public abstract async onPageEnter(): Promise<void>;
public abstract async onPageLeave(): Promise<void>;
private readonly stateChanges: (() => Promise<void>)[] = [];
protected async onStateChangeEvent(e: StateChangeEvent) {
this.stateChanges.push((): Promise<void> => {
return this.handleStateChange(e);
});
this.enableQueueProcessor();
}
private queueActive = false;
private async enableQueueProcessor(): Promise<void> {
if (this.queueActive) {
return;
}
this.queueActive = true;
while (true) {
const stateChangeFunction = this.stateChanges.shift();
if (!stateChangeFunction) {
break;
}
try {
await stateChangeFunction();
} catch (ex) {
console.error(ex);
}
}
this.queueActive = false;
}
protected abstract async handleStateChange(e: StateChangeEvent): Promise<void>;
}

View File

@@ -27,6 +27,7 @@ export enum State {
export interface Model {
readonly sourceConnection: azdata.connection.Connection;
readonly currentState: State;
gatheringInformationError: string | undefined;
}
export interface StateChangeEvent {
@@ -37,6 +38,7 @@ export interface StateChangeEvent {
export class MigrationStateModel implements Model, vscode.Disposable {
private _stateChangeEventEmitter = new vscode.EventEmitter<StateChangeEvent>();
private _currentState: State;
private _gatheringInformationError: string | undefined;
constructor(private readonly _sourceConnection: azdata.connection.Connection) {
this._currentState = State.INIT;
@@ -58,6 +60,14 @@ export class MigrationStateModel implements Model, vscode.Disposable {
this._stateChangeEventEmitter.fire({ oldState, newState: this.currentState });
}
public get gatheringInformationError(): string | undefined {
return this._gatheringInformationError;
}
public set gatheringInformationError(error: string | undefined) {
this._gatheringInformationError = error;
}
public get stateChangeEvent(): vscode.Event<StateChangeEvent> {
return this._stateChangeEventEmitter.event;
}