Skip to content

Commit 8bea667

Browse files
jteplitzrkirov
authored andcommitted
feat(WebWorker): Add Router Support for WebWorker Apps
Closes angular#3563.
1 parent 800c8f1 commit 8bea667

Some content is hidden

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

43 files changed

+838
-152
lines changed

modules/angular2/platform/worker_app.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ export 'package:angular2/src/web_workers/shared/service_message_broker.dart'
1212
show ReceivedMessage, ServiceMessageBroker, ServiceMessageBrokerFactory;
1313
export 'package:angular2/src/web_workers/shared/serializer.dart' show PRIMITIVE;
1414
export 'package:angular2/src/web_workers/shared/message_bus.dart';
15+
export 'package:angular2/src/web_workers/worker/router_providers.dart'
16+
show WORKER_APP_ROUTER;

modules/angular2/platform/worker_app.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ export {
88
ClientMessageBrokerFactory,
99
FnArg,
1010
UiArguments
11-
} from '../src/web_workers/shared/client_message_broker';
11+
} from 'angular2/src/web_workers/shared/client_message_broker';
1212
export {
1313
ReceivedMessage,
1414
ServiceMessageBroker,
1515
ServiceMessageBrokerFactory
16-
} from '../src/web_workers/shared/service_message_broker';
17-
export {PRIMITIVE} from '../src/web_workers/shared/serializer';
18-
export * from '../src/web_workers/shared/message_bus';
16+
} from 'angular2/src/web_workers/shared/service_message_broker';
17+
export {PRIMITIVE} from 'angular2/src/web_workers/shared/serializer';
18+
export * from 'angular2/src/web_workers/shared/message_bus';
1919
export {AngularEntrypoint} from 'angular2/src/core/angular_entrypoint';
20+
export {WORKER_APP_ROUTER} from 'angular2/src/web_workers/worker/router_providers';

modules/angular2/platform/worker_render.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export '../src/web_workers/shared/service_message_broker.dart'
1818

1919
export '../src/web_workers/shared/serializer.dart' show PRIMITIVE;
2020
export '../src/web_workers/shared/message_bus.dart';
21+
export '../src/web_workers/ui/router_providers.dart' show WORKER_RENDER_ROUTER;
2122

2223
import 'package:angular2/src/platform/worker_render_common.dart';
2324

2425
const WORKER_RENDER_APP = WORKER_RENDER_APPLICATION_COMMON;
26+

modules/angular2/platform/worker_render.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ import {WORKER_RENDER_APPLICATION} from 'angular2/src/platform/worker_render';
2424
* @deprecated Use WORKER_RENDER_APPLICATION
2525
*/
2626
export const WORKER_RENDER_APP = WORKER_RENDER_APPLICATION;
27+
export {WORKER_RENDER_ROUTER} from 'angular2/src/web_workers/ui/router_providers';

modules/angular2/router.ts

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,12 @@ export {OnActivate, OnDeactivate, OnReuse, CanDeactivate, CanReuse} from './src/
2020
export {CanActivate} from './src/router/lifecycle_annotations';
2121
export {Instruction, ComponentInstruction} from './src/router/instruction';
2222
export {OpaqueToken} from 'angular2/core';
23+
export {ROUTER_PROVIDERS_COMMON} from 'angular2/src/router/router_providers_common';
24+
export {ROUTER_PROVIDERS, ROUTER_BINDINGS} from 'angular2/src/router/router_providers';
2325

24-
import {PlatformLocation} from './src/router/platform_location';
25-
import {LocationStrategy} from './src/router/location_strategy';
26-
import {PathLocationStrategy} from './src/router/path_location_strategy';
27-
import {Router, RootRouter} from './src/router/router';
2826
import {RouterOutlet} from './src/router/router_outlet';
2927
import {RouterLink} from './src/router/router_link';
30-
import {RouteRegistry, ROUTER_PRIMARY_COMPONENT} from './src/router/route_registry';
31-
import {Location} from './src/router/location';
32-
import {ApplicationRef, provide, OpaqueToken, Provider} from 'angular2/core';
3328
import {CONST_EXPR} from './src/facade/lang';
34-
import {BaseException} from 'angular2/src/facade/exceptions';
3529

