angular-docs-cn/packages/platform-webworker/src/worker_render.ts

194 lines
7.0 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google LLC 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 {DOCUMENT, ɵPLATFORM_WORKER_UI_ID as PLATFORM_WORKER_UI_ID} from '@angular/common';
import {createPlatformFactory, ErrorHandler, Injectable, InjectionToken, Injector, isDevMode, NgZone, PLATFORM_ID, PLATFORM_INITIALIZER, platformCore, RendererFactory2, StaticProvider, Testability, ɵAPP_ID_RANDOM_PROVIDER as APP_ID_RANDOM_PROVIDER, ɵsetDocument} from '@angular/core';
import {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} from '@angular/platform-browser';
import {ON_WEB_WORKER} from './web_workers/shared/api';
import {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} from './web_workers/shared/service_message_broker';
import {MessageBasedRenderer2} from './web_workers/ui/renderer';
2016-05-20 19:11:49 -04:00
2016-05-20 19:11:49 -04:00
/**
* Wrapper class that exposes the Worker
* and underlying {@link MessageBus} for lower level message passing.
*
* @publicApi
* @deprecated platform-webworker is deprecated in Angular and will be removed in a future version
* of Angular
2016-05-20 19:11:49 -04:00
*/
@Injectable()
export class WebWorkerInstance {
// TODO(issue/24571): remove '!'.
public worker!: Worker;
// TODO(issue/24571): remove '!'.
public bus!: MessageBus;
2016-05-20 19:11:49 -04:00
/** @internal */
public init(worker: Worker, bus: MessageBus) {
this.worker = worker;
this.bus = bus;
}
}
/**
* @publicApi
* @deprecated platform-webworker is deprecated in Angular and will be removed in a future version
* of Angular
*/
feat(core): Add type information to injector.get() (#13785) - Introduce `InjectionToken<T>` which is a parameterized and type-safe version of `OpaqueToken`. DEPRECATION: - `OpaqueToken` is now deprecated, use `InjectionToken<T>` instead. - `Injector.get(token: any, notFoundValue?: any): any` is now deprecated use the same method which is now overloaded as `Injector.get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T): T;`. Migration - Replace `OpaqueToken` with `InjectionToken<?>` and parameterize it. - Migrate your code to only use `Type<?>` or `InjectionToken<?>` as injection tokens. Using other tokens will not be supported in the future. BREAKING CHANGE: - Because `injector.get()` is now parameterize it is possible that code which used to work no longer type checks. Example would be if one injects `Foo` but configures it as `{provide: Foo, useClass: MockFoo}`. The injection instance will be that of `MockFoo` but the type will be `Foo` instead of `any` as in the past. This means that it was possible to call a method on `MockFoo` in the past which now will fail type check. See this example: ``` class Foo {} class MockFoo extends Foo { setupMock(); } var PROVIDERS = [ {provide: Foo, useClass: MockFoo} ]; ... function myTest(injector: Injector) { var foo = injector.get(Foo); // This line used to work since `foo` used to be `any` before this // change, it will now be `Foo`, and `Foo` does not have `setUpMock()`. // The fix is to downcast: `injector.get(Foo) as MockFoo`. foo.setUpMock(); } ``` PR Close #13785
2017-01-03 19:54:46 -05:00
export const WORKER_SCRIPT = new InjectionToken<string>('WebWorkerScript');
2016-05-20 19:11:49 -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.
*
* @publicApi
* @deprecated platform-webworker is deprecated in Angular and will be removed in a future version
* of Angular
2016-05-20 19:11:49 -04:00
*/
export const WORKER_UI_STARTABLE_MESSAGING_SERVICE =
new InjectionToken<({start: () => void})[]>('WorkerRenderStartableMsgService');
2016-05-20 19:11:49 -04:00
export const _WORKER_UI_PLATFORM_PROVIDERS: StaticProvider[] = [
{provide: NgZone, useFactory: createNgZone, deps: []},
{
provide: MessageBasedRenderer2,
deps: [ServiceMessageBrokerFactory, MessageBus, Serializer, RenderStore, RendererFactory2]
},
{provide: WORKER_UI_STARTABLE_MESSAGING_SERVICE, useExisting: MessageBasedRenderer2, multi: true},
BROWSER_SANITIZATION_PROVIDERS,
{provide: ErrorHandler, useFactory: _exceptionHandler, deps: []},
{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,
deps: [DOCUMENT, NgZone],
multi: true
},
{provide: EVENT_MANAGER_PLUGINS, useClass: KeyEventsPlugin, deps: [DOCUMENT], multi: true},
{
provide: EVENT_MANAGER_PLUGINS,
useClass: HammerGesturesPlugin,
deps: [DOCUMENT, HAMMER_GESTURE_CONFIG],
multi: true
},
{provide: HAMMER_GESTURE_CONFIG, useClass: HammerGestureConfig, deps: []},
2016-11-02 14:38:10 -04:00
APP_ID_RANDOM_PROVIDER,
{provide: DomRendererFactory2, deps: [EventManager, DomSharedStylesHost]},
{provide: RendererFactory2, useExisting: DomRendererFactory2},
{provide: SharedStylesHost, useExisting: DomSharedStylesHost},
{
provide: ServiceMessageBrokerFactory,
useClass: ServiceMessageBrokerFactory,
deps: [MessageBus, Serializer]
},
{
provide: ClientMessageBrokerFactory,
useClass: ClientMessageBrokerFactory,
deps: [MessageBus, Serializer]
},
{provide: Serializer, deps: [RenderStore]},
{provide: ON_WEB_WORKER, useValue: false},
{provide: RenderStore, deps: []},
{provide: DomSharedStylesHost, deps: [DOCUMENT]},
{provide: Testability, deps: [NgZone]},
{provide: EventManager, deps: [EVENT_MANAGER_PLUGINS, NgZone]},
{provide: WebWorkerInstance, deps: []},
{
provide: PLATFORM_INITIALIZER,
useFactory: initWebWorkerRenderPlatform,
multi: true,
deps: [Injector]
},
{provide: PLATFORM_ID, useValue: PLATFORM_WORKER_UI_ID},
{provide: MessageBus, useFactory: messageBusFactory, deps: [WebWorkerInstance]},
];
2016-05-20 19:11:49 -04:00
function initializeGenericWorkerRenderer(injector: Injector) {
const bus = injector.get(MessageBus);
const zone = injector.get<NgZone>(NgZone);
2016-05-20 19:11:49 -04:00
bus.attachToZone(zone);
// initialize message services after the bus has been created
const services = injector.get(WORKER_UI_STARTABLE_MESSAGING_SERVICE);
zone.runGuarded(() => {
services.forEach((svc: any) => {
svc.start();
});
});
2016-05-20 19:11:49 -04:00
}
function messageBusFactory(instance: WebWorkerInstance): MessageBus {
return instance.bus;
}
function initWebWorkerRenderPlatform(injector: Injector): () => void {
return () => {
BrowserDomAdapter.makeCurrent();
BrowserGetTestability.init();
let scriptUri: string;
try {
scriptUri = injector.get(WORKER_SCRIPT);
} catch {
throw new Error(
'You must provide your WebWorker\'s initialization script with the WORKER_SCRIPT token');
}
const instance = injector.get(WebWorkerInstance);
spawnWebWorker(scriptUri, instance);
initializeGenericWorkerRenderer(injector);
};
2016-05-20 19:11:49 -04:00
}
/**
* @publicApi
* @deprecated platform-webworker is deprecated in Angular and will be removed in a future version
* of Angular
*/
export const platformWorkerUi =
createPlatformFactory(platformCore, 'workerUi', _WORKER_UI_PLATFORM_PROVIDERS);
function _exceptionHandler(): ErrorHandler {
return new ErrorHandler();
}
function _document(): any {
// Tell ivy about the global document
ɵsetDocument(document);
return document;
}
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 {
const webWorker: Worker = new Worker(uri);
// webWorker is casted to any because the lib.d.ts signature changed in TS3.5 to require the
// transfer argument in postMessage method.
// this seems wrong but since all of this code is deprecated it shouldn't matter that much.
const sink = new PostMessageBusSink(webWorker as any);
const source = new PostMessageBusSource(webWorker);
const bus = new PostMessageBus(sink, source);
2016-05-20 19:11:49 -04:00
instance.init(webWorker, bus);
}