Modifying angular bootstrap to add injection at the module level (#1691)

* work on fixing injection

* change bootstrapping method

* add a catch for testing

* remove unneeded code
This commit is contained in:
Anthony Dresser
2018-06-22 16:09:13 -07:00
committed by GitHub
parent a627285a4c
commit 473ddfcdf1
11 changed files with 127 additions and 152 deletions

View File

@@ -3,20 +3,31 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { NgModuleRef, enableProdMode, InjectionToken, ReflectiveInjector, Type, PlatformRef, Provider } from '@angular/core';
import { NgModuleRef, enableProdMode, InjectionToken, Type, PlatformRef, Provider, Injector, Optional, Inject, ComponentFactoryResolver } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { IEditorInput } from 'vs/platform/editor/common/editor';
import { IInstantiationService, _util } from 'vs/platform/instantiation/common/instantiation';
const selectorCounter = new Map<string, number>();
const serviceMap = new Map<string, IInstantiationService>();
export const IBootstrapParams = new InjectionToken('bootstrap_params');
export function providerIterator(service: IInstantiationService): Provider[] {
return Array.from(_util.serviceIds.values()).map(v => {
return {
provide: v, useFactory: () => {
return (<any>service)._getOrCreateServiceInstance(v);
}
};
});
}
export const ISelector = new InjectionToken<string>('selector');
export const IBootstrapParams = new InjectionToken<IBootstrapParams>('bootstrap_params');
export interface IBootstrapParams {
}
export type IModuleFactory<T> = (params: IBootstrapParams, selector: string) => Type<T>;
export type IModuleFactory<T> = (params: IBootstrapParams, selector: string, service: IInstantiationService) => Type<T>;
function createUniqueSelector(selector: string): string {
let num: number;
@@ -37,31 +48,13 @@ export function bootstrapAngular<T>(service: IInstantiationService, moduleType:
let selector = document.createElement(uniqueSelectorString);
container.appendChild(selector);
serviceMap.set(uniqueSelectorString, service);
if (!platform) {
// Perform the bootsrap
const providers: Provider = [];
_util.serviceIds.forEach(id => {
providers.push({
provide: id, useFactory: () => {
return (<any>serviceMap.get(uniqueSelectorString))._getOrCreateServiceInstance(id);
}
});
});
platform = platformBrowserDynamic(providers);
platform = platformBrowserDynamic();
}
platform.bootstrapModule(moduleType(params, uniqueSelectorString)).then(moduleRef => {
platform.bootstrapModule(moduleType(params, uniqueSelectorString, service)).then(moduleRef => {
if (input) {
input.onDispose(() => {
serviceMap.delete(uniqueSelectorString);
moduleRef.onDestroy(() => {
serviceMap.delete(uniqueSelectorString);
});
moduleRef.destroy();
});
}

View File

@@ -95,7 +95,6 @@ export class SingleQueryManagementService {
*/
@Injectable()
export class CommonServiceInterface extends AngularDisposable {
protected _uniqueSelector: string;
protected _uri: string;
/* Special Services */
@@ -115,6 +114,12 @@ export class CommonServiceInterface extends AngularDisposable {
@Inject(IQueryManagementService) protected _queryManagementService: IQueryManagementService
) {
super();
// during testing there may not be params
if (this._params) {
this.scopedContextKeyService = this._params.scopedContextService;
this._connectionContextKey = this._params.connectionContextKey;
this.uri = this._params.ownerUri;
}
}
public get metadataService(): SingleConnectionMetadataService {
@@ -133,20 +138,6 @@ export class CommonServiceInterface extends AngularDisposable {
return this._singleQueryManagementService;
}
/**
* Set the selector for this instance, should only be set once
*/
public set selector(selector: string) {
this._uniqueSelector = selector;
this._getbootstrapParams();
}
protected _getbootstrapParams(): void {
this.scopedContextKeyService = this._params.scopedContextService;
this._connectionContextKey = this._params.connectionContextKey;
this.uri = this._params.ownerUri;
}
protected setUri(uri: string) {
this._uri = uri;
this._singleMetadataService = new SingleConnectionMetadataService(this._metadataService, this._uri);