Skip to content

Commit e9cb9d6

Browse files
authored
Merge pull request #1 from burdiuz/v1
V1
2 parents 54ae879 + 8a7f39c commit e9cb9d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+18796
-2296
lines changed

.eslintrc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"parser": "babel-eslint",
3+
"extends": "airbnb",
4+
"parserOptions": {
5+
"ecmaVersion": 6,
6+
"ecmaFeatures": {
7+
"impliedStrict": true,
8+
"experimentalObjectRestSpread": true
9+
}
10+
},
11+
"env": {
12+
"browser": true,
13+
"node": true
14+
},
15+
"rules": {
16+
"arrow-parens": [
17+
"error",
18+
"always"
19+
],
20+
"linebreak-style": 0,
21+
"no-underscore-dangle": 0,
22+
"no-restricted-syntax": [
23+
"error",
24+
"WithStatement"
25+
],
26+
"no-param-reassign": 1,
27+
"padded-blocks": 0,
28+
"no-plusplus": 0
29+
}
30+
}

.flowconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[ignore]
2+
3+
[include]
4+
source/**/*.js
5+
6+
[libs]
7+
8+
[options]

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
language: node_js
22
node_js:
3-
- 4.1
3+
- "8"
44
addons:
55
firefox: "latest"
66
before_script:
77
- export DISPLAY=:99.0
88
- sh -e /etc/init.d/xvfb start
99
- sleep 3
10-
- gulp
10+
- webpack

README.md

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# WorkerEventDispatcher
1+
# WorkerDispatcher
22

