contributions.ts 3.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

7
import { Registry } from 'vs/platform/registry/common/platform';
8
import { IInstantiationService, IConstructorSignature0 } from 'vs/platform/instantiation/common/instantiation';
9
import { ILifecycleService, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
E
Erich Gamma 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// --- Workbench Contribution Registry

/**
 * A workbench contribution that will be loaded when the workbench starts and disposed when the workbench shuts down.
 */
export interface IWorkbenchContribution {

	/**
	 * The unique identifier of this workbench contribution.
	 */
	getId(): string;
}

export namespace Extensions {
	export const Workbench = 'workbench.contributions.kind';
B
Benjamin Pasero 已提交
26
}
E
Erich Gamma 已提交
27

28
export type IWorkbenchContributionSignature = IConstructorSignature0<IWorkbenchContribution>;
E
Erich Gamma 已提交
29 30 31 32 33 34

export interface IWorkbenchContributionsRegistry {

	/**
	 * Registers a workbench contribution to the platform that will be loaded when the workbench starts and disposed when
	 * the workbench shuts down.
35 36
	 *
	 * @param phase the lifecycle phase when to instantiate the contribution.
E
Erich Gamma 已提交
37
	 */
38
	registerWorkbenchContribution(contribution: IWorkbenchContributionSignature, phase?: LifecyclePhase): void;
E
Erich Gamma 已提交
39 40

	/**
41
	 * Starts the registry by providing the required services.
E
Erich Gamma 已提交
42
	 */
43
	start(instantiationService: IInstantiationService, lifecycleService: ILifecycleService): void;
E
Erich Gamma 已提交
44 45
}

46 47 48 49 50 51 52
export class WorkbenchContributionsRegistry implements IWorkbenchContributionsRegistry {
	private instantiationService: IInstantiationService;
	private lifecycleService: ILifecycleService;

	private toBeInstantiated: Map<LifecyclePhase, IConstructorSignature0<IWorkbenchContribution>[]> = new Map<LifecyclePhase, IConstructorSignature0<IWorkbenchContribution>[]>();

	public registerWorkbenchContribution(ctor: IWorkbenchContributionSignature, phase: LifecyclePhase = LifecyclePhase.Starting): void {
E
Erich Gamma 已提交
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
		// Instantiate directly if we are already matching the provided phase
		if (this.instantiationService && this.lifecycleService && this.lifecycleService.phase >= phase) {
			this.instantiationService.createInstance(ctor);
		}

		// Otherwise keep contributions by lifecycle phase
		else {
			let toBeInstantiated = this.toBeInstantiated.get(phase);
			if (!toBeInstantiated) {
				toBeInstantiated = [];
				this.toBeInstantiated.set(phase, toBeInstantiated);
			}

			toBeInstantiated.push(ctor);
		}
E
Erich Gamma 已提交
69 70
	}

71 72 73 74
	public start(instantiationService: IInstantiationService, lifecycleService: ILifecycleService): void {
		this.instantiationService = instantiationService;
		this.lifecycleService = lifecycleService;

75
		[LifecyclePhase.Starting, LifecyclePhase.Restoring, LifecyclePhase.Running, LifecyclePhase.RunningForABit, LifecyclePhase.ShuttingDown].forEach(phase => {
76 77
			this.instantiateByPhase(instantiationService, lifecycleService, phase);
		});
E
Erich Gamma 已提交
78 79
	}

80 81 82 83 84 85 86 87 88
	private instantiateByPhase(instantiationService: IInstantiationService, lifecycleService: ILifecycleService, phase: LifecyclePhase): void {
		lifecycleService.when(phase).then(() => {
			const toBeInstantiated = this.toBeInstantiated.get(phase);
			if (toBeInstantiated) {
				while (toBeInstantiated.length > 0) {
					instantiationService.createInstance(toBeInstantiated.shift());
				}
			}
		});
E
Erich Gamma 已提交
89 90 91 92
	}
}

Registry.add(Extensions.Workbench, new WorkbenchContributionsRegistry());