69 lines
2.1 KiB
Dart
Raw Normal View History

refactor(WebWorker): Use the new generic bootstrap. BREAKING CHANGE: You can no longer bootstrap a WebWorker or Isolate using `bootstrap` or `bootstrapWebWorker`. Instead you have to do the following: In TypeScript: ```TypeScript // index.js import {WORKER_RENDER_PLATFORM, WORKER_RENDER_APPLICATION, WORKER_SCRIPT} from "angular2/platforms/worker_render"; import {platform} from "angular2/platform"; platform([WORKER_RENDER_PLATFORM]) .application([WORKER_RENDER_APPLICATION, new Provider(WORKER_SCRIPT, {useValue: "loader.js"}); ``` ```JavaScript // loader.js importScripts("https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.js", "https://jspm.io/system@0.16.js", "angular2/web_worker/worker.js"); System.import("app"); ``` ```TypeScript // app.ts import {Component, View} from "angular2/core"; import {WORKER_APP_PLATFORM, setupWebWorker} from "angular2/platforms/worker_app"; import {platform} from "angular2/platform"; @Component({ selector: "hello-world" }) @View({ template: "<h1>Hello {{name}}</h1> }) export class HelloWorld { name: string = "Jane"; } platform([WORKER_APP_PLATFORM]) .asyncApplication(setupWebWorker, optionalProviders?) .then((ref) => ref.bootstrap(RootComponent)); ``` In Dart: ```Dart // index.dart import "angular2/platform.dart"; import "angular2/platforms/worker_render.dart"; main() { platform([WORKER_RENDER_PLATFORM]) .asyncApplication(initIsolate("my_worker.dart")); } ``` ```Dart // background_index.dart import "angular2/platform.dart"; import "angular2/platforms/worker_app.dart"; import "package:angular2/src/core/reflection/reflection.dart"; import "package:angular2/src/core/reflection/reflection_capabilities.dart"; @Component( selector: "hello-world" ) @View( template: "<h1>Hello {{name}}</h1>" ) class HelloWorld { String name = "Jane"; } main(List<String> args, SendPort replyTo) { reflector.reflectionCapabilities = new ReflectionCapabilities(); platform([WORKER_APP_PLATFORM]) .asyncApplication(setupIsolate(replyTo)) .then((ref) => ref.bootstrap(RootComponent)); } ``` You should no longer import from the `angular2/web_worker/worker` and `angular2/web_worker/ui` paths. Instead you can now import directly from core, directives, etc.. The WebWorkerApplication class has been removed. If you want to use ServiceMessageBroker or ClientMessageBroker on the render thread, you must inject their factories via DI. If you need to use the MessageBus on the render thread you must also obtain it through DI. closes #3277 closes #5473 Closes #5519
2015-12-02 20:25:24 -08:00
library angular2.src.platform.worker_render;
import 'package:angular2/src/platform/worker_render_common.dart'
show
WORKER_RENDER_APPLICATION_COMMON,
refactor(WebWorker): Use the new generic bootstrap. BREAKING CHANGE: You can no longer bootstrap a WebWorker or Isolate using `bootstrap` or `bootstrapWebWorker`. Instead you have to do the following: In TypeScript: ```TypeScript // index.js import {WORKER_RENDER_PLATFORM, WORKER_RENDER_APPLICATION, WORKER_SCRIPT} from "angular2/platforms/worker_render"; import {platform} from "angular2/platform"; platform([WORKER_RENDER_PLATFORM]) .application([WORKER_RENDER_APPLICATION, new Provider(WORKER_SCRIPT, {useValue: "loader.js"}); ``` ```JavaScript // loader.js importScripts("https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.js", "https://jspm.io/system@0.16.js", "angular2/web_worker/worker.js"); System.import("app"); ``` ```TypeScript // app.ts import {Component, View} from "angular2/core"; import {WORKER_APP_PLATFORM, setupWebWorker} from "angular2/platforms/worker_app"; import {platform} from "angular2/platform"; @Component({ selector: "hello-world" }) @View({ template: "<h1>Hello {{name}}</h1> }) export class HelloWorld { name: string = "Jane"; } platform([WORKER_APP_PLATFORM]) .asyncApplication(setupWebWorker, optionalProviders?) .then((ref) => ref.bootstrap(RootComponent)); ``` In Dart: ```Dart // index.dart import "angular2/platform.dart"; import "angular2/platforms/worker_render.dart"; main() { platform([WORKER_RENDER_PLATFORM]) .asyncApplication(initIsolate("my_worker.dart")); } ``` ```Dart // background_index.dart import "angular2/platform.dart"; import "angular2/platforms/worker_app.dart"; import "package:angular2/src/core/reflection/reflection.dart"; import "package:angular2/src/core/reflection/reflection_capabilities.dart"; @Component( selector: "hello-world" ) @View( template: "<h1>Hello {{name}}</h1>" ) class HelloWorld { String name = "Jane"; } main(List<String> args, SendPort replyTo) { reflector.reflectionCapabilities = new ReflectionCapabilities(); platform([WORKER_APP_PLATFORM]) .asyncApplication(setupIsolate(replyTo)) .then((ref) => ref.bootstrap(RootComponent)); } ``` You should no longer import from the `angular2/web_worker/worker` and `angular2/web_worker/ui` paths. Instead you can now import directly from core, directives, etc.. The WebWorkerApplication class has been removed. If you want to use ServiceMessageBroker or ClientMessageBroker on the render thread, you must inject their factories via DI. If you need to use the MessageBus on the render thread you must also obtain it through DI. closes #3277 closes #5473 Closes #5519
2015-12-02 20:25:24 -08:00
WORKER_RENDER_MESSAGING_PROVIDERS,
WORKER_SCRIPT,
initializeGenericWorkerRenderer;
import 'package:angular2/src/web_workers/shared/isolate_message_bus.dart';
import 'package:angular2/src/web_workers/shared/message_bus.dart';
import 'package:angular2/core.dart';
import 'package:angular2/src/core/di.dart';
import 'package:angular2/src/core/zone/ng_zone.dart';
import 'dart:isolate';
import 'dart:async';
const WORKER_RENDER_APP = WORKER_RENDER_APPLICATION_COMMON;
refactor(WebWorker): Use the new generic bootstrap. BREAKING CHANGE: You can no longer bootstrap a WebWorker or Isolate using `bootstrap` or `bootstrapWebWorker`. Instead you have to do the following: In TypeScript: ```TypeScript // index.js import {WORKER_RENDER_PLATFORM, WORKER_RENDER_APPLICATION, WORKER_SCRIPT} from "angular2/platforms/worker_render"; import {platform} from "angular2/platform"; platform([WORKER_RENDER_PLATFORM]) .application([WORKER_RENDER_APPLICATION, new Provider(WORKER_SCRIPT, {useValue: "loader.js"}); ``` ```JavaScript // loader.js importScripts("https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.js", "https://jspm.io/system@0.16.js", "angular2/web_worker/worker.js"); System.import("app"); ``` ```TypeScript // app.ts import {Component, View} from "angular2/core"; import {WORKER_APP_PLATFORM, setupWebWorker} from "angular2/platforms/worker_app"; import {platform} from "angular2/platform"; @Component({ selector: "hello-world" }) @View({ template: "<h1>Hello {{name}}</h1> }) export class HelloWorld { name: string = "Jane"; } platform([WORKER_APP_PLATFORM]) .asyncApplication(setupWebWorker, optionalProviders?) .then((ref) => ref.bootstrap(RootComponent)); ``` In Dart: ```Dart // index.dart import "angular2/platform.dart"; import "angular2/platforms/worker_render.dart"; main() { platform([WORKER_RENDER_PLATFORM]) .asyncApplication(initIsolate("my_worker.dart")); } ``` ```Dart // background_index.dart import "angular2/platform.dart"; import "angular2/platforms/worker_app.dart"; import "package:angular2/src/core/reflection/reflection.dart"; import "package:angular2/src/core/reflection/reflection_capabilities.dart"; @Component( selector: "hello-world" ) @View( template: "<h1>Hello {{name}}</h1>" ) class HelloWorld { String name = "Jane"; } main(List<String> args, SendPort replyTo) { reflector.reflectionCapabilities = new ReflectionCapabilities(); platform([WORKER_APP_PLATFORM]) .asyncApplication(setupIsolate(replyTo)) .then((ref) => ref.bootstrap(RootComponent)); } ``` You should no longer import from the `angular2/web_worker/worker` and `angular2/web_worker/ui` paths. Instead you can now import directly from core, directives, etc.. The WebWorkerApplication class has been removed. If you want to use ServiceMessageBroker or ClientMessageBroker on the render thread, you must inject their factories via DI. If you need to use the MessageBus on the render thread you must also obtain it through DI. closes #3277 closes #5473 Closes #5519
2015-12-02 20:25:24 -08:00
initIsolate(String scriptUri) {
return (NgZone zone) async {
var instance = await spawnIsolate(Uri.parse(scriptUri));
return [
WORKER_RENDER_APPLICATION_COMMON,
refactor(WebWorker): Use the new generic bootstrap. BREAKING CHANGE: You can no longer bootstrap a WebWorker or Isolate using `bootstrap` or `bootstrapWebWorker`. Instead you have to do the following: In TypeScript: ```TypeScript // index.js import {WORKER_RENDER_PLATFORM, WORKER_RENDER_APPLICATION, WORKER_SCRIPT} from "angular2/platforms/worker_render"; import {platform} from "angular2/platform"; platform([WORKER_RENDER_PLATFORM]) .application([WORKER_RENDER_APPLICATION, new Provider(WORKER_SCRIPT, {useValue: "loader.js"}); ``` ```JavaScript // loader.js importScripts("https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.js", "https://jspm.io/system@0.16.js", "angular2/web_worker/worker.js"); System.import("app"); ``` ```TypeScript // app.ts import {Component, View} from "angular2/core"; import {WORKER_APP_PLATFORM, setupWebWorker} from "angular2/platforms/worker_app"; import {platform} from "angular2/platform"; @Component({ selector: "hello-world" }) @View({ template: "<h1>Hello {{name}}</h1> }) export class HelloWorld { name: string = "Jane"; } platform([WORKER_APP_PLATFORM]) .asyncApplication(setupWebWorker, optionalProviders?) .then((ref) => ref.bootstrap(RootComponent)); ``` In Dart: ```Dart // index.dart import "angular2/platform.dart"; import "angular2/platforms/worker_render.dart"; main() { platform([WORKER_RENDER_PLATFORM]) .asyncApplication(initIsolate("my_worker.dart")); } ``` ```Dart // background_index.dart import "angular2/platform.dart"; import "angular2/platforms/worker_app.dart"; import "package:angular2/src/core/reflection/reflection.dart"; import "package:angular2/src/core/reflection/reflection_capabilities.dart"; @Component( selector: "hello-world" ) @View( template: "<h1>Hello {{name}}</h1>" ) class HelloWorld { String name = "Jane"; } main(List<String> args, SendPort replyTo) { reflector.reflectionCapabilities = new ReflectionCapabilities(); platform([WORKER_APP_PLATFORM]) .asyncApplication(setupIsolate(replyTo)) .then((ref) => ref.bootstrap(RootComponent)); } ``` You should no longer import from the `angular2/web_worker/worker` and `angular2/web_worker/ui` paths. Instead you can now import directly from core, directives, etc.. The WebWorkerApplication class has been removed. If you want to use ServiceMessageBroker or ClientMessageBroker on the render thread, you must inject their factories via DI. If you need to use the MessageBus on the render thread you must also obtain it through DI. closes #3277 closes #5473 Closes #5519
2015-12-02 20:25:24 -08:00
new Provider(WebWorkerInstance, useValue: instance),
new Provider(APP_INITIALIZER,
useFactory: (injector) => () => initializeGenericWorkerRenderer(injector),
multi: true,
deps: [Injector]),
new Provider(MessageBus, useValue: instance.bus)
];
};
}
/**
* Spawns a new class and initializes the WebWorkerInstance
*/
Future<WebWorkerInstance> spawnIsolate(Uri uri) async {
var receivePort = new ReceivePort();
var isolateEndSendPort = receivePort.sendPort;
var isolate = await Isolate.spawnUri(uri, const [], isolateEndSendPort);
var source = new UIMessageBusSource(receivePort);
var sendPort = await source.sink;
var sink = new IsolateMessageBusSink(sendPort);
var bus = new IsolateMessageBus(sink, source);
return new WebWorkerInstance(isolate, bus);
}
class UIMessageBusSource extends IsolateMessageBusSource {
UIMessageBusSource(ReceivePort port) : super(port);
Future<SendPort> get sink => stream.firstWhere((message) {
return message is SendPort;
});
}
/**
* Wrapper class that exposes the Isolate
* and underlying {@link MessageBus} for lower level message passing.
*/
@Injectable()
class WebWorkerInstance {
Isolate worker;
MessageBus bus;
WebWorkerInstance(this.worker, this.bus);
}