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 updated, so this readme may be not relevant until(i hope, short coming) review.
7
-
8
6
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.
9
7
8
+
[Demo with dedicated and shared workers](http://burdiuz.github.io/js-worker-event-dispatcher/index.html)
9
+
10
10
## Installation
11
-
WorkerDispatcher is available via [bower](http://bower.io/)
12
-
```
13
-
bower install worker-event-dispatcher --save
11
+
Easy to install with [npm](https://www.npmjs.com/) package manager
12
+
```javascript
13
+
npm install --save @actualwave/worker-dispatcher
14
14
```
15
-
If you want to use it with [npm](https://www.npmjs.com/) package manger, add it to `dependencies`section of your package.json file.
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.
22
19
23
20
## Usage
24
-
WorkerDispatcher should be used on HTML page and in Worker script to properly handle communication.
21
+
> Note: WorkerDispatcher distribution package contains `dist/` folder with package wrapped into [UMD](https://github.com/umdjs/umd) wrapper, so it can be used with any AMD module loader, nodejs `require()` or without any.
22
+
23
+
**WorkerDispatcher should be used on HTML page and in Worker script to properly handle communication.**
25
24
26
25
#### Dedicated Worker
27
-
WorkerDispatcher for Dedicated Worker can be created via operator `new`
28
-
```javascript
29
-
var worker =newWorker('/workers/worker.js');
30
-
var dispatcher =newWorkerDispatcher(worker);
31
-
```
32
-
or `WorkerDispatcher.create()` factory method
26
+
WorkerDispatcher for Dedicated Worker can be created via `WorkerDispatcher.create()` factory function
33
27
```javascript
34
28
var worker =newWorker('/workers/worker.js');
35
29
var dispatcher =WorkerDispatcher.create(worker);
36
-
/* or you can specify worker type */
30
+
/* or you can explicitly specify worker type */
37
31
var dispatcher =WorkerDispatcher.create(worker, WorkerDispatcher.DEDICATED_WORKER);
38
32
```
39
33
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.
40
-
```javascript
34
+
```javascript
41
35
var dispatcher =WorkerDispatcher.createForSelf();
42
36
```
43
37
WorkerDispatcher accepts Worker objects or string URL to JS file that should be launched in worker.
44
38
Here Worker will be created from passed URL string:
45
39
```javascript
46
-
var dispatcher =newWorkerDispatcher('/workers/worker.js');
40
+
var dispatcher =WorkerDispatcher.create('/workers/worker.js');
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
71
-
```javascript
72
-
var dispatcher =newWorkerDispatcher('/workers/worker.js');
65
+
```javascript
66
+
var dispatcher =WorkerDispatcher.create('/workers/worker.js');
@@ -101,7 +95,7 @@ Project contains `example` folder with examples for Dedicated and Shared workers
101
95
#### WorkerDispatcher shared instance members
102
96
WorkerDispatcher is a base class and it shares functionality across all types of WorkerDispatcher's. When WorkerDispatcher instantiated directly, it actually creates DedicatedWorkerDispatcher.
103
97
104
-
-**type**:String - type of the worker
98
+
-**type**:String - type of the worker
105
99
Including [all members of MessagePortDispatcher](https://github.com/burdiuz/js-messageport-event-dispatcher/blob/master/README.md#messageportdispatcher-instance-members), some most important:
106
100
-**addEventListener**(eventType:String, listener:Function):void - add listener for incoming events. This method copied from `receiver`.
107
101
-**hasEventListener**(eventType:String):Boolean - check if incoming event has listeners. This method copied from `receiver`.
@@ -117,19 +111,19 @@ Including [all members of MessagePortDispatcher](https://github.com/burdiuz/js-m
117
111
-**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
112
-**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.
119
113
120
-
- WorkerEvent:Object - Worker event types
114
+
- WorkerEvent:Object - Worker event types
121
115
- 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.
122
116
- ERROR:String - Mirroring [error event](https://developer.mozilla.org/en-US/docs/Web/Events/error) fired from WorkerGlobalScope
123
117
- LANGUAGECHANGE:String - Mirroring [languagechange event](https://developer.mozilla.org/en-US/docs/Web/Events/languagechange) fired from WorkerGlobalScope
124
-
- ONLINE:String - Mirroring [online event](https://developer.mozilla.org/en-US/docs/Web/Events/online) fired from WorkerGlobalScope
118
+
- ONLINE:String - Mirroring [online event](https://developer.mozilla.org/en-US/docs/Web/Events/online) fired from WorkerGlobalScope
125
119
- OFFLINE:String - Mirroring [offline event](https://developer.mozilla.org/en-US/docs/Web/Events/offline) fired from WorkerGlobalScope
126
120
- WorkerType:Object - Possible dispatcher types, used with `WorkerDispatcher.create()`
127
121
- DEDICATED_WORKER:String - Default type, will create DedicatedWorkerDispatcher
128
122
- SHARED_WORKER:String - Will create SharedWorkerDispatcher
129
123
- SHARED_WORKER_SERVER:String - For internal usage, will create ServerEventDispatcher
130
124
- SHARED_WORKER_CLIENT:String - For internal usage, will create ClientEventDispatcher
131
125
- DedicatedWorker:Function - Constructor of DedicatedWorkerDispatcher
132
-
- SharedWorker:Function - Constructor of SharedWorkerDispatcher
126
+
- SharedWorker:Function - Constructor of SharedWorkerDispatcher
133
127
- Server:Function - Constructor of ServerEventDispatcher
134
128
- Client:Function - Constructor of ClientEventDispatcher
135
129
@@ -143,7 +137,7 @@ Created when `WorkerDispatcher.DEDICATED_WORKER` used, when `WorkerDispatcher.cr
143
137
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.
144
138
```javascript
145
139
var dispatcher =newWorkerDispatcher.SharedWorkerDispatcher('/workers/sharedworker.js', 'worker-name');
146
-
```
140
+
```
147
141
148
142
#### ServerEventDispatcher
149
143
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.
0 commit comments