You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Note: This project was renamed from WorkerEventDispatcher and its API was slightly updates, so this readme may be not relevant until(i hope, short coming) review.
5
7
6
8
This is extension of [MessagePortDispatcher](https://github.com/burdiuz/js-messageport-event-dispatcher) to work with Dedicated and Shared Workers. It makes possible two-way communication with Workers using custom events. So, instead of using `postMessage()` and catching `message` event all the time, you are free to send any type of events to and from worker.
7
9
8
10
## Installation
9
-
WorkerEventDispatcher is available via [bower](http://bower.io/)
11
+
WorkerDispatcher is available via [bower](http://bower.io/)
10
12
```
11
13
bower install worker-event-dispatcher --save
12
14
```
@@ -16,45 +18,45 @@ If you want to use it with [npm](https://www.npmjs.com/) package manger, add it
Also WorkerEventDispatcher has standalone distribution file that includes all dependencies, so you can just [download single file](https://github.com/burdiuz/js-worker-event-dispatcher/blob/master/dist/worker-event-dispatcher.standalone.min.js) with everything needed to start using it.
21
+
Also WorkerDispatcher has standalone distribution file that includes all dependencies, so you can just [download single file](https://github.com/burdiuz/js-worker-event-dispatcher/blob/master/dist/worker-event-dispatcher.standalone.min.js) with everything needed to start using it.
20
22
21
23
## Usage
22
-
WorkerEventDispatcher should be used on HTML page and in Worker script to properly handle communication.
24
+
WorkerDispatcher should be used on HTML page and in Worker script to properly handle communication.
23
25
24
26
#### Dedicated Worker
25
-
WorkerEventDispatcher for Dedicated Worker can be created via operator `new`
27
+
WorkerDispatcher for Dedicated Worker can be created via operator `new`
26
28
```javascript
27
29
var worker =newWorker('/workers/worker.js');
28
-
var dispatcher =newWorkerEventDispatcher(worker);
30
+
var dispatcher =newWorkerDispatcher(worker);
29
31
```
30
-
or `WorkerEventDispatcher.create()` factory method
32
+
or `WorkerDispatcher.create()` factory method
31
33
```javascript
32
34
var worker =newWorker('/workers/worker.js');
33
-
var dispatcher =WorkerEventDispatcher.create(worker);
35
+
var dispatcher =WorkerDispatcher.create(worker);
34
36
/* or you can specify worker type */
35
-
var dispatcher =WorkerEventDispatcher.create(worker, WorkerEventDispatcher.DEDICATED_WORKER);
37
+
var dispatcher =WorkerDispatcher.create(worker, WorkerDispatcher.DEDICATED_WORKER);
36
38
```
37
-
Within Worker script it can be created via `WorkerEventDispatcher.self()`, don't need to pass anything, it grabs Worker's global scope object to communicate.
39
+
Within Worker script it can be created via `WorkerDispatcher.createForSelf()`, don't need to pass anything, it grabs Worker's global scope object to communicate.
38
40
```javascript
39
-
var dispatcher =WorkerEventDispatcher.self();
41
+
var dispatcher =WorkerDispatcher.createForSelf();
40
42
```
41
-
WorkerEventDispatcher accepts Worker objects or string URL to JS file that should be launched in worker.
43
+
WorkerDispatcher accepts Worker objects or string URL to JS file that should be launched in worker.
42
44
Here Worker will be created from passed URL string:
43
45
```javascript
44
-
var dispatcher =newWorkerEventDispatcher('/workers/worker.js');
46
+
var dispatcher =newWorkerDispatcher('/workers/worker.js');
45
47
```
46
48
47
49
#### Shared Worker
48
-
To use WorkerEventDispatcher with Shared Worker, it should be created via `WorkerEventDispatcher.create()` factory method with specified Worker type.
50
+
To use WorkerDispatcher with Shared Worker, it should be created via `WorkerDispatcher.create()` factory method with specified Worker type.
49
51
```javascript
50
52
var worker =newSharedWorker('/workers/sharedworker.js');
51
-
var dispatcher =WorkerEventDispatcher.create(worker, WorkerEventDispatcher.SHARED_WORKER);
53
+
var dispatcher =WorkerDispatcher.create(worker, WorkerDispatcher.SHARED_WORKER);
52
54
dispatcher.start();
53
55
```
54
-
Within SharedWorker it can be created via `WorkerEventDispatcher.self()`. WorkerEventDispatcher's for client connections will be created automatically.
56
+
Within SharedWorker it can be created via `WorkerDispatcher.createForSelf()`. WorkerDispatcher's for client connections will be created automatically.
To send messages use `dispatchEvent()` event and to receive messages add event listeners. Sent events will not be fired for sender dispatcher, so you cannot listen for event you just sent
69
71
```javascript
70
-
var dispatcher =newWorkerEventDispatcher('/workers/worker.js');
72
+
var dispatcher =newWorkerDispatcher('/workers/worker.js');
Project contains `example` folder with examples for Dedicated and Shared workers communication built with WorkerEventDispatcher.
92
+
Project contains `example` folder with examples for Dedicated and Shared workers communication built with WorkerDispatcher.
91
93
92
94
## API
93
-
#### WorkerEventDispatcher constructor arguments
95
+
#### WorkerDispatcher constructor arguments
94
96
-**worker**:Worker|MessagePort|String - Worker instance or URL string for worker script.
95
97
-**receiverEventPreprocessor**:Function - Optional, allows pre-processing of events and their data before firing event.
96
98
-**senderEventPreprocessor**:Function - Optional, allows pre-processing of events and their data before passing them to `postMessage`.
97
99
-*type?:String - argument used internally to generate type property in prototype.*
98
100
99
-
#### WorkerEventDispatcher shared instance members
100
-
WorkerEventDispatcher is a base class and it shares functionality across all types of WorkerEventDispatcher's. When WorkerEventDispatcher instantiated directly, it actually creates DedicatedWorkerEventDispatcher.
101
+
#### WorkerDispatcher shared instance members
102
+
WorkerDispatcher is a base class and it shares functionality across all types of WorkerDispatcher's. When WorkerDispatcher instantiated directly, it actually creates DedicatedWorkerDispatcher.
101
103
102
104
-**type**:String - type of the worker
103
105
Including [all members of MessagePortDispatcher](https://github.com/burdiuz/js-messageport-event-dispatcher/blob/master/README.md#messageportdispatcher-instance-members), some most important:
@@ -107,44 +109,44 @@ Including [all members of MessagePortDispatcher](https://github.com/burdiuz/js-m
107
109
-**dispatchEvent**(event:Object):void - does not fire event, it sends event to `postMessage()`. Can be used with two arguments:
-**CONNECT_EVENT**:String - Short of `WorkerEvent.CONNECT`. Event fired in Shared Worker script when new client is available.
113
115
-**DEDICATED_WORKER**:String - Short of `WorkerType.DEDICATED_WORKER`
114
116
-**SHARED_WORKER**:String - Short of `WorkerType.SHARED_WORKER`
115
-
-**create**(target:String|Worker|SharedWorker, type?:String, receiverEventPreprocessor?:Function, senderEventPreprocessor?:Function):WorkerEventDispatcher - Creates WorkerEventDispatcher instance based on type. Currently supported types are `WorkerEventDispatcher.DEDICATED_WORKER` and `WorkerEventDispatcher.SHARED_WORKER`. By default will create dispatcher for Dedicated Worker.
116
-
-**self**(receiverEventPreprocessor?:Function, senderEventPreprocessor?:Function):WorkerEventDispatcher - Can be used in Worker script, it checks what kind of worker is used and returns proper dispatcher object for WorkerGlobalScope. For Dedicated Worker returns instance of DedicatedWorkerEventDispatcher and for Shared Worker -- ServerEventDispatcher.
117
+
-**create**(target:String|Worker|SharedWorker, type?:String, receiverEventPreprocessor?:Function, senderEventPreprocessor?:Function):WorkerDispatcher - Creates WorkerDispatcher instance based on type. Currently supported types are `WorkerDispatcher.DEDICATED_WORKER` and `WorkerDispatcher.SHARED_WORKER`. By default will create dispatcher for Dedicated Worker.
118
+
-**self**(receiverEventPreprocessor?:Function, senderEventPreprocessor?:Function):WorkerDispatcher - Can be used in Worker script, it checks what kind of worker is used and returns proper dispatcher object for WorkerGlobalScope. For Dedicated Worker returns instance of DedicatedWorkerDispatcher and for Shared Worker -- ServerEventDispatcher.
117
119
118
120
- WorkerEvent:Object - Worker event types
119
121
- CONNECT:String - Mirroring connect event fired from WorkerGlobalScope, fired when new client connected. Event object contains field `client` with `ClientEventDispatcher` instance, to communicate with client.
120
122
- ERROR:String - Mirroring [error event](https://developer.mozilla.org/en-US/docs/Web/Events/error) fired from WorkerGlobalScope
121
123
- LANGUAGECHANGE:String - Mirroring [languagechange event](https://developer.mozilla.org/en-US/docs/Web/Events/languagechange) fired from WorkerGlobalScope
122
124
- ONLINE:String - Mirroring [online event](https://developer.mozilla.org/en-US/docs/Web/Events/online) fired from WorkerGlobalScope
123
125
- OFFLINE:String - Mirroring [offline event](https://developer.mozilla.org/en-US/docs/Web/Events/offline) fired from WorkerGlobalScope
124
-
- WorkerType:Object - Possible dispatcher types, used with `WorkerEventDispatcher.create()`
125
-
- DEDICATED_WORKER:String - Default type, will create DedicatedWorkerEventDispatcher
126
-
- SHARED_WORKER:String - Will create SharedWorkerEventDispatcher
126
+
- WorkerType:Object - Possible dispatcher types, used with `WorkerDispatcher.create()`
127
+
- DEDICATED_WORKER:String - Default type, will create DedicatedWorkerDispatcher
128
+
- SHARED_WORKER:String - Will create SharedWorkerDispatcher
127
129
- SHARED_WORKER_SERVER:String - For internal usage, will create ServerEventDispatcher
128
130
- SHARED_WORKER_CLIENT:String - For internal usage, will create ClientEventDispatcher
129
-
- DedicatedWorker:Function - Constructor of DedicatedWorkerEventDispatcher
130
-
- SharedWorker:Function - Constructor of SharedWorkerEventDispatcher
131
+
- DedicatedWorker:Function - Constructor of DedicatedWorkerDispatcher
132
+
- SharedWorker:Function - Constructor of SharedWorkerDispatcher
131
133
- Server:Function - Constructor of ServerEventDispatcher
132
134
- Client:Function - Constructor of ClientEventDispatcher
133
135
134
136
135
-
#### DedicatedWorkerEventDispatcher
136
-
Created when `WorkerEventDispatcher.DEDICATED_WORKER` used, when `WorkerEventDispatcher.self()` called in Dedicated Worker or when WorkerEventDispatcher called with `new` operator.
137
+
#### DedicatedWorkerDispatcher
138
+
Created when `WorkerDispatcher.DEDICATED_WORKER` used, when `WorkerDispatcher.createForSelf()` called in Dedicated Worker or when WorkerDispatcher called with `new` operator.
137
139
138
140
-**terminate**():void - close connection to worker, i.e. destroy worker.
139
141
140
-
#### SharedWorkerEventDispatcher
141
-
Created when WorkerEventDispatcher.SHARED_WORKER used. When created using `WorkerEventDispatcher.create()`, worker's name will default to `null`, if you need to specify name, you can instantiate it with constructor.
142
+
#### SharedWorkerDispatcher
143
+
Created when WorkerDispatcher.SHARED_WORKER used. When created using `WorkerDispatcher.create()`, worker's name will default to `null`, if you need to specify name, you can instantiate it with constructor.
142
144
```javascript
143
-
var dispatcher =newWorkerEventDispatcher.SharedWorkerEventDispatcher('/workers/sharedworker.js', 'worker-name');
145
+
var dispatcher =newWorkerDispatcher.SharedWorkerDispatcher('/workers/sharedworker.js', 'worker-name');
144
146
```
145
147
146
148
#### ServerEventDispatcher
147
-
Created when WorkerEventDispatcher.self() called in Shared Worker. It differs from other types of WorkerEventDispatcher's because **does not have `dispatchEvent()` method**, so it can only listen for events, like WorkerEvent.CONNECT to accept connections. Since it cannot send data, it does not have `sender` EventDispatcher either, only `receiver` available.
149
+
Created when WorkerDispatcher.createForSelf() called in Shared Worker. It differs from other types of WorkerDispatcher's because **does not have `dispatchEvent()` method**, so it can only listen for events, like WorkerEvent.CONNECT to accept connections. Since it cannot send data, it does not have `sender` EventDispatcher either, only `receiver` available.
148
150
149
151
#### ClientEventDispatcher
150
152
Created when Shared Worker gets new connection. to capture new connections, you shuld listen to WorkerEvent.CONNECT event.
@@ -155,9 +157,9 @@ Created when Shared Worker gets new connection. to capture new connections, you
0 commit comments