Skip to content

Commit 6a0cbb8

Browse files
committed
refactor(core): type ComponentRef, ComponentFactory and ComponentFixture by the component type
BREAKING CHANGE: - `ComponetRef`, `ComponentFactory`, `ComponentFixture` now all require a type parameter with the component type. Closes angular#8361
1 parent 4e2c683 commit 6a0cbb8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+225
-209
lines changed

modules/angular2/platform/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export function browserPlatform(): PlatformRef {
121121
*/
122122
export function bootstrap(
123123
appComponentType: Type,
124-
customProviders?: Array<any /*Type | Provider | any[]*/>): Promise<ComponentRef> {
124+
customProviders?: Array<any /*Type | Provider | any[]*/>): Promise<ComponentRef<any>> {
125125
reflector.reflectionCapabilities = new ReflectionCapabilities();
126126
var appInjector = ReflectiveInjector.resolveAndCreate(
127127
[BROWSER_APP_PROVIDERS, isPresent(customProviders) ? customProviders : []],

modules/angular2/platform/browser_static.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function browserStaticPlatform(): PlatformRef {
4747
*/
4848
export function bootstrapStatic(appComponentType: Type,
4949
customProviders?: Array<any /*Type | Provider | any[]*/>,
50-
initReflector?: Function): Promise<ComponentRef> {
50+
initReflector?: Function): Promise<ComponentRef<any>> {
5151
if (isPresent(initReflector)) {
5252
initReflector();
5353
}

modules/angular2/platform/worker_app.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ PlatformRef workerAppPlatform(SendPort renderSendPort) {
3939
return platform;
4040
}
4141

42-
Future<ComponentRef> bootstrapApp(
42+
Future<ComponentRef<dynamic>> bootstrapApp(
4343
SendPort renderSendPort,
4444
Type appComponentType,
4545
[List<dynamic /*Type | Provider | any[]*/> customProviders]) {

modules/angular2/platform/worker_app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function workerAppPlatform(): PlatformRef {
4444

4545
export function bootstrapApp(
4646
appComponentType: Type,
47-
customProviders?: Array<any /*Type | Provider | any[]*/>): Promise<ComponentRef> {
47+
customProviders?: Array<any /*Type | Provider | any[]*/>): Promise<ComponentRef<any>> {
4848
var appInjector = ReflectiveInjector.resolveAndCreate(
4949
[WORKER_APP_APPLICATION, isPresent(customProviders) ? customProviders : []],
5050
workerAppPlatform().injector);

modules/angular2/src/alt_router/directives/router_outlet.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {isPresent, isBlank} from 'angular2/src/facade/lang';
1515

1616
@Directive({selector: 'router-outlet'})
1717
export class RouterOutlet {
18-
private _loaded: ComponentRef;
18+
private _loaded: ComponentRef<any>;
1919
public outletMap: RouterOutletMap;
2020

2121
constructor(parentOutletMap: RouterOutletMap, private _location: ViewContainerRef,
@@ -32,8 +32,8 @@ export class RouterOutlet {
3232

3333
get isLoaded(): boolean { return isPresent(this._loaded); }
3434

35-
load(factory: ComponentFactory, providers: ResolvedReflectiveProvider[],
36-
outletMap: RouterOutletMap): ComponentRef {
35+
load(factory: ComponentFactory<any>, providers: ResolvedReflectiveProvider[],
36+
outletMap: RouterOutletMap): ComponentRef<any> {
3737
this.outletMap = outletMap;
3838
let inj = ReflectiveInjector.fromResolvedProviders(providers, this._location.parentInjector);
3939
this._loaded = this._location.createComponent(factory, this._location.length, inj, []);

modules/angular2/src/alt_router/segments.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ export class RouteSegment {
8686
_type: Type;
8787

8888
/** @internal */
89-
_componentFactory: ComponentFactory;
89+
_componentFactory: ComponentFactory<any>;
9090

9191
constructor(public urlSegments: UrlSegment[], public parameters: {[key: string]: string},
92-
public outlet: string, type: Type, componentFactory: ComponentFactory) {
92+
public outlet: string, type: Type, componentFactory: ComponentFactory<any>) {
9393
this._type = type;
9494
this._componentFactory = componentFactory;
9595
}
@@ -123,6 +123,6 @@ export function equalSegments(a: RouteSegment, b: RouteSegment): boolean {
123123
return StringMapWrapper.equals(a.parameters, b.parameters);
124124
}
125125

126-
export function routeSegmentComponentFactory(a: RouteSegment): ComponentFactory {
126+
export function routeSegmentComponentFactory(a: RouteSegment): ComponentFactory<any> {
127127
return a._componentFactory;
128128
}

modules/angular2/src/compiler/offline_compiler.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,18 @@ export class OfflineCompiler {
6161
var hostMeta = createHostComponentMeta(compMeta.type, compMeta.selector);
6262
var hostViewFactoryVar = this._compileComponent(hostMeta, [compMeta], [], statements);
6363
var compFactoryVar = `${compMeta.type.name}NgFactory`;
64-
statements.push(o.variable(compFactoryVar)
65-
.set(o.importExpr(_COMPONENT_FACTORY_IDENTIFIER)
66-
.instantiate(
67-
[
68-
o.literal(compMeta.selector),
69-
o.variable(hostViewFactoryVar),
70-
o.importExpr(compMeta.type)
71-
],
72-
o.importType(_COMPONENT_FACTORY_IDENTIFIER, null,
73-
[o.TypeModifier.Const])))
74-
.toDeclStmt(null, [o.StmtModifier.Final]));
64+
statements.push(
65+
o.variable(compFactoryVar)
66+
.set(o.importExpr(_COMPONENT_FACTORY_IDENTIFIER, [o.importType(compMeta.type)])
67+
.instantiate(
68+
[
69+
o.literal(compMeta.selector),
70+
o.variable(hostViewFactoryVar),
71+
o.importExpr(compMeta.type)
72+
],
73+
o.importType(_COMPONENT_FACTORY_IDENTIFIER,
74+
[o.importType(compMeta.type)], [o.TypeModifier.Const])))
75+
.toDeclStmt(null, [o.StmtModifier.Final]));
7576
exportedVars.push(compFactoryVar);
7677
});
7778
return this._codegenSourceModule(moduleUrl, statements, exportedVars);

modules/angular2/src/compiler/runtime_compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class RuntimeCompiler implements ComponentResolver {
7777
private _viewCompiler: ViewCompiler, private _xhr: XHR,
7878
private _genConfig: CompilerConfig) {}
7979

80-
resolveComponent(componentType: Type): Promise<ComponentFactory> {
80+
resolveComponent(componentType: Type): Promise<ComponentFactory<any>> {
8181
var compMeta: CompileDirectiveMetadata =
8282
this._metadataResolver.getDirectiveMetadata(componentType);
8383
var hostCacheKey = this._hostCacheKeys.get(componentType);

modules/angular2/src/core/application_ref.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ export function getPlatform(): PlatformRef {
9494
* Shortcut for ApplicationRef.bootstrap.
9595
* Requires a platform the be created first.
9696
*/
97-
export function coreBootstrap(injector: Injector,
98-
componentFactory: ComponentFactory): ComponentRef {
97+
export function coreBootstrap<C>(injector: Injector,
98+
componentFactory: ComponentFactory<C>): ComponentRef<C> {
9999
var appRef: ApplicationRef = injector.get(ApplicationRef);
100100
return appRef.bootstrap(componentFactory);
101101
}
@@ -106,7 +106,7 @@ export function coreBootstrap(injector: Injector,
106106
* Requires a platform the be created first.
107107
*/
108108
export function coreLoadAndBootstrap(injector: Injector,
109-
componentType: Type): Promise<ComponentRef> {
109+
componentType: Type): Promise<ComponentRef<any>> {
110110
var appRef: ApplicationRef = injector.get(ApplicationRef);
111111
return appRef.run(() => {
112112
var componentResolver: ComponentResolver = injector.get(ComponentResolver);
@@ -190,7 +190,7 @@ export abstract class ApplicationRef {
190190
* Register a listener to be called each time `bootstrap()` is called to bootstrap
191191
* a new root component.
192192
*/
193-
abstract registerBootstrapListener(listener: (ref: ComponentRef) => void): void;
193+
abstract registerBootstrapListener(listener: (ref: ComponentRef<any>) => void): void;
194194

195195
/**
196196
* Register a listener to be called when the application is disposed.
@@ -221,7 +221,7 @@ export abstract class ApplicationRef {
221221
* ### Example
222222
* {@example core/ts/platform/platform.ts region='longform'}
223223
*/
224-
abstract bootstrap(componentFactory: ComponentFactory): ComponentRef;
224+
abstract bootstrap<C>(componentFactory: ComponentFactory<C>): ComponentRef<C>;
225225

226226
/**
227227
* Retrieve the application {@link Injector}.
@@ -266,7 +266,7 @@ export class ApplicationRef_ extends ApplicationRef {
266266
/** @internal */
267267
private _disposeListeners: Function[] = [];
268268
/** @internal */
269-
private _rootComponents: ComponentRef[] = [];
269+
private _rootComponents: ComponentRef<any>[] = [];
270270
/** @internal */
271271
private _rootComponentTypes: Type[] = [];
272272
/** @internal */
@@ -315,7 +315,7 @@ export class ApplicationRef_ extends ApplicationRef {
315315
(_) => { this._zone.run(() => { this.tick(); }); });
316316
}
317317

318-
registerBootstrapListener(listener: (ref: ComponentRef) => void): void {
318+
registerBootstrapListener(listener: (ref: ComponentRef<any>) => void): void {
319319
this._bootstrapListeners.push(listener);
320320
}
321321

@@ -357,7 +357,7 @@ export class ApplicationRef_ extends ApplicationRef {
357357
return isPromise(result) ? completer.promise : result;
358358
}
359359

360-
bootstrap(componentFactory: ComponentFactory): ComponentRef {
360+
bootstrap<C>(componentFactory: ComponentFactory<C>): ComponentRef<C> {
361361
if (!this._asyncInitDone) {
362362
throw new BaseException(
363363
'Cannot bootstrap as there are still asynchronous initializers running. Wait for them using waitForAsyncInitializers().');
@@ -383,15 +383,15 @@ export class ApplicationRef_ extends ApplicationRef {
383383
}
384384

385385
/** @internal */
386-
_loadComponent(componentRef: ComponentRef): void {
386+
_loadComponent(componentRef: ComponentRef<any>): void {
387387
this._changeDetectorRefs.push(componentRef.changeDetectorRef);
388388
this.tick();
389389
this._rootComponents.push(componentRef);
390390
this._bootstrapListeners.forEach((listener) => listener(componentRef));
391391
}
392392

393393
/** @internal */
394-
_unloadComponent(componentRef: ComponentRef): void {
394+
_unloadComponent(componentRef: ComponentRef<any>): void {
395395
if (!ListWrapper.contains(this._rootComponents, componentRef)) {
396396
return;
397397
}

modules/angular2/src/core/linker/component_factory.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {ChangeDetectorRef} from '../change_detection/change_detection';
1414
* Component Instance and allows you to destroy the Component Instance via the {@link #destroy}
1515
* method.
1616
*/
17-
export abstract class ComponentRef {
17+
export abstract class ComponentRef<C> {
1818
/**
1919
* Location of the Host Element of this Component Instance.
2020
*/
@@ -28,7 +28,7 @@ export abstract class ComponentRef {
2828
/**
2929
* The instance of the Component.
3030
*/
31-
get instance(): any { return unimplemented(); };
31+
get instance(): C { return unimplemented(); };
3232

3333
/**
3434
* The {@link ViewRef} of the Host View of this Component instance.
@@ -56,11 +56,11 @@ export abstract class ComponentRef {
5656
abstract onDestroy(callback: Function): void;
5757
}
5858

59-
export class ComponentRef_ extends ComponentRef {
59+
export class ComponentRef_<C> extends ComponentRef<C> {
6060
constructor(private _hostElement: AppElement, private _componentType: Type) { super(); }
6161
get location(): ElementRef { return this._hostElement.elementRef; }
6262
get injector(): Injector { return this._hostElement.injector; }
63-
get instance(): any { return this._hostElement.component; };
63+
get instance(): C { return this._hostElement.component; };
6464
get hostView(): ViewRef { return this._hostElement.parentView.ref; };
6565
get changeDetectorRef(): ChangeDetectorRef { return this._hostElement.parentView.ref; };
6666
get componentType(): Type { return this._componentType; }
@@ -72,7 +72,7 @@ export class ComponentRef_ extends ComponentRef {
7272
const EMPTY_CONTEXT = /*@ts2dart_const*/ new Object();
7373

7474
/*@ts2dart_const*/
75-
export class ComponentFactory {
75+
export class ComponentFactory<C> {
7676
constructor(public selector: string, private _viewFactory: Function,
7777
private _componentType: Type) {}
7878

@@ -82,14 +82,14 @@ export class ComponentFactory {
8282
* Creates a new component.
8383
*/
8484
create(injector: Injector, projectableNodes: any[][] = null,
85-
rootSelectorOrNode: string | any = null): ComponentRef {
85+
rootSelectorOrNode: string | any = null): ComponentRef<C> {
8686
var vu: ViewUtils = injector.get(ViewUtils);
8787
if (isBlank(projectableNodes)) {
8888
projectableNodes = [];
8989
}
9090
// Note: Host views don't need a declarationAppElement!
9191
var hostView = this._viewFactory(vu, injector, null);
9292
var hostElement = hostView.create(EMPTY_CONTEXT, projectableNodes, rootSelectorOrNode);
93-
return new ComponentRef_(hostElement, this._componentType);
93+
return new ComponentRef_<C>(hostElement, this._componentType);
9494
}
9595
}

0 commit comments

Comments
 (0)