From b77a4a40a46735f3c194bf5e241efb7b5eac8b49 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Thu, 14 Jul 2016 17:29:01 -0700 Subject: [PATCH] fix(router): expose initalNavigation and dispose so they can be used with webworkers --- modules/@angular/router/src/recognize.ts | 9 +++++---- modules/@angular/router/src/router.ts | 4 ++-- modules/@angular/router/test/recognize.spec.ts | 4 ++-- .../playground/src/web_workers/router/app.html | 6 +++--- .../src/web_workers/router/background_index.ts | 6 +++--- .../src/web_workers/router/index_common.ts | 17 +++++++++++------ .../playground/src/web_workers/router/loader.js | 2 +- tools/public_api_guard/router/index.d.ts | 2 ++ 8 files changed, 29 insertions(+), 21 deletions(-) diff --git a/modules/@angular/router/src/recognize.ts b/modules/@angular/router/src/recognize.ts index 824138f424..47d7081b5d 100644 --- a/modules/@angular/router/src/recognize.ts +++ b/modules/@angular/router/src/recognize.ts @@ -47,7 +47,8 @@ export function recognize(rootComponentType: Type, config: Routes, urlTree: UrlT [], Object.freeze({}), {}, PRIMARY_OUTLET, rootComponentType, null, urlTree.root, -1, InheritedResolve.empty); const rootNode = new TreeNode(root, children); - return of (new RouterStateSnapshot(url, rootNode, Object.freeze(urlTree.queryParams), urlTree.fragment)); + return of (new RouterStateSnapshot( + url, rootNode, Object.freeze(urlTree.queryParams), urlTree.fragment)); } catch (e) { if (e instanceof NoMatch) { return new Observable( @@ -113,9 +114,9 @@ function processPathsWithParamsAgainstRoute( if (route.path === '**') { const params = paths.length > 0 ? last(paths).parameters : {}; const snapshot = new ActivatedRouteSnapshot( - paths, Object.freeze(merge(inherited.allParams, params)), merge(inherited.allData, getData(route)), outlet, - route.component, route, getSourceSegment(rawSegment), getPathIndexShift(rawSegment) - 1, - newInheritedResolve); + paths, Object.freeze(merge(inherited.allParams, params)), + merge(inherited.allData, getData(route)), outlet, route.component, route, + getSourceSegment(rawSegment), getPathIndexShift(rawSegment) - 1, newInheritedResolve); return [new TreeNode(snapshot, [])]; } diff --git a/modules/@angular/router/src/router.ts b/modules/@angular/router/src/router.ts index f9094a9ca7..9b57faf882 100644 --- a/modules/@angular/router/src/router.ts +++ b/modules/@angular/router/src/router.ts @@ -143,7 +143,7 @@ export class Router { } /** - * @internal + * Sets up the location change listener and performs the inital navigation */ initialNavigation(): void { this.setUpLocationChangeListener(); @@ -185,7 +185,7 @@ export class Router { } /** - * @internal + * Disposes of the router. */ dispose(): void { this.locationSubscription.unsubscribe(); } diff --git a/modules/@angular/router/test/recognize.spec.ts b/modules/@angular/router/test/recognize.spec.ts index b08e27734f..bc72517fc7 100644 --- a/modules/@angular/router/test/recognize.spec.ts +++ b/modules/@angular/router/test/recognize.spec.ts @@ -16,7 +16,7 @@ describe('recognize', () => { checkRecognize([{path: 'a/:id', component: ComponentA}], 'a/10', (s: RouterStateSnapshot) => { checkActivatedRoute(s.root, '', {}, RootComponent); const child = s.firstChild(s.root); - expect(() => child.params['prop'] = "new").toThrowError(/Can't add property/); + expect(() => child.params['prop'] = 'new').toThrowError(/Can't add property/); }); }); @@ -515,7 +515,7 @@ describe('recognize', () => { it('should freeze query params object', () => { checkRecognize([{path: 'a', component: ComponentA}], 'a?q=11', (s: RouterStateSnapshot) => { - expect(() => s.queryParams['prop'] = "new").toThrowError(/Can't add property/); + expect(() => s.queryParams['prop'] = 'new').toThrowError(/Can't add property/); }); }); }); diff --git a/modules/playground/src/web_workers/router/app.html b/modules/playground/src/web_workers/router/app.html index a96a63606f..d79243c194 100644 --- a/modules/playground/src/web_workers/router/app.html +++ b/modules/playground/src/web_workers/router/app.html @@ -1,8 +1,8 @@
diff --git a/modules/playground/src/web_workers/router/background_index.ts b/modules/playground/src/web_workers/router/background_index.ts index a82b771524..176a7838d8 100644 --- a/modules/playground/src/web_workers/router/background_index.ts +++ b/modules/playground/src/web_workers/router/background_index.ts @@ -6,16 +6,16 @@ * found in the LICENSE file at https://angular.io/license */ -import {ROUTER_PROVIDERS} from '@angular/router-deprecated'; +import {provideRouter} from '@angular/router'; import {WORKER_APP_LOCATION_PROVIDERS} from '@angular/platform-browser'; import {bootstrapWorkerApp} from '@angular/platform-browser-dynamic'; import {HashLocationStrategy, LocationStrategy} from '@angular/common'; -import {App} from './index_common'; +import {App, ROUTES} from './index_common'; export function main() { bootstrapWorkerApp(App, [ - ROUTER_PROVIDERS, + provideRouter(ROUTES), WORKER_APP_LOCATION_PROVIDERS, {provide: LocationStrategy, useClass: HashLocationStrategy} ]); diff --git a/modules/playground/src/web_workers/router/index_common.ts b/modules/playground/src/web_workers/router/index_common.ts index accf15558a..271fd3f6a7 100644 --- a/modules/playground/src/web_workers/router/index_common.ts +++ b/modules/playground/src/web_workers/router/index_common.ts @@ -10,13 +10,18 @@ import {Component} from '@angular/core'; import {Start} from './components/start'; import {About} from './components/about'; import {Contact} from './components/contact'; -import {ROUTER_DIRECTIVES, RouteConfig, Route} from '@angular/router-deprecated'; +import {ROUTER_DIRECTIVES, Router} from '@angular/router'; @Component({selector: 'app', directives: [ROUTER_DIRECTIVES], templateUrl: 'app.html'}) -@RouteConfig([ - new Route({path: '/', component: Start, name: "Start"}), - new Route({path: '/contact', component: Contact, name: "Contact"}), - new Route({path: '/about', component: About, name: "About"}) -]) export class App { + constructor(router: Router) { + // this should not be required once web worker bootstrap method can use modules + router.initialNavigation(); + } } + +export const ROUTES = [ + {path: '', component: Start}, + {path: 'contact', component: Contact}, + {path: 'about', component: About} +]; \ No newline at end of file diff --git a/modules/playground/src/web_workers/router/loader.js b/modules/playground/src/web_workers/router/loader.js index 7cb0f7ecff..6bbb7b8322 100644 --- a/modules/playground/src/web_workers/router/loader.js +++ b/modules/playground/src/web_workers/router/loader.js @@ -16,7 +16,7 @@ System.config({ '@angular/common': {main: 'index.js', defaultExtension: 'js'}, '@angular/platform-browser': {main: 'index.js', defaultExtension: 'js'}, '@angular/platform-browser-dynamic': {main: 'index.js', defaultExtension: 'js'}, - '@angular/router-deprecated': {main: 'index.js', defaultExtension: 'js'}, + '@angular/router': {main: 'index.js', defaultExtension: 'js'}, 'rxjs': { defaultExtension: 'js' }, diff --git a/tools/public_api_guard/router/index.d.ts b/tools/public_api_guard/router/index.d.ts index 53755d92ee..a7cf3ba3d0 100644 --- a/tools/public_api_guard/router/index.d.ts +++ b/tools/public_api_guard/router/index.d.ts @@ -127,6 +127,8 @@ export declare class Router { url: string; constructor(rootComponentType: Type, resolver: ComponentResolver, urlSerializer: UrlSerializer, outletMap: RouterOutletMap, location: Location, injector: Injector, loader: AppModuleFactoryLoader, config: Routes); createUrlTree(commands: any[], {relativeTo, queryParams, fragment}?: NavigationExtras): UrlTree; + dispose(): void; + initialNavigation(): void; navigate(commands: any[], extras?: NavigationExtras): Promise; navigateByUrl(url: string | UrlTree): Promise; parseUrl(url: string): UrlTree;