/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as azdata from 'azdata'; import { MigrationStateModel, StateChangeEvent } from './stateMachine'; export abstract class MigrationWizardPage { constructor( protected readonly wizard: azdata.window.Wizard, protected readonly wizardPage: azdata.window.WizardPage, protected readonly migrationStateModel: MigrationStateModel ) { } public registerWizardContent(): Promise { return new Promise(async (resolve, reject) => { this.wizardPage.registerContent(async (view) => { try { await this.registerContent(view); resolve(); } catch (ex) { reject(ex); } finally { reject(new Error()); } }); }); } protected abstract registerContent(view: azdata.ModelView): Promise; public getwizardPage(): azdata.window.WizardPage { return this.wizardPage; } public abstract onPageEnter(pageChangeInfo: azdata.window.WizardPageChangeInfo): Promise; public abstract onPageLeave(pageChangeInfo: azdata.window.WizardPageChangeInfo): Promise; private readonly stateChanges: (() => Promise)[] = []; protected async onStateChangeEvent(e: StateChangeEvent) { this.stateChanges.push((): Promise => { return this.handleStateChange(e); }); this.enableQueueProcessor(); } private queueActive = false; private async enableQueueProcessor(): Promise { 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 handleStateChange(e: StateChangeEvent): Promise; public canEnter(): Promise { return Promise.resolve(true); } public canLeave(): Promise { return Promise.resolve(true); } protected async goToNextPage(): Promise { const current = this.wizard.currentPage; await this.wizard.setCurrentPage(current + 1); } }