3630
/**
3731
* A list of directives. To use the router directives like {@link RouterOutlet} and
@@ -56,63 +50,3 @@ import {BaseException} from 'angular2/src/facade/exceptions';
5650
* ```
5751
*/
5852
export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);
59-
60-
/**
61-
* A list of {@link Provider}s. To use the router, you must add this to your application.
62-
*
63-
* ### Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
64-
*
65-
* ```
66-
* import {Component} from 'angular2/core';
67-
* import {
68-
* ROUTER_DIRECTIVES,
69-
* ROUTER_PROVIDERS,
70-
* RouteConfig
71-
* } from 'angular2/router';
72-
*
73-
* @Component({directives: [ROUTER_DIRECTIVES]})
74-
* @RouteConfig([
75-
* {...},
76-
* ])
77-
* class AppCmp {
78-
* // ...
79-
* }
80-
*
81-
* bootstrap(AppCmp, [ROUTER_PROVIDERS]);
82-
* ```
83-
*/
84-
export const ROUTER_PROVIDERS: any[] = CONST_EXPR([
85-
RouteRegistry,
86-
CONST_EXPR(new Provider(LocationStrategy, {useClass: PathLocationStrategy})),
87-
PlatformLocation,
88-
Location,
89-
CONST_EXPR(new Provider(
90-
Router,
91-
{
92-
useFactory: routerFactory,
93-
deps: CONST_EXPR([RouteRegistry, Location, ROUTER_PRIMARY_COMPONENT, ApplicationRef])
94-
})),
95-
CONST_EXPR(new Provider(
96-
ROUTER_PRIMARY_COMPONENT,
97-
{useFactory: routerPrimaryComponentFactory, deps: CONST_EXPR([ApplicationRef])}))
98-
]);
99-
100-
/**
101-
* Use {@link ROUTER_PROVIDERS} instead.
102-
*
103-
* @deprecated
104-
*/
105-
export const ROUTER_BINDINGS = ROUTER_PROVIDERS;
106-
107-
function routerFactory(registry, location, primaryComponent, appRef) {
108-
var rootRouter = new RootRouter(registry, location, primaryComponent);
109-
appRef.registerDisposeListener(() => rootRouter.dispose());
110-
return rootRouter;
111-
}
112-
113-
function routerPrimaryComponentFactory(app) {
114-
if (app.componentTypes.length == 0) {
115-
throw new BaseException("Bootstrap at least one component before injecting Router.");
116-
}
117-
return app.componentTypes[0];
118-
}

modules/angular2/src/platform/worker_render_common.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {BrowserDomAdapter} from './browser/browser_adapter';
3636
import {wtfInit} from 'angular2/src/core/profile/wtf_init';
3737
import {MessageBasedRenderer} from 'angular2/src/web_workers/ui/renderer';
3838
import {MessageBasedXHRImpl} from 'angular2/src/web_workers/ui/xhr_impl';
39+
import {BrowserPlatformLocation} from 'angular2/src/router/browser_platform_location';
3940
import {
4041
ServiceMessageBrokerFactory,
4142
ServiceMessageBrokerFactory_
@@ -59,6 +60,13 @@ export const WORKER_RENDER_PLATFORM: Array<any /*Type | Provider | any[]*/> = CO
5960
new Provider(PLATFORM_INITIALIZER, {useValue: initWebWorkerRenderPlatform, multi: true})
6061
]);
6162

