-
Notifications
You must be signed in to change notification settings - Fork 25.3k
/
worker_render.ts
179 lines (158 loc) · 6.55 KB
/
worker_render.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @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 {DOCUMENT, ɵPLATFORM_WORKER_UI_ID as PLATFORM_WORKER_UI_ID} from '@angular/common';
import {ErrorHandler, Injectable, InjectionToken, Injector, NgZone, PLATFORM_ID, PLATFORM_INITIALIZER, PlatformRef, RendererFactory2, StaticProvider, Testability, createPlatformFactory, isDevMode, platformCore, ɵAPP_ID_RANDOM_PROVIDER as APP_ID_RANDOM_PROVIDER} 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';
/**
* 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 version 10
*/
@Injectable()
export class WebWorkerInstance {
// TODO(issue/24571): remove '!'.
public worker !: Worker;
// TODO(issue/24571): remove '!'.
public bus !: MessageBus;
/** @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 version 10
*/
export const WORKER_SCRIPT = new InjectionToken<string>('WebWorkerScript');
/**
* A multi-provider used to automatically call the `start()` method after the service is
* created.
*
* @publicApi
* @deprecated platform-webworker is deprecated in Angular and will be removed in version 10
*/
export const WORKER_UI_STARTABLE_MESSAGING_SERVICE =
new InjectionToken<({start: () => void})[]>('WorkerRenderStartableMsgService');
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: []},
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]},
];
function initializeGenericWorkerRenderer(injector: Injector) {
const bus = injector.get(MessageBus);
const zone = injector.get<NgZone>(NgZone);
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(); }); });
}
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);
};
}
/**
* @publicApi
*/
export const platformWorkerUi =
createPlatformFactory(platformCore, 'workerUi', _WORKER_UI_PLATFORM_PROVIDERS);
function _exceptionHandler(): ErrorHandler {
return new ErrorHandler();
}
function _document(): any {
return document;
}
function createNgZone(): NgZone {
return new NgZone({enableLongStackTrace: isDevMode()});
}
/**
* Spawns a new class and initializes the WebWorkerInstance
*/
function spawnWebWorker(uri: string, instance: WebWorkerInstance): void {
const webWorker: Worker = new Worker(uri);
const sink = new PostMessageBusSink(webWorker);
const source = new PostMessageBusSource(webWorker);
const bus = new PostMessageBus(sink, source);
instance.init(webWorker, bus);
}