Skip to content

Commit a27de47

Browse files
committed
Updates README to describe the API.
1 parent 9028730 commit a27de47

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,46 @@ thread.js
44
Parallel Computing with Javascript
55
---
66

7-
This branch experiments with a completly rewritten API and internal structure for parallel.js.
7+
thread.js allows you to spawn webworkers without any hassle in node.js and in the webbrowser.
8+
The API is really simple:
89

9-
For details on the implementation look at this [issue](https://github.com/Sebmaster/parallel.js/issues/1).
10+
var thread = new Thread(data, opts)
11+
---
12+
This initializes a new thread.js instance (note: not a webworker). ```data``` is any data you want to work with in your operations.
13+
In node.js data has to be ```JSON.serialize()```able, in a webbrowser, the object you want to transmit, has to be a transferable.
14+
15+
The following options are available:
16+
17+
* path
18+
19+
default value: ```(isNode ? __dirname + '/' : '') + 'eval.js'```
20+
21+
22+
This is the path to the eval.js file on your webserver (webbrowser-only).
23+
24+
* maxWorkers
25+
26+
default value: ```isNode ? require('os').cpus().length : 4```
27+
28+
The maximum of threads you want to use simultaneously.
29+
30+
Note that every function call returns a promise!
31+
32+
.spawn(fn)
33+
---
34+
```.spawn``` spawns a single webworker with your given function as an argument.
35+
The function will receive one parameter with the data which is currently held by the thread.js instance.
36+
The value returned from spawn will be stored in the thread.js instance after execution in the worker.
37+
38+
.map(fn)
39+
---
40+
```.map``` spawns one worker per element in the ```data```-array up to the given ```maxWorkers``` option.
41+
The function given to map will receive a single element of the array and the returned value will be stored at that index of the array again.
42+
43+
.then(success, fail)
44+
---
45+
The functions given to ```.then``` are called after the last requested operation has finished.
46+
```success``` receives the resulting data object, while ```fail``` will receive an error object.
1047

1148
### Code Style
1249

0 commit comments

Comments
 (0)