33
[![Build Status](https://travis-ci.org/burdiuz/js-worker-event-dispatcher.svg?branch=master)](https://travis-ci.org/burdiuz/js-worker-event-dispatcher)
4-
[![Coverage Status](https://coveralls.io/repos/github/burdiuz/js-worker-event-dispatcher/badge.svg?branch=master)](https://coveralls.io/github/burdiuz/js-worker-event-dispatcher?branch=master)
4+
[![Coverage Status](https://coveralls.io/repos/github/burdiuz/js-worker-event-dispatcher/badge.svg?branch=master)](https://coveralls.io/github/burdiuz/js-worker-event-dispatcher?branch=master)
5+
6+
> 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.
57
68
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.
79

810
## Installation
9-
WorkerEventDispatcher is available via [bower](http://bower.io/)
11+
WorkerDispatcher is available via [bower](http://bower.io/)
1012
```
1113
bower install worker-event-dispatcher --save
1214
```
@@ -16,45 +18,45 @@ If you want to use it with [npm](https://www.npmjs.com/) package manger, add it
1618
"worker-event-dispatcher": "git://github.com/burdiuz/js-worker-event-dispatcher.git"
1719
}
1820
```
19-
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.
2022

2123
## 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.
2325

2426
#### Dedicated Worker
25-
WorkerEventDispatcher for Dedicated Worker can be created via operator `new`
27+
WorkerDispatcher for Dedicated Worker can be created via operator `new`
2628
```javascript
2729
var worker = new Worker('/workers/worker.js');
28-
var dispatcher = new WorkerEventDispatcher(worker);
30+
var dispatcher = new WorkerDispatcher(worker);
2931
```
30-
or `WorkerEventDispatcher.create()` factory method
32+
or `WorkerDispatcher.create()` factory method
3133
```javascript
3234
var worker = new Worker('/workers/worker.js');
33-
var dispatcher = WorkerEventDispatcher.create(worker);
35+
var dispatcher = WorkerDispatcher.create(worker);
3436
/* or you can specify worker type */
35-
var dispatcher = WorkerEventDispatcher.create(worker, WorkerEventDispatcher.DEDICATED_WORKER);
37+
var dispatcher = WorkerDispatcher.create(worker, WorkerDispatcher.DEDICATED_WORKER);
3638
```
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.
3840
```javascript
39-
var dispatcher = WorkerEventDispatcher.self();
41+
var dispatcher = WorkerDispatcher.createForSelf();
4042
```
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.
4244
Here Worker will be created from passed URL string:
4345
```javascript
44-
var dispatcher = new WorkerEventDispatcher('/workers/worker.js');
46+
var dispatcher = new WorkerDispatcher('/workers/worker.js');
4547
```
4648

4749
#### 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.
4951
```javascript
5052
var worker = new SharedWorker('/workers/sharedworker.js');
51-
var dispatcher = WorkerEventDispatcher.create(worker, WorkerEventDispatcher.SHARED_WORKER);
53+
var dispatcher = WorkerDispatcher.create(worker, WorkerDispatcher.SHARED_WORKER);
5254
dispatcher.start();
5355
```
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.
5557
```javascript
56-
var dispatcher = WorkerEventDispatcher.self();
57-
dispatcher.addEventListener(WorkerEventDispatcher.WorkerEvent.CONNECT, function(event) {
58+
var dispatcher = WorkerDispatcher.createForSelf();
59+
dispatcher.addEventListener(WorkerDispatcher.WorkerEvent.CONNECT, function(event) {
5860
var client = event.client;
5961
client.addEventListener('data', function(event) {
6062
console.log('new data from client', event.data);
@@ -67,7 +69,7 @@ dispatcher.addEventListener(WorkerEventDispatcher.WorkerEvent.CONNECT, function(
6769
#### Sending and receiving messages
6870
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
6971
```javascript
70-
var dispatcher = new WorkerEventDispatcher('/workers/worker.js');
72+
var dispatcher = new WorkerDispatcher('/workers/worker.js');
7173
dispatcher.addEventListener('anyEvent', function(){
7274
console.log('Event received');
7375
});
@@ -76,28 +78,28 @@ dispatcher.dispatchEvent('anyEvent');
7678
In this case event listener will not be called, but if other side will send `"anyEvent"` event, this listener will be called.
7779
On HTML page:
7880
```javascript
79-
var dispatcher = new WorkerEventDispatcher('/workers/worker.js');
81+
var dispatcher = new WorkerDispatcher('/workers/worker.js');
8082
dispatcher.addEventListener('anyEvent', function(event) {
8183
console.log('Event received');
8284
});
8385
```
8486
Worker code:
8587
```javascript
86-
var dispatcher = WorkerEventDispatcher.self();
88+
var dispatcher = WorkerDispatcher.createForSelf();
8789
dispatcher.dispatchEvent('anyEvent');
8890
```
8991

90-
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.
9193

9294
## API
93-
#### WorkerEventDispatcher constructor arguments
95+
#### WorkerDispatcher constructor arguments
9496
- **worker**:Worker|MessagePort|String - Worker instance or URL string for worker script.
9597
- **receiverEventPreprocessor**:Function - Optional, allows pre-processing of events and their data before firing event.
9698
- **senderEventPreprocessor**:Function - Optional, allows pre-processing of events and their data before passing them to `postMessage`.
9799
- *type?:String - argument used internally to generate type property in prototype.*
98100

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.
101103

102104
- **type**:String - type of the worker
103105
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
107109
- **dispatchEvent**(event:Object):void - does not fire event, it sends event to `postMessage()`. Can be used with two arguments:
108110
- dispatchEvent(eventType:String, data?:Object):void
109111

110-
#### WorkerEventDispatcher static members
112+
#### WorkerDispatcher static members
111113

112114
- **CONNECT_EVENT**:String - Short of `WorkerEvent.CONNECT`. Event fired in Shared Worker script when new client is available.
113115
- **DEDICATED_WORKER**:String - Short of `WorkerType.DEDICATED_WORKER`
114116
- **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.
117119

118120
- WorkerEvent:Object - Worker event types
119121
- 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.
120122
- ERROR:String - Mirroring [error event](https://developer.mozilla.org/en-US/docs/Web/Events/error) fired from WorkerGlobalScope
121123
- LANGUAGECHANGE:String - Mirroring [languagechange event](https://developer.mozilla.org/en-US/docs/Web/Events/languagechange) fired from WorkerGlobalScope
122124
- ONLINE:String - Mirroring [online event](https://developer.mozilla.org/en-US/docs/Web/Events/online) fired from WorkerGlobalScope
123125
- 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
127129
- SHARED_WORKER_SERVER:String - For internal usage, will create ServerEventDispatcher
128130
- 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
131133
- Server:Function - Constructor of ServerEventDispatcher
132134
- Client:Function - Constructor of ClientEventDispatcher
133135

134136

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.
137139

138140
- **terminate**():void - close connection to worker, i.e. destroy worker.
139141

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.
142144
```javascript
143-
var dispatcher = new WorkerEventDispatcher.SharedWorkerEventDispatcher('/workers/sharedworker.js', 'worker-name');
145+
var dispatcher = new WorkerDispatcher.SharedWorkerDispatcher('/workers/sharedworker.js', 'worker-name');
144146
```
145147

146148
#### 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.
148150

149151
#### ClientEventDispatcher
150152
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
155157
```javascript
156158
var _clients = [];
157159
// Create ServerEventDispatcher
158-
var dispatcher = WorkerEventDispatcher.self();
160+
var dispatcher = WorkerDispatcher.createForSelf();
159161
// Listen to incoming connections
160-
dispatcher.addEventListener(WorkerEventDispatcher.WorkerEvent.CONNECT, function(event) {
162+
dispatcher.addEventListener(WorkerDispatcher.WorkerEvent.CONNECT, function(event) {
161163
// Get ClientEventDispatcher of new connection from event, save and start it
162164
var client = event.client;
163165
_clients.push(client);

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "worker-event-dispatcher",
33
"description": "WorkerEventDispatcher is a JavaScript class that adds events layer to Worker API",
4-
"version": "0.0.4",
4+
"version": "1.1.7",
55
"main": [
66
"dist/worker-event-dispatcher.min.js"
77
],

dist/worker-dispatcher.direct.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/worker-dispatcher.direct.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)