mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
Previously these symbols were exposed via platform-browser-dynamic, then we merged then into platform-browser thinking that tools would know how to shake off the compiler and other dynamic bits not used with the offline compilation flow. This turned out to be wrong as both webpack and rollup don't have good enough tree-shaking capabilities to do this today. We think that in the future we'll be able to merge these two entry points into one, but we need to give tooling some time before we can do it. In the meantime the reintroduction of the -dynamic package point allows us to separate the compiler dependencies from the rest of the framework. This change undoes the previous breaking change that removed the platform-browser-dynamic package.
163 lines
6.4 KiB
TypeScript
163 lines
6.4 KiB
TypeScript
import {APPLICATION_COMMON_PROVIDERS, APP_INITIALIZER, ApplicationRef, ExceptionHandler, Injectable, Injector, NgZone, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, RootRenderer, Testability, assertPlatform, createPlatform, getPlatform} from '@angular/core';
|
|
|
|
import {AnimationDriver, NoOpAnimationDriver, wtfInit} from '../core_private';
|
|
|
|
import {BROWSER_SANITIZATION_PROVIDERS} from './browser';
|
|
import {BrowserDomAdapter} from './browser/browser_adapter';
|
|
import {BrowserGetTestability} from './browser/testability';
|
|
import {getDOM} from './dom/dom_adapter';
|
|
import {DomRootRenderer, DomRootRenderer_} from './dom/dom_renderer';
|
|
import {DOCUMENT} from './dom/dom_tokens';
|
|
import {DomEventsPlugin} from './dom/events/dom_events';
|
|
import {EVENT_MANAGER_PLUGINS, EventManager} from './dom/events/event_manager';
|
|
import {HAMMER_GESTURE_CONFIG, HammerGestureConfig, HammerGesturesPlugin} from './dom/events/hammer_gestures';
|
|
import {KeyEventsPlugin} from './dom/events/key_events';
|
|
import {DomSharedStylesHost, SharedStylesHost} from './dom/shared_styles_host';
|
|
import {BaseException} from './facade/exceptions';
|
|
import {isBlank} from './facade/lang';
|
|
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';
|
|
|
|
const WORKER_RENDER_PLATFORM_MARKER = new OpaqueToken('WorkerRenderPlatformMarker');
|
|
|
|
/**
|
|
* Wrapper class that exposes the Worker
|
|
* and underlying {@link MessageBus} for lower level message passing.
|
|
*/
|
|
@Injectable()
|
|
export class WebWorkerInstance {
|
|
public worker: Worker;
|
|
public bus: MessageBus;
|
|
|
|
/** @internal */
|
|
public init(worker: Worker, bus: MessageBus) {
|
|
this.worker = worker;
|
|
this.bus = bus;
|
|
}
|
|
}
|
|
|
|
export const WORKER_SCRIPT: OpaqueToken = new OpaqueToken('WebWorkerScript');
|
|
|
|
/**
|
|
* A multiple providers used to automatically call the `start()` method after the service is
|
|
* created.
|
|
*
|
|
* TODO(vicb): create an interface for startable services to implement
|
|
*/
|
|
export const WORKER_RENDER_STARTABLE_MESSAGING_SERVICE =
|
|
new OpaqueToken('WorkerRenderStartableMsgService');
|
|
|
|
export const WORKER_RENDER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
|
|
PLATFORM_COMMON_PROVIDERS, {provide: WORKER_RENDER_PLATFORM_MARKER, useValue: true},
|
|
{provide: PLATFORM_INITIALIZER, useValue: initWebWorkerRenderPlatform, multi: true}
|
|
];
|
|
|
|
export const WORKER_RENDER_APPLICATION_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
|
|
APPLICATION_COMMON_PROVIDERS,
|
|
MessageBasedRenderer,
|
|
{
|
|
provide: WORKER_RENDER_STARTABLE_MESSAGING_SERVICE,
|
|
useExisting: MessageBasedRenderer,
|
|
multi: true
|
|
},
|
|
BROWSER_SANITIZATION_PROVIDERS,
|
|
{provide: ExceptionHandler, 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, multi: true},
|
|
{provide: EVENT_MANAGER_PLUGINS, useClass: KeyEventsPlugin, multi: true},
|
|
{provide: EVENT_MANAGER_PLUGINS, useClass: HammerGesturesPlugin, multi: true},
|
|
{provide: HAMMER_GESTURE_CONFIG, useClass: HammerGestureConfig},
|
|
{provide: DomRootRenderer, useClass: DomRootRenderer_},
|
|
{provide: RootRenderer, useExisting: DomRootRenderer},
|
|
{provide: SharedStylesHost, useExisting: DomSharedStylesHost},
|
|
{provide: ServiceMessageBrokerFactory, useClass: ServiceMessageBrokerFactory_},
|
|
{provide: ClientMessageBrokerFactory, useClass: ClientMessageBrokerFactory_},
|
|
{provide: AnimationDriver, useFactory: _resolveDefaultAnimationDriver},
|
|
Serializer,
|
|
{provide: ON_WEB_WORKER, useValue: false},
|
|
RenderStore,
|
|
DomSharedStylesHost,
|
|
Testability,
|
|
EventManager,
|
|
WebWorkerInstance,
|
|
{provide: APP_INITIALIZER, useFactory: initWebWorkerAppFn, multi: true, deps: [Injector]},
|
|
{provide: MessageBus, useFactory: messageBusFactory, deps: [WebWorkerInstance]}
|
|
];
|
|
|
|
export function initializeGenericWorkerRenderer(injector: Injector) {
|
|
var bus = injector.get(MessageBus);
|
|
let zone = injector.get(NgZone);
|
|
bus.attachToZone(zone);
|
|
|
|
// initialize message services after the bus has been created
|
|
let services = injector.get(WORKER_RENDER_STARTABLE_MESSAGING_SERVICE);
|
|
zone.runGuarded(() => { services.forEach((svc: any /** TODO #9100 */) => { svc.start(); }); });
|
|
}
|
|
|
|
function messageBusFactory(instance: WebWorkerInstance): MessageBus {
|
|
return instance.bus;
|
|
}
|
|
|
|
function initWebWorkerRenderPlatform(): void {
|
|
BrowserDomAdapter.makeCurrent();
|
|
wtfInit();
|
|
BrowserGetTestability.init();
|
|
}
|
|
|
|
export function workerRenderPlatform(): PlatformRef {
|
|
if (isBlank(getPlatform())) {
|
|
createPlatform(ReflectiveInjector.resolveAndCreate(WORKER_RENDER_PLATFORM_PROVIDERS));
|
|
}
|
|
return assertPlatform(WORKER_RENDER_PLATFORM_MARKER);
|
|
}
|
|
|
|
function _exceptionHandler(): ExceptionHandler {
|
|
return new ExceptionHandler(getDOM());
|
|
}
|
|
|
|
function _document(): any {
|
|
return getDOM().defaultDoc();
|
|
}
|
|
|
|
function initWebWorkerAppFn(injector: Injector): () => void {
|
|
return () => {
|
|
var scriptUri: string;
|
|
try {
|
|
scriptUri = injector.get(WORKER_SCRIPT);
|
|
} catch (e) {
|
|
throw new BaseException(
|
|
'You must provide your WebWorker\'s initialization script with the WORKER_SCRIPT token');
|
|
}
|
|
|
|
let instance = injector.get(WebWorkerInstance);
|
|
spawnWebWorker(scriptUri, instance);
|
|
|
|
initializeGenericWorkerRenderer(injector);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Spawns a new class and initializes the WebWorkerInstance
|
|
*/
|
|
function spawnWebWorker(uri: string, instance: WebWorkerInstance): void {
|
|
var webWorker: Worker = new Worker(uri);
|
|
var sink = new PostMessageBusSink(webWorker);
|
|
var source = new PostMessageBusSource(webWorker);
|
|
var bus = new PostMessageBus(sink, source);
|
|
|
|
instance.init(webWorker, bus);
|
|
}
|
|
|
|
function _resolveDefaultAnimationDriver(): AnimationDriver {
|
|
// web workers have not been tested or configured to
|
|
// work with animations just yet...
|
|
return new NoOpAnimationDriver();
|
|
}
|