fix(providers): make providers static analysis friendly

This commit is contained in:
Rob Wormald 2016-06-14 15:20:43 -07:00 committed by vsavkin
parent 127401598b
commit 15f27b5455
2 changed files with 50 additions and 44 deletions

View File

@ -1,5 +1,5 @@
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
import {APP_INITIALIZER, ApplicationRef, ComponentResolver, Injector} from '@angular/core';
import {APP_INITIALIZER, ApplicationRef, ComponentResolver, Injector, OpaqueToken} from '@angular/core';
import {RouterConfig} from './config';
import {Router} from './router';
@ -7,8 +7,48 @@ import {RouterOutletMap} from './router_outlet_map';
import {ActivatedRoute} from './router_state';
import {DefaultUrlSerializer, UrlSerializer} from './url_serializer';
export const ROUTER_CONFIG = new OpaqueToken('ROUTER_CONFIG');
export const ROUTER_OPTIONS = new OpaqueToken('ROUTER_OPTIONS');
export interface ExtraOptions { enableTracing?: boolean; }
export function setupRouter(
ref, resolver, urlSerializer, outletMap, location, injector, config, opts) {
if (ref.componentTypes.length == 0) {
throw new Error('Bootstrap at least one component before injecting Router.');
}
const componentType = ref.componentTypes[0];
const r =
new Router(componentType, resolver, urlSerializer, outletMap, location, injector, config);
ref.registerDisposeListener(() => r.dispose());
if (opts.enableTracing) {
r.events.subscribe(e => {
console.group(`Router Event: ${(<any>e.constructor).name}`);
console.log(e.toString());
console.log(e);
console.groupEnd();
});
}
return r;
}
export function setupRouterInitializer(injector: Injector) {
// https://github.com/angular/angular/issues/9101
// Delay the router instantiation to avoid circular dependency (ApplicationRef ->
// APP_INITIALIZER -> Router)
setTimeout(_ => {
const appRef = injector.get(ApplicationRef);
if (appRef.componentTypes.length == 0) {
appRef.registerBootstrapListener((_) => { injector.get(Router).initialNavigation(); });
} else {
injector.get(Router).initialNavigation();
}
}, 0);
return _ => null;
}
/**
* A list of {@link Provider}s. To use the router, you must add this to your application.
*
@ -27,59 +67,25 @@ export interface ExtraOptions { enableTracing?: boolean; }
* bootstrap(AppCmp, [provideRouter(router)]);
* ```
*/
export function provideRouter(config: RouterConfig, opts: ExtraOptions): any[] {
export function provideRouter(_config: RouterConfig, _opts: ExtraOptions): any[] {
return [
{provide: ROUTER_CONFIG, useValue: _config}, {provide: ROUTER_OPTIONS, useValue: _opts},
Location, {provide: LocationStrategy, useClass: PathLocationStrategy},
{provide: UrlSerializer, useClass: DefaultUrlSerializer},
{
provide: Router,
useFactory: (ref, resolver, urlSerializer, outletMap, location, injector) => {
if (ref.componentTypes.length == 0) {
throw new Error('Bootstrap at least one component before injecting Router.');
}
const componentType = ref.componentTypes[0];
const r = new Router(
componentType, resolver, urlSerializer, outletMap, location, injector, config);
ref.registerDisposeListener(() => r.dispose());
if (opts.enableTracing) {
r.events.subscribe(e => {
console.group(`Router Event: ${(<any>e.constructor).name}`);
console.log(e.toString());
console.log(e);
console.groupEnd();
});
}
return r;
},
deps:
[ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector]
useFactory: setupRouter,
deps: [
ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector,
ROUTER_CONFIG, ROUTER_OPTIONS
]
},
RouterOutletMap,
{provide: ActivatedRoute, useFactory: (r) => r.routerState.root, deps: [Router]},
// Trigger initial navigation
{
provide: APP_INITIALIZER,
multi: true,
useFactory: (injector) => {
// https://github.com/angular/angular/issues/9101
// Delay the router instantiation to avoid circular dependency (ApplicationRef ->
// APP_INITIALIZER -> Router)
setTimeout(_ => {
const appRef = injector.get(ApplicationRef);
if (appRef.componentTypes.length == 0) {
appRef.registerBootstrapListener((_) => { injector.get(Router).initialNavigation(); });
} else {
injector.get(Router).initialNavigation();
}
}, 0);
return _ => null;
},
deps: [Injector]
}
{provide: APP_INITIALIZER, multi: true, useFactory: setupRouterInitializer, deps: [Injector]}
];
}

View File

@ -195,7 +195,7 @@ function prefixedWith(segment: UrlSegment, startIndex: number, commands: any[])
currentPathIndex++;
}
return { match: true, lastIndex: currentCommandIndex };
return {match: true, lastIndex: currentCommandIndex};
}
function createNewSegment(segment: UrlSegment, startIndex: number, commands: any[]): UrlSegment {