Skip to content

Commit 9118aea

Browse files
author
fresa
committed
updated the readme
1 parent a59f97a commit 9118aea

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,76 @@
11
# angular-workers
2+
3+
> A wrapper for web workers in angular
4+
5+
##Why?
6+
Using web workers is somewhat awkward in raw Javascript. Doing it in angular applications even more so.
7+
Each web worker runs in it's own context, and this context is isolated from the angular application.
8+
9+
##What does angular-workers do
10+
angular-workers provides an angular service which upon request creates a web worker.
11+
The returned web worker runs it's own angular context which allows it to resolve angular dependencies.
12+
13+
##How to use
14+
1. Depend on the WorkerService.
15+
2. Specify the URL to the file containing the angular script by invoking:
16+
<pre><code>
17+
// The URL must be absolute because of the URL blob specification
18+
WorkerService.setAngularUrl(url)
19+
</pre></code>
20+
3. OPTIONALLY: Specify how the web worker is to find any dependencies by invoking:
21+
<pre><code>
22+
// The URL must be absolute because of the URL blob specification
23+
WorkerService.addDependency(serviceName, moduleName, url)
24+
</pre></code>
25+
4. Create create a promise of an angularWorker by invoking:
26+
<pre><code>
27+
var workerPromise = WorkerService.createAngularWorker(['input', 'output' /*additional optional deps*/,
28+
&nbsp;&nbsp;function(input, output /*additional optional deps*/) {
29+
&nbsp;&nbsp;&nbsp;&nbsp;// This contains the worker body
30+
&nbsp;&nbsp;&nbsp;&nbsp;// The function must be self contained (the function body will be converted to source)
31+
&nbsp;&nbsp;&nbsp;&nbsp;// The input paramter is what will be passed to the worker when it is to be executed, it must be a serializable object
32+
&nbsp;&nbsp;&nbsp;&nbsp;// The output parameter is a promise and is logically what the worker will return to the main thread.
33+
&nbsp;&nbsp;&nbsp;&nbsp;// All communication from the worker to the main thread is performed by resolving, rejecting or notifying the output promise
34+
&nbsp;&nbsp;&nbsp;&nbsp;// We may optionally depend on other angular services. These services can be used just as in the main thread.
35+
&nbsp;&nbsp;}]);
36+
</code></pre>
37+
5. When the workerPromise resolves the worker is initialized with it's own angular context and is ready to use. Like so:
38+
<pre><code>
39+
workerPromise.then(function success(angularWorker) {
40+
&nbsp;&nbsp;&nbsp;&nbsp;//The input must be serializable
41+
&nbsp;&nbsp;&nbsp;&nbsp;return angularWorker.run(inputObject);
42+
&nbsp;&nbsp;}, function error(reason) {
43+
&nbsp;&nbsp;&nbsp;&nbsp;//for some reason the worker failed to initialize
44+
&nbsp;&nbsp;&nbsp;&nbsp;//not all browsers support the HTML5 tech that is required, see below.
45+
&nbsp;&nbsp;}).then(function success(result) {
46+
&nbsp;&nbsp;&nbsp;&nbsp;//handle result
47+
&nbsp;&nbsp;}, function error(reason) {
48+
&nbsp;&nbsp;&nbsp;&nbsp;//handle error
49+
&nbsp;&nbsp;}, function notify(update) {
50+
&nbsp;&nbsp;&nbsp;&nbsp;//handle update
51+
&nbsp;&nbsp;});
52+
</pre></code>
53+
54+
The same initialized worker can be used many times with different input.
55+
56+
##Requirements
57+
The browser running the angular service needs to support the following:
58+
* [Web Workers](http://caniuse.com/#feat=webworkers) (angular-workers does not use shared workers)
59+
* [Blob URLs](http://caniuse.com/#feat=bloburls), specifically [creating blobs from strings](https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL#Browser_compatibility)
60+
61+
##Limitations
62+
The angular-workers is a wrapper around standard web workers. So all limitations with web workers apply.
63+
* Data sent between the worker and main thread is deep cloned. (angular-workers does not use transferable objects, yet)
64+
This means transferring large object (about >20Mb, [Communicating Large Objects with Web Workers in javascript, Samuel Mendenhall](http://developerblog.redhat.com/2014/05/20/communicating-large-objects-with-web-workers-in-javascript/))
65+
will cause noticeable delays. Samuel Mendenhall recommends sending the data in chunks. This can be achieved using the notify
66+
in the angular promise.
67+
* There is no DOM in the worker. Other things are missing as well. No global "document" object. The bare minimum of these
68+
have been mocked to allow angular to start in the worker.
69+
* The web worker share no runtime data with the main thread. This is great since it prevents deadlock, starvation and many
70+
other concurrency issues. But it also means that any angular service instance upon which your worker depends is created
71+
in that worker, and not shared with the main thread. <b> One can not communicate data between worker and main thread
72+
by using service states. All communication must be done through the input object and output promise.</b>
73+
* Running in a separate context means the web worker does not share the cookies set in the main thread! If you depend on
74+
cookies for authentication pass these manually to the worker.
75+
76+

0 commit comments

Comments
 (0)