Files
angular-docs-cn/modules/@angular/platform-webworker/src/worker_app.ts
T

81 lines
2.9 KiB
TypeScript
Raw Normal View History

/**
* @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
*/
import {CommonModule, ɵPLATFORM_WORKER_APP_ID as PLATFORM_WORKER_APP_ID} from '@angular/common';
import {APP_INITIALIZER, ApplicationModule, ErrorHandler, NgModule, NgZone, PLATFORM_ID, PlatformRef, Provider, RendererFactoryV2, RootRenderer, createPlatformFactory, platformCore} from '@angular/core';
import {DOCUMENT, ɵBROWSER_SANITIZATION_PROVIDERS as BROWSER_SANITIZATION_PROVIDERS} from '@angular/platform-browser';
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';
import {WebWorkerRendererFactoryV2} from './web_workers/worker/renderer';
2016-06-08 16:38:52 -07:00
import {WorkerDomAdapter} from './web_workers/worker/worker_adapter';
2016-05-20 16:11:49 -07:00
/**
* @experimental
*/
export const platformWorkerApp = createPlatformFactory(
platformCore, 'workerApp', [{provide: PLATFORM_ID, useValue: PLATFORM_WORKER_APP_ID}]);
export function errorHandler(): ErrorHandler {
return new ErrorHandler();
2016-05-20 16:11:49 -07:00
}
2016-05-20 16:11:49 -07:00
// TODO(jteplitz602) remove this and compile with lib.webworker.d.ts (#3492)
const _postMessage = {
2016-06-08 16:38:52 -07:00
postMessage: (message: any, transferrables?: [ArrayBuffer]) => {
2016-05-20 16:11:49 -07:00
(<any>postMessage)(message, transferrables);
}
};
export function createMessageBus(zone: NgZone): MessageBus {
const sink = new PostMessageBusSink(_postMessage);
const source = new PostMessageBusSource();
const bus = new PostMessageBus(sink, source);
2016-05-20 16:11:49 -07:00
bus.attachToZone(zone);
return bus;
}
export function setupWebWorker(): void {
2016-05-20 16:11:49 -07:00
WorkerDomAdapter.makeCurrent();
}
/**
2016-07-18 03:50:31 -07:00
* The ng module for the worker app side.
*
* @experimental
*/
2016-07-18 03:50:31 -07:00
@NgModule({
providers: [
2017-02-16 14:09:06 -08:00
BROWSER_SANITIZATION_PROVIDERS,
Serializer,
{provide: DOCUMENT, useValue: null},
2016-07-18 03:50:31 -07:00
{provide: ClientMessageBrokerFactory, useClass: ClientMessageBrokerFactory_},
{provide: ServiceMessageBrokerFactory, useClass: ServiceMessageBrokerFactory_},
WebWorkerRendererFactoryV2,
{provide: RendererFactoryV2, useExisting: WebWorkerRendererFactoryV2},
2017-02-16 14:09:06 -08:00
{provide: ON_WEB_WORKER, useValue: true},
RenderStore,
{provide: ErrorHandler, useFactory: errorHandler, deps: []},
2016-07-18 03:50:31 -07:00
{provide: MessageBus, useFactory: createMessageBus, deps: [NgZone]},
2017-02-16 14:09:06 -08:00
{provide: APP_INITIALIZER, useValue: setupWebWorker, multi: true},
2016-07-18 03:50:31 -07:00
],
2017-02-16 14:09:06 -08:00
exports: [
CommonModule,
ApplicationModule,
]
})
export class WorkerAppModule {
2016-07-18 03:50:31 -07:00
}