Skip to content

Commit 84463cf

Browse files
jteplitzjteplitz
authored andcommitted
Feat(WebWorker): Add WebWorker Image Filter Demo
1 parent 2dcf714 commit 84463cf

29 files changed

Lines changed: 694 additions & 21 deletions

modules/angular2/src/web-workers/ui/application.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import 'package:angular2/src/web-workers/ui/impl.dart' show bootstrapUICommon;
1313
* You instantiate a WebWorker application by calling bootstrap with the URI of your worker's index script
1414
* Note: The WebWorker script must call bootstrapWebworker once it is set up to complete the bootstrapping process
1515
*/
16-
void bootstrap(String uri) {
17-
spawnWorker(Uri.parse(uri)).then((bus) {
16+
Future<MessageBus> bootstrap(String uri) {
17+
return spawnWorker(Uri.parse(uri)).then((bus) {
1818
bootstrapUICommon(bus);
19+
return bus;
1920
});
2021
}
2122

modules/angular2/src/web-workers/ui/application.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import {bootstrapUICommon} from "angular2/src/web-workers/ui/impl";
1515
* Note: The WebWorker script must call bootstrapWebworker once it is set up to complete the
1616
* bootstrapping process
1717
*/
18-
export function bootstrap(uri: string): void {
18+
export function bootstrap(uri: string): MessageBus {
1919
var messageBus = spawnWorker(uri);
2020
bootstrapUICommon(messageBus);
21+
return messageBus;
2122
}
2223

2324
export function spawnWorker(uri: string): MessageBus {

modules/angular2/src/web-workers/ui/event_serializer.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ Map<String, dynamic> serializeGenericEvent(dynamic e) {
8080

8181
// TODO(jteplitz602): Allow users to specify the properties they need rather than always
8282
// adding value #3374
83-
Map<String, dynamic> serializeEventWithValue(dynamic e) {
83+
Map<String, dynamic> serializeEventWithTarget(dynamic e) {
8484
var serializedEvent = serializeEvent(e, EVENT_PROPERTIES);
85-
return addValue(e, serializedEvent);
85+
return addTarget(e, serializedEvent);
8686
}
8787

8888
Map<String, dynamic> serializeMouseEvent(dynamic e) {
@@ -91,13 +91,16 @@ Map<String, dynamic> serializeMouseEvent(dynamic e) {
9191

9292
Map<String, dynamic> serializeKeyboardEvent(dynamic e) {
9393
var serializedEvent = serializeEvent(e, KEYBOARD_EVENT_PROPERTIES);
94-
return addValue(e, serializedEvent);
94+
return addTarget(e, serializedEvent);
9595
}
9696

9797
// TODO(jteplitz602): #3374. See above.
98-
Map<String, dynamic> addValue(dynamic e, Map<String, dynamic> serializedEvent) {
98+
Map<String, dynamic> addTarget(dynamic e, Map<String, dynamic> serializedEvent) {
9999
if (NODES_WITH_VALUE.contains(e.target.tagName.toLowerCase())) {
100100
serializedEvent['target'] = {'value': e.target.value};
101+
if (e.target is InputElement) {
102+
serializedEvent['target']['files'] = e.target.files;
103+
}
101104
}
102105
return serializedEvent;
103106
}

modules/angular2/src/web-workers/ui/event_serializer.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {StringMap, Set} from 'angular2/src/facade/collection';
2+
import {isPresent} from 'angular2/src/facade/lang';
23

34
const MOUSE_EVENT_PROPERTIES = [
45
"altKey",
@@ -41,10 +42,10 @@ export function serializeGenericEvent(e: Event): StringMap<string, any> {
4142
}
4243

4344
// TODO(jteplitz602): Allow users to specify the properties they need rather than always
44-
// adding value #3374
45-
export function serializeEventWithValue(e: Event): StringMap<string, any> {
45+
// adding value and files #3374
46+
export function serializeEventWithTarget(e: Event): StringMap<string, any> {
4647
var serializedEvent = serializeEvent(e, EVENT_PROPERTIES);
47-
return addValue(e, serializedEvent);
48+
return addTarget(e, serializedEvent);
4849
}
4950

5051
export function serializeMouseEvent(e: MouseEvent): StringMap<string, any> {
@@ -53,13 +54,17 @@ export function serializeMouseEvent(e: MouseEvent): StringMap<string, any> {
5354

5455
export function serializeKeyboardEvent(e: KeyboardEvent): StringMap<string, any> {
5556
var serializedEvent = serializeEvent(e, KEYBOARD_EVENT_PROPERTIES);
56-
return addValue(e, serializedEvent);
57+
return addTarget(e, serializedEvent);
5758
}
5859

5960
// TODO(jteplitz602): #3374. See above.
60-
function addValue(e: Event, serializedEvent: StringMap<string, any>): StringMap<string, any> {
61+
function addTarget(e: Event, serializedEvent: StringMap<string, any>): StringMap<string, any> {
6162
if (NODES_WITH_VALUE.has((<HTMLElement>e.target).tagName.toLowerCase())) {
62-
serializedEvent['target'] = {'value': (<HTMLInputElement>e.target).value};
63+
var target = <HTMLInputElement>e.target;
64+
serializedEvent['target'] = {'value': target.value};
65+
if (isPresent(target.files)) {
66+
serializedEvent['target']['files'] = target.files;
67+
}
6368
}
6469
return serializedEvent;
6570
}

modules/angular2/src/web-workers/ui/impl.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ import {
3535
serializeMouseEvent,
3636
serializeKeyboardEvent,
3737
serializeGenericEvent,
38-
serializeEventWithValue
38+
serializeEventWithTarget
3939
} from 'angular2/src/web-workers/ui/event_serializer';
40+
import {wtfInit} from 'angular2/src/profile/wtf_init';
4041

4142
/**
4243
* Creates a zone, sets up the DI bindings
@@ -45,6 +46,7 @@ import {
4546
export function bootstrapUICommon(bus: MessageBus) {
4647
BrowserDomAdapter.makeCurrent();
4748
var zone = createNgZone();
49+
wtfInit();
4850
zone.run(() => {
4951
var injector = createInjector(zone);
5052
var webWorkerMain = injector.get(WebWorkerMain);
@@ -259,7 +261,7 @@ class EventDispatcher implements RenderEventDispatcher {
259261
case "input":
260262
case "change":
261263
case "blur":
262-
serializedEvent = serializeEventWithValue(e);
264+
serializedEvent = serializeEventWithTarget(e);
263265
break;
264266
case "abort":
265267
case "afterprint":

modules/angular2/src/web-workers/worker/application.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {bootstrapWebworkerCommon} from "angular2/src/web-workers/worker/applicat
1111
import {ApplicationRef} from "angular2/src/core/application";
1212
import {Injectable} from "angular2/di";
1313

14+
// TODO(jteplitz602) remove this and compile with lib.webworker.d.ts (#3492)
15+
var _postMessage: (message: any, transferrables?:[ArrayBuffer]) => void = <any>postMessage;
16+
1417
/**
1518
* Bootstrapping a Webworker Application
1619
*
@@ -41,7 +44,7 @@ export class WorkerMessageBus implements MessageBus {
4144
}
4245

4346
export class WorkerMessageBusSink implements MessageBusSink {
44-
public send(message: Object) { postMessage(message, null); }
47+
public send(message: Object) { _postMessage(message); }
4548
}
4649

4750
export class WorkerMessageBusSource implements MessageBusSource {

modules/angular2/src/web-workers/worker/application_common.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ import {
1717
ChangeDetection,
1818
DynamicChangeDetection,
1919
JitChangeDetection,
20+
PreGeneratedChangeDetection,
2021
Pipes,
2122
defaultPipes,
22-
PreGeneratedChangeDetection
23+
IterableDiffers,
24+
defaultIterableDiffers,
25+
KeyValueDiffers,
26+
defaultKeyValueDiffers
2327
} from 'angular2/src/change_detection/change_detection';
2428
import {StyleUrlResolver} from 'angular2/src/render/dom/compiler/style_url_resolver';
2529
import {ExceptionHandler} from 'angular2/src/core/exception_handler';
@@ -119,6 +123,8 @@ function _injectorBindings(appComponentType, bus: WorkerMessageBus,
119123
CompilerCache,
120124
ViewResolver,
121125
defaultPipes,
126+
bind(IterableDiffers).toValue(defaultIterableDiffers),
127+
bind(KeyValueDiffers).toValue(defaultKeyValueDiffers),
122128
bind(ChangeDetection).toClass(bestChangeDetection),
123129
DirectiveResolver,
124130
Parser,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "B64" {
2+
export function fromByteArray(arr: Uint8Array): string;
3+
export function toByteArray(str: string): Uint8Array;
4+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
library examples.src.web_workers.images.background_index;
2+
3+
import "index_common.dart" show ImageDemo;
4+
import "dart:isolate";
5+
import "package:angular2/src/web-workers/worker/application.dart"
6+
show bootstrapWebworker;
7+
import "package:angular2/src/reflection/reflection_capabilities.dart";
8+
import "package:angular2/src/reflection/reflection.dart";
9+
10+
main(List<String> args, SendPort replyTo) {
11+
reflector.reflectionCapabilities = new ReflectionCapabilities();
12+
bootstrapWebworker(replyTo, ImageDemo).catchError((error) => throw error);
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {bootstrapWebworker} from "angular2/src/web-workers/worker/application";
2+
import {ImageDemo} from "./index_common";
3+
4+
export function main() {
5+
bootstrapWebworker(ImageDemo);
6+
}

0 commit comments

Comments
 (0)