2016-06-23 09:47:54 -07: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
|
|
|
|
*/
|
|
|
|
|
2017-02-22 16:49:46 -08:00
|
|
|
import {CommonModule, ɵPLATFORM_WORKER_UI_ID as PLATFORM_WORKER_UI_ID} from '@angular/common';
|
2017-03-07 16:36:12 -08:00
|
|
|
import {ErrorHandler, Injectable, InjectionToken, Injector, NgZone, PLATFORM_ID, PLATFORM_INITIALIZER, PlatformRef, Provider, RendererFactory2, RootRenderer, Testability, createPlatformFactory, isDevMode, platformCore, ɵAPP_ID_RANDOM_PROVIDER as APP_ID_RANDOM_PROVIDER} from '@angular/core';
|
|
|
|
import {DOCUMENT, EVENT_MANAGER_PLUGINS, EventManager, HAMMER_GESTURE_CONFIG, HammerGestureConfig, ɵBROWSER_SANITIZATION_PROVIDERS as BROWSER_SANITIZATION_PROVIDERS, ɵBrowserDomAdapter as BrowserDomAdapter, ɵBrowserGetTestability as BrowserGetTestability, ɵDomEventsPlugin as DomEventsPlugin, ɵDomRendererFactory2 as DomRendererFactory2, ɵDomSharedStylesHost as DomSharedStylesHost, ɵHammerGesturesPlugin as HammerGesturesPlugin, ɵKeyEventsPlugin as KeyEventsPlugin, ɵSharedStylesHost as SharedStylesHost, ɵgetDOM as getDOM} from '@angular/platform-browser';
|
2017-02-18 21:28:27 -08:00
|
|
|
|
2016-06-08 16:38:52 -07: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';
|
2017-03-07 16:36:12 -08:00
|
|
|
import {MessageBasedRenderer2} from './web_workers/ui/renderer';
|
2016-05-20 16:11:49 -07:00
|
|
|
|
2017-02-18 21:28:27 -08:00
|
|
|
|
2016-05-20 16:11:49 -07:00
|
|
|
/**
|
|
|
|
* Wrapper class that exposes the Worker
|
|
|
|
* and underlying {@link MessageBus} for lower level message passing.
|
2016-06-27 12:27:23 -07:00
|
|
|
*
|
|
|
|
* @experimental WebWorker support is currently experimental.
|
2016-05-20 16:11:49 -07: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 08:41:41 -07:00
|
|
|
/**
|
2016-06-27 12:27:23 -07:00
|
|
|
* @experimental WebWorker support is currently experimental.
|
2016-06-15 08:41:41 -07:00
|
|
|
*/
|
2017-01-03 16:54:46 -08:00
|
|
|
export const WORKER_SCRIPT = new InjectionToken<string>('WebWorkerScript');
|
2016-05-20 16:11:49 -07:00
|
|
|
|
|
|
|
/**
|
2016-08-30 11:15:25 -07:00
|
|
|
* A multi-provider used to automatically call the `start()` method after the service is
|
2016-05-20 16:11:49 -07:00
|
|
|
* created.
|
|
|
|
*
|
2016-06-27 12:27:23 -07:00
|
|
|
* @experimental WebWorker support is currently experimental.
|
2016-05-20 16:11:49 -07:00
|
|
|
*/
|
2016-06-15 08:25:31 -07:00
|
|
|
export const WORKER_UI_STARTABLE_MESSAGING_SERVICE =
|
2017-02-20 08:00:31 -08:00
|
|
|
new InjectionToken<({start: () => void})[]>('WorkerRenderStartableMsgService');
|
2016-05-20 16:11:49 -07:00
|
|
|
|
2016-08-15 19:37:42 -07:00
|
|
|
export const _WORKER_UI_PLATFORM_PROVIDERS: Provider[] = [
|
2016-07-18 03:50:31 -07:00
|
|
|
{provide: NgZone, useFactory: createNgZone, deps: []},
|
2017-03-07 16:36:12 -08:00
|
|
|
MessageBasedRenderer2,
|
|
|
|
{provide: WORKER_UI_STARTABLE_MESSAGING_SERVICE, useExisting: MessageBasedRenderer2, multi: true},
|
2016-06-08 16:38:52 -07:00
|
|
|
BROWSER_SANITIZATION_PROVIDERS,
|
2016-08-25 00:50:16 -07:00
|
|
|
{provide: ErrorHandler, useFactory: _exceptionHandler, deps: []},
|
2016-06-08 16:38:52 -07: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 11:38:10 -07:00
|
|
|
APP_ID_RANDOM_PROVIDER,
|
2017-03-07 16:36:12 -08:00
|
|
|
DomRendererFactory2,
|
|
|
|
{provide: RendererFactory2, useExisting: DomRendererFactory2},
|
2016-06-08 16:38:52 -07:00
|
|
|
{provide: SharedStylesHost, useExisting: DomSharedStylesHost},
|
|
|
|
{provide: ServiceMessageBrokerFactory, useClass: ServiceMessageBrokerFactory_},
|
|
|
|
{provide: ClientMessageBrokerFactory, useClass: ClientMessageBrokerFactory_},
|
|
|
|
Serializer,
|
|
|
|
{provide: ON_WEB_WORKER, useValue: false},
|
|
|
|
RenderStore,
|
|
|
|
DomSharedStylesHost,
|
|
|
|
Testability,
|
|
|
|
EventManager,
|
|
|
|
WebWorkerInstance,
|
2016-07-18 03:50:31 -07:00
|
|
|
{
|
|
|
|
provide: PLATFORM_INITIALIZER,
|
|
|
|
useFactory: initWebWorkerRenderPlatform,
|
|
|
|
multi: true,
|
|
|
|
deps: [Injector]
|
|
|
|
},
|
2017-02-22 16:49:46 -08:00
|
|
|
{provide: PLATFORM_ID, useValue: PLATFORM_WORKER_UI_ID},
|
2017-02-16 14:09:06 -08:00
|
|
|
{provide: MessageBus, useFactory: messageBusFactory, deps: [WebWorkerInstance]},
|
2016-06-08 16:38:52 -07:00
|
|
|
];
|
2016-05-20 16:11:49 -07:00
|
|
|
|
2016-06-15 08:25:31 -07:00
|
|
|
function initializeGenericWorkerRenderer(injector: Injector) {
|
2016-11-12 14:08:58 +01:00
|
|
|
const bus = injector.get(MessageBus);
|
|
|
|
const zone = injector.get(NgZone);
|
2016-05-20 16:11:49 -07:00
|
|
|
bus.attachToZone(zone);
|
|
|
|
|
|
|
|
// initialize message services after the bus has been created
|
2016-11-12 14:08:58 +01:00
|
|
|
const services = injector.get(WORKER_UI_STARTABLE_MESSAGING_SERVICE);
|
2016-06-28 11:35:59 -07:00
|
|
|
zone.runGuarded(() => { services.forEach((svc: any) => { svc.start(); }); });
|
2016-05-20 16:11:49 -07:00
|
|
|
}
|
|
|
|
|
2016-06-02 17:30:40 -07:00
|
|
|
function messageBusFactory(instance: WebWorkerInstance): MessageBus {
|
|
|
|
return instance.bus;
|
|
|
|
}
|
|
|
|
|
2016-07-18 03:50:31 -07:00
|
|
|
function initWebWorkerRenderPlatform(injector: Injector): () => void {
|
2016-06-02 17:30:40 -07:00
|
|
|
return () => {
|
2016-07-18 03:50:31 -07:00
|
|
|
BrowserDomAdapter.makeCurrent();
|
|
|
|
BrowserGetTestability.init();
|
2016-11-12 14:08:58 +01:00
|
|
|
let scriptUri: string;
|
2016-06-02 17:30:40 -07:00
|
|
|
try {
|
|
|
|
scriptUri = injector.get(WORKER_SCRIPT);
|
|
|
|
} catch (e) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new Error(
|
2016-06-08 16:38:52 -07:00
|
|
|
'You must provide your WebWorker\'s initialization script with the WORKER_SCRIPT token');
|
2016-06-02 17:30:40 -07:00
|
|
|
}
|
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
const instance = injector.get(WebWorkerInstance);
|
2016-06-02 17:30:40 -07:00
|
|
|
spawnWebWorker(scriptUri, instance);
|
|
|
|
|
|
|
|
initializeGenericWorkerRenderer(injector);
|
|
|
|
};
|
2016-05-20 16:11:49 -07:00
|
|
|
}
|
|
|
|
|
2016-07-18 03:50:31 -07:00
|
|
|
/**
|
|
|
|
* @experimental WebWorker support is currently experimental.
|
|
|
|
*/
|
2016-07-26 05:21:19 -07:00
|
|
|
export const platformWorkerUi =
|
|
|
|
createPlatformFactory(platformCore, 'workerUi', _WORKER_UI_PLATFORM_PROVIDERS);
|
|
|
|
|
2016-08-25 00:50:16 -07:00
|
|
|
function _exceptionHandler(): ErrorHandler {
|
|
|
|
return new ErrorHandler();
|
2016-07-18 03:50:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function _document(): any {
|
2017-02-14 16:14:40 -08:00
|
|
|
return document;
|
2016-07-18 03:50:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function createNgZone(): NgZone {
|
|
|
|
return new NgZone({enableLongStackTrace: isDevMode()});
|
|
|
|
}
|
|
|
|
|
2016-05-20 16:11:49 -07:00
|
|
|
/**
|
|
|
|
* Spawns a new class and initializes the WebWorkerInstance
|
|
|
|
*/
|
|
|
|
function spawnWebWorker(uri: string, instance: WebWorkerInstance): void {
|
2016-11-12 14:08:58 +01: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 16:11:49 -07:00
|
|
|
|
|
|
|
instance.init(webWorker, bus);
|
|
|
|
}
|