Skip to content

Commit 90fec34

Browse files
author
Benjamin Pasero
committed
allow to register workbench contributions per phase of instantiation (microsoft#38080)
1 parent fd49805 commit 90fec34

3 files changed

Lines changed: 46 additions & 54 deletions

File tree

src/vs/platform/registry/common/platform.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import Types = require('vs/base/common/types');
88
import Assert = require('vs/base/common/assert');
9-
import { IInstantiationService, IConstructorSignature0 } from 'vs/platform/instantiation/common/instantiation';
109

1110
export interface IRegistry {
1211

@@ -58,42 +57,3 @@ class RegistryImpl implements IRegistry {
5857
}
5958

6059
export const Registry = <IRegistry>new RegistryImpl();
61-
62-
/**
63-
* A base class for registries that leverage the instantiation service to create instances.
64-
*/
65-
export class BaseRegistry<T> {
66-
private toBeInstantiated: IConstructorSignature0<T>[] = [];
67-
private instances: T[] = [];
68-
private instantiationService: IInstantiationService;
69-
70-
public setInstantiationService(service: IInstantiationService): void {
71-
this.instantiationService = service;
72-
73-
while (this.toBeInstantiated.length > 0) {
74-
let entry = this.toBeInstantiated.shift();
75-
this.instantiate(entry);
76-
}
77-
}
78-
79-
private instantiate(ctor: IConstructorSignature0<T>): void {
80-
let instance = this.instantiationService.createInstance(ctor);
81-
this.instances.push(instance);
82-
}
83-
84-
_register(ctor: IConstructorSignature0<T>): void {
85-
if (this.instantiationService) {
86-
this.instantiate(ctor);
87-
} else {
88-
this.toBeInstantiated.push(ctor);
89-
}
90-
}
91-
92-
_getInstances(): T[] {
93-
return this.instances.slice(0);
94-
}
95-
96-
_setInstances(instances: T[]): void {
97-
this.instances = instances;
98-
}
99-
}

src/vs/workbench/common/contributions.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import { Registry, BaseRegistry } from 'vs/platform/registry/common/platform';
7+
import { Registry } from 'vs/platform/registry/common/platform';
88
import { IInstantiationService, IConstructorSignature0 } from 'vs/platform/instantiation/common/instantiation';
9+
import { ILifecycleService, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
910

1011
// --- Workbench Contribution Registry
1112

@@ -31,29 +32,60 @@ export interface IWorkbenchContributionsRegistry {
3132
/**
3233
* Registers a workbench contribution to the platform that will be loaded when the workbench starts and disposed when
3334
* the workbench shuts down.
35+
*
36+
* @param phase the lifecycle phase when to instantiate the contribution.
3437
*/
35-
registerWorkbenchContribution(contribution: IWorkbenchContributionSignature): void;
38+
registerWorkbenchContribution(contribution: IWorkbenchContributionSignature, phase?: LifecyclePhase): void;
3639

3740
/**
38-
* Returns all workbench contributions that are known to the platform.
41+
* Starts the registry by providing the required services.
3942
*/
40-
getWorkbenchContributions(): IWorkbenchContribution[];
41-
42-
setInstantiationService(service: IInstantiationService): void;
43+
start(instantiationService: IInstantiationService, lifecycleService: ILifecycleService): void;
4344
}
4445

45-
class WorkbenchContributionsRegistry extends BaseRegistry<IWorkbenchContribution> implements IWorkbenchContributionsRegistry {
46+
export class WorkbenchContributionsRegistry implements IWorkbenchContributionsRegistry {
47+
private instantiationService: IInstantiationService;
48+
private lifecycleService: ILifecycleService;
49+
50+
private toBeInstantiated: Map<LifecyclePhase, IConstructorSignature0<IWorkbenchContribution>[]> = new Map<LifecyclePhase, IConstructorSignature0<IWorkbenchContribution>[]>();
51+
52+
public registerWorkbenchContribution(ctor: IWorkbenchContributionSignature, phase: LifecyclePhase = LifecyclePhase.Starting): void {
4653

47-
public registerWorkbenchContribution(ctor: IWorkbenchContributionSignature): void {
48-
super._register(ctor);
54+
// Instantiate directly if we are already matching the provided phase
55+
if (this.instantiationService && this.lifecycleService && this.lifecycleService.phase >= phase) {
56+
this.instantiationService.createInstance(ctor);
57+
}
58+
59+
// Otherwise keep contributions by lifecycle phase
60+
else {
61+
let toBeInstantiated = this.toBeInstantiated.get(phase);
62+
if (!toBeInstantiated) {
63+
toBeInstantiated = [];
64+
this.toBeInstantiated.set(phase, toBeInstantiated);
65+
}
66+
67+
toBeInstantiated.push(ctor);
68+
}
4969
}
5070

51-
public getWorkbenchContributions(): IWorkbenchContribution[] {
52-
return super._getInstances();
71+
public start(instantiationService: IInstantiationService, lifecycleService: ILifecycleService): void {
72+
this.instantiationService = instantiationService;
73+
this.lifecycleService = lifecycleService;
74+
75+
[LifecyclePhase.Starting, LifecyclePhase.Restoring, LifecyclePhase.Running, LifecyclePhase.ShuttingDown].forEach(phase => {
76+
this.instantiateByPhase(instantiationService, lifecycleService, phase);
77+
});
5378
}
5479

55-
public setWorkbenchContributions(contributions: IWorkbenchContribution[]): void {
56-
super._setInstances(contributions);
80+
private instantiateByPhase(instantiationService: IInstantiationService, lifecycleService: ILifecycleService, phase: LifecyclePhase): void {
81+
lifecycleService.when(phase).then(() => {
82+
const toBeInstantiated = this.toBeInstantiated.get(phase);
83+
if (toBeInstantiated) {
84+
while (toBeInstantiated.length > 0) {
85+
instantiationService.createInstance(toBeInstantiated.shift());
86+
}
87+
}
88+
});
5789
}
5890
}
5991

src/vs/workbench/electron-browser/workbench.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ export class Workbench implements IPartService {
615615

616616
// Set the some services to registries that have been created eagerly
617617
Registry.as<IActionBarRegistry>(ActionBarExtensions.Actionbar).setInstantiationService(this.instantiationService);
618-
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).setInstantiationService(this.instantiationService);
618+
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).start(this.instantiationService, this.lifecycleService);
619619
Registry.as<IEditorInputFactoryRegistry>(EditorExtensions.EditorInputFactories).setInstantiationService(this.instantiationService);
620620

621621
this.instantiationService.createInstance(DefaultConfigurationExportHelper);

0 commit comments

Comments
 (0)