63+
/**
64+
* A list of {@link Provider}s. To use the router in a Worker enabled application you must
65+
* include these providers when setting up the render thread.
66+
*/
67+
export const WORKER_RENDER_ROUTER: Array<any /*Type | Provider | any[]*/> =
68+
CONST_EXPR([BrowserPlatformLocation]);
69+
6270
export const WORKER_RENDER_APPLICATION_COMMON: Array<any /*Type | Provider | any[]*/> = CONST_EXPR([
6371
APPLICATION_COMMON_PROVIDERS,
6472
WORKER_RENDER_MESSAGING_PROVIDERS,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {Injectable} from 'angular2/core';
2+
import {History, Location} from 'angular2/src/facade/browser';
3+
import {UrlChangeListener} from './platform_location';
4+
import {PlatformLocation} from './platform_location';
5+
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
6+
7+
/**
8+
* `PlatformLocation` encapsulates all of the direct calls to platform APIs.
9+
* This class should not be used directly by an application developer. Instead, use
10+
* {@link Location}.
11+
*/
12+
@Injectable()
13+
export class BrowserPlatformLocation extends PlatformLocation {
14+
private _location: Location;
15+
private _history: History;
16+
17+
constructor() {
18+
super();
19+
this._init();
20+
}
21+
22+
// This is moved to its own method so that `MockPlatformLocationStrategy` can overwrite it
23+
/** @internal */
24+
_init() {
25+
this._location = DOM.getLocation();
26+
this._history = DOM.getHistory();
27+
}
28+
29+
/** @internal */
30+
get location(): Location { return this._location; }
31+
32+
getBaseHrefFromDOM(): string { return DOM.getBaseHref(); }
33+
34+
onPopState(fn: UrlChangeListener): void {
35+
DOM.getGlobalEventTarget('window').addEventListener('popstate', fn, false);
36+
}
37+
38+
onHashChange(fn: UrlChangeListener): void {
39+
DOM.getGlobalEventTarget('window').addEventListener('hashchange', fn, false);
40+
}
41+
42+
get pathname(): string { return this._location.pathname; }
43+
get search(): string { return this._location.search; }
44+
get hash(): string { return this._location.hash; }
45+
set pathname(newPath: string) { this._location.pathname = newPath; }
46+
47+
pushState(state: any, title: string, url: string): void {
48+
this._history.pushState(state, title, url);
49+
}
50+
51+
replaceState(state: any, title: string, url: string): void {
52+
this._history.replaceState(state, title, url);
53+
}
54+
55+
forward(): void { this._history.forward(); }
56+
57+
back(): void { this._history.back(); }
58+
}

modules/angular2/src/router/hash_location_strategy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
APP_BASE_HREF,
66
normalizeQueryParams
77
} from './location_strategy';
8-
import {EventListener} from 'angular2/src/facade/browser';
8+
import {UrlChangeListener} from './platform_location';
99
import {isPresent} from 'angular2/src/facade/lang';
1010
import {PlatformLocation} from './platform_location';
1111

@@ -58,7 +58,7 @@ export class HashLocationStrategy extends LocationStrategy {
5858
}
5959
}
6060

61-
onPopState(fn: EventListener): void {
61+
onPopState(fn: UrlChangeListener): void {
6262
this._platformLocation.onPopState(fn);
6363
this._platformLocation.onHashChange(fn);
6464
}

modules/angular2/src/router/location_strategy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {CONST_EXPR} from 'angular2/src/facade/lang';
22
import {OpaqueToken} from 'angular2/core';
3+
import {UrlChangeListener} from './platform_location';
34

45
/**
56
* `LocationStrategy` is responsible for representing and reading route state
@@ -24,7 +25,7 @@ export abstract class LocationStrategy {
2425
abstract replaceState(state: any, title: string, url: string, queryParams: string): void;
2526
abstract forward(): void;
2627
abstract back(): void;
27-
abstract onPopState(fn: (_: any) => any): void;
28+
abstract onPopState(fn: UrlChangeListener): void;
2829
abstract getBaseHref(): string;
2930
}
3031

modules/angular2/src/router/path_location_strategy.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Injectable, Inject, Optional} from 'angular2/core';
2-
import {EventListener, History, Location} from 'angular2/src/facade/browser';
32
import {isBlank} from 'angular2/src/facade/lang';
43
import {BaseException} from 'angular2/src/facade/exceptions';
54
import {
@@ -8,7 +7,7 @@ import {
87
normalizeQueryParams,
98
joinWithSlash
109
} from './location_strategy';
11-
import {PlatformLocation} from './platform_location';
10+
import {PlatformLocation, UrlChangeListener} from './platform_location';
1211

1312
/**
1413
* `PathLocationStrategy` is a {@link LocationStrategy} used to configure the
@@ -75,7 +74,7 @@ export class PathLocationStrategy extends LocationStrategy {
7574
this._baseHref = href;
7675
}
7776

78-
onPopState(fn: EventListener): void {
77+
onPopState(fn: UrlChangeListener): void {
7978
this._platformLocation.onPopState(fn);
8079
this._platformLocation.onHashChange(fn);
8180
}

0 commit comments

Comments
 (0)