2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-09-27 20:12:25 -04:00
|
|
|
import {ErrorHandler, Injectable, Injector, NgZone, OpaqueToken, PLATFORM_INITIALIZER, PlatformRef, Provider, RootRenderer, Testability, createPlatformFactory, isDevMode, platformCore} from '@angular/core';
|
2016-08-30 14:15:25 -04:00
|
|
|
import {AnimationDriver, DOCUMENT, EVENT_MANAGER_PLUGINS, EventManager, HAMMER_GESTURE_CONFIG, HammerGestureConfig} from '@angular/platform-browser';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-11-02 14:38:10 -04:00
|
|
|
import {APP_ID_RANDOM_PROVIDER} from './private_import_core';
|
2016-11-10 14:53:50 -05:00
|
|
|
import {BROWSER_SANITIZATION_PROVIDERS, BrowserDomAdapter, BrowserGetTestability, DomEventsPlugin, DomRootRenderer, DomRootRenderer_, DomSharedStylesHost, HammerGesturesPlugin, KeyEventsPlugin, SharedStylesHost, WebAnimationsDriver, getDOM} from './private_import_platform-browser';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {ON_WEB_WORKER} from './web_workers/shared/api';
|
|
|
|
import {ClientMessageBrokerFactory, ClientMessageBrokerFactory_} from './web_workers/shared/client_message_broker';
|
|
|
|
import {MessageBus} from './web_workers/shared/message_bus';
|
|
|
|
import {PostMessageBus, PostMessageBusSink, PostMessageBusSource} from './web_workers/shared/post_message_bus';
|
|
|
|
import {RenderStore} from './web_workers/shared/render_store';
|
|
|
|
import {Serializer} from './web_workers/shared/serializer';
|
|
|
|
import {ServiceMessageBrokerFactory, ServiceMessageBrokerFactory_} from './web_workers/shared/service_message_broker';
|
|
|
|
import {MessageBasedRenderer} from './web_workers/ui/renderer';
|
2016-05-20 19:11:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper class that exposes the Worker
|
|
|
|
* and underlying {@link MessageBus} for lower level message passing.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental WebWorker support is currently experimental.
|
2016-05-20 19:11:49 -04:00
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class WebWorkerInstance {
|
|
|
|
public worker: Worker;
|
|
|
|
public bus: MessageBus;
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
public init(worker: Worker, bus: MessageBus) {
|
|
|
|
this.worker = worker;
|
|
|
|
this.bus = bus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 11:41:41 -04:00
|
|
|
/**
|
2016-06-27 15:27:23 -04:00
|
|
|
* @experimental WebWorker support is currently experimental.
|
2016-06-15 11:41:41 -04:00
|
|
|
*/
|
2016-06-08 19:38:52 -04:00
|
|
|
export const WORKER_SCRIPT: OpaqueToken = new OpaqueToken('WebWorkerScript');
|
2016-05-20 19:11:49 -04:00
|
|
|
|
|
|
|
/**
|
2016-08-30 14:15:25 -04:00
|
|
|
* A multi-provider used to automatically call the `start()` method after the service is
|
2016-05-20 19:11:49 -04:00
|
|
|
* created.
|
|
|
|
*
|
|
|
|
* TODO(vicb): create an interface for startable services to implement
|
2016-06-27 15:27:23 -04:00
|
|
|
* @experimental WebWorker support is currently experimental.
|
2016-05-20 19:11:49 -04:00
|
|
|
*/
|
2016-06-15 11:25:31 -04:00
|
|
|
export const WORKER_UI_STARTABLE_MESSAGING_SERVICE =
|
2016-06-08 19:38:52 -04:00
|
|
|
new OpaqueToken('WorkerRenderStartableMsgService');
|
2016-05-20 19:11:49 -04:00
|
|
|
|
2016-08-15 22:37:42 -04:00
|
|
|
export const _WORKER_UI_PLATFORM_PROVIDERS: Provider[] = [
|
2016-07-18 06:50:31 -04:00
|
|
|
{provide: NgZone, useFactory: createNgZone, deps: []},
|
2016-06-08 19:38:52 -04:00
|
|
|
MessageBasedRenderer,
|
2016-06-15 11:25:31 -04:00
|
|
|
{provide: WORKER_UI_STARTABLE_MESSAGING_SERVICE, useExisting: MessageBasedRenderer, multi: true},
|
2016-06-08 19:38:52 -04:00
|
|
|
BROWSER_SANITIZATION_PROVIDERS,
|
2016-08-25 03:50:16 -04:00
|
|
|
{provide: ErrorHandler, useFactory: _exceptionHandler, deps: []},
|
2016-06-08 19:38:52 -04:00
|
|
|
{provide: DOCUMENT, useFactory: _document, deps: []},
|
|
|
|
// TODO(jteplitz602): Investigate if we definitely need EVENT_MANAGER on the render thread
|
|
|
|
// #5298
|
|
|
|
{provide: EVENT_MANAGER_PLUGINS, useClass: DomEventsPlugin, multi: true},
|
|
|
|
{provide: EVENT_MANAGER_PLUGINS, useClass: KeyEventsPlugin, multi: true},
|
|
|
|
{provide: EVENT_MANAGER_PLUGINS, useClass: HammerGesturesPlugin, multi: true},
|
|
|
|
{provide: HAMMER_GESTURE_CONFIG, useClass: HammerGestureConfig},
|
2016-11-02 14:38:10 -04:00
|
|
|
APP_ID_RANDOM_PROVIDER,
|
2016-06-08 19:38:52 -04:00
|
|
|
{provide: DomRootRenderer, useClass: DomRootRenderer_},
|
|
|
|
{provide: RootRenderer, useExisting: DomRootRenderer},
|
|
|
|
{provide: SharedStylesHost, useExisting: DomSharedStylesHost},
|
|
|
|
{provide: ServiceMessageBrokerFactory, useClass: ServiceMessageBrokerFactory_},
|
|
|
|
{provide: ClientMessageBrokerFactory, useClass: ClientMessageBrokerFactory_},
|
2016-08-15 22:37:42 -04:00
|
|
|
{provide: AnimationDriver, useFactory: _resolveDefaultAnimationDriver, deps: []},
|
2016-06-08 19:38:52 -04:00
|
|
|
Serializer,
|
|
|
|
{provide: ON_WEB_WORKER, useValue: false},
|
|
|
|
RenderStore,
|
|
|
|
DomSharedStylesHost,
|
|
|
|
Testability,
|
|
|
|
EventManager,
|
|
|
|
WebWorkerInstance,
|
2016-07-18 06:50:31 -04:00
|
|
|
{
|
|
|
|
provide: PLATFORM_INITIALIZER,
|
|
|
|
useFactory: initWebWorkerRenderPlatform,
|
|
|
|
multi: true,
|
|
|
|
deps: [Injector]
|
|
|
|
},
|
2016-06-08 19:38:52 -04:00
|
|
|
{provide: MessageBus, useFactory: messageBusFactory, deps: [WebWorkerInstance]}
|
|
|
|
];
|
2016-05-20 19:11:49 -04:00
|
|
|
|
2016-06-15 11:25:31 -04:00
|
|
|
function initializeGenericWorkerRenderer(injector: Injector) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const bus = injector.get(MessageBus);
|
|
|
|
const zone = injector.get(NgZone);
|
2016-05-20 19:11:49 -04:00
|
|
|
bus.attachToZone(zone);
|
|
|
|
|
|
|
|
// initialize message services after the bus has been created
|
2016-11-12 08:08:58 -05:00
|
|
|
const services = injector.get(WORKER_UI_STARTABLE_MESSAGING_SERVICE);
|
2016-06-28 14:35:59 -04:00
|
|
|
zone.runGuarded(() => { services.forEach((svc: any) => { svc.start(); }); });
|
2016-05-20 19:11:49 -04:00
|
|
|
}
|
|
|
|
|
2016-06-02 20:30:40 -04:00
|
|
|
function messageBusFactory(instance: WebWorkerInstance): MessageBus {
|
|
|
|
return instance.bus;
|
|
|
|
}
|
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
function initWebWorkerRenderPlatform(injector: Injector): () => void {
|
2016-06-02 20:30:40 -04:00
|
|
|
return () => {
|
2016-07-18 06:50:31 -04:00
|
|
|
BrowserDomAdapter.makeCurrent();
|
|
|
|
BrowserGetTestability.init();
|
2016-11-12 08:08:58 -05:00
|
|
|
let scriptUri: string;
|
2016-06-02 20:30:40 -04:00
|
|
|
try {
|
|
|
|
scriptUri = injector.get(WORKER_SCRIPT);
|
|
|
|
} catch (e) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(
|
2016-06-08 19:38:52 -04:00
|
|
|
'You must provide your WebWorker\'s initialization script with the WORKER_SCRIPT token');
|
2016-06-02 20:30:40 -04:00
|
|
|
}
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const instance = injector.get(WebWorkerInstance);
|
2016-06-02 20:30:40 -04:00
|
|
|
spawnWebWorker(scriptUri, instance);
|
|
|
|
|
|
|
|
initializeGenericWorkerRenderer(injector);
|
|
|
|
};
|
2016-05-20 19:11:49 -04:00
|
|
|
}
|
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
/**
|
|
|
|
* @experimental WebWorker support is currently experimental.
|
|
|
|
*/
|
2016-07-26 08:21:19 -04:00
|
|
|
export const platformWorkerUi =
|
|
|
|
createPlatformFactory(platformCore, 'workerUi', _WORKER_UI_PLATFORM_PROVIDERS);
|
|
|
|
|
2016-08-25 03:50:16 -04:00
|
|
|
function _exceptionHandler(): ErrorHandler {
|
|
|
|
return new ErrorHandler();
|
2016-07-18 06:50:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function _document(): any {
|
|
|
|
return getDOM().defaultDoc();
|
|
|
|
}
|
|
|
|
|
|
|
|
function createNgZone(): NgZone {
|
|
|
|
return new NgZone({enableLongStackTrace: isDevMode()});
|
|
|
|
}
|
|
|
|
|
2016-05-20 19:11:49 -04:00
|
|
|
/**
|
|
|
|
* Spawns a new class and initializes the WebWorkerInstance
|
|
|
|
*/
|
|
|
|
function spawnWebWorker(uri: string, instance: WebWorkerInstance): void {
|
2016-11-12 08:08:58 -05:00
|
|
|
const webWorker: Worker = new Worker(uri);
|
|
|
|
const sink = new PostMessageBusSink(webWorker);
|
|
|
|
const source = new PostMessageBusSource(webWorker);
|
|
|
|
const bus = new PostMessageBus(sink, source);
|
2016-05-20 19:11:49 -04:00
|
|
|
|
|
|
|
instance.init(webWorker, bus);
|
|
|
|
}
|
2016-05-25 15:46:22 -04:00
|
|
|
|
|
|
|
function _resolveDefaultAnimationDriver(): AnimationDriver {
|
2016-11-10 14:53:50 -05:00
|
|
|
if (getDOM().supportsWebAnimation()) {
|
|
|
|
return new WebAnimationsDriver();
|
|
|
|
}
|
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
|
|
|
return AnimationDriver.NOOP;
|
2016-05-25 15:46:22 -04:00
|
|
|
}
|