44 *--------------------------------------------------------------------------------------------*/
55'use strict' ;
66
7- import { Registry , BaseRegistry } from 'vs/platform/registry/common/platform' ;
7+ import { Registry } from 'vs/platform/registry/common/platform' ;
88import { 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
0 commit comments