@@ -14,57 +14,65 @@ The returned web worker runs it's own angular context which allows it to resolve
14
14
##Installation
15
15
16
16
install with bower using:
17
- <pre ><code >
18
- bower install angular-workers
19
- </pre ></code >
17
+
18
+ bower install angular-workers
20
19
21
20
##How to use
22
21
23
- 1 . Depend on the WorkerService.
24
- 2 . Specify the URL to the file containing the angular script by invoking:
22
+ * Depend on the WorkerService.
23
+ * Specify the URL to the file containing the angular script by invoking:
24
+
25
+
26
+ // The URL must be absolute because of the URL blob specification
27
+ WorkerService.setAngularUrl(url)
28
+
29
+
30
+ * OPTIONALLY: Specify how the web worker is to find any dependencies by invoking:
25
31
26
- <pre ><code >
27
- // The URL must be absolute because of the URL blob specification
28
- WorkerService.setAngularUrl(url)
29
- <pre ><code >
30
32
31
- 3. OPTIONALLY: Specify how the web worker is to find any dependencies by invoking:
33
+ // The URL must be absolute because of the URL blob specification
34
+ WorkerService.addDependency(serviceName, moduleName, url)
32
35
33
- <pre ><code >
34
- // The URL must be absolute because of the URL blob specification
35
- WorkerService.addDependency(serviceName, moduleName, url)
36
- </pre ></code >
37
36
38
- 4 . Create create a promise of an angularWorker by invoking:
37
+ * Create create a promise of an angularWorker by invoking:
38
+
39
39
40
- <pre ><code >
41
40
var workerPromise = WorkerService.createAngularWorker([ 'input', 'output' /* additional optional deps* /,
42
-   ;  ; function(input, output /*additional optional deps*/) {
43
-   ;  ;  ;  ; // This contains the worker body
44
-   ;  ;  ;  ; // The function must be self contained (the function body will be converted to source)
45
-   ;  ;  ;  ; // The input paramter is what will be passed to the worker when it is to be executed, it must be a serializable object
46
-   ;  ;  ;  ; // The output parameter is a promise and is logically what the worker will return to the main thread.
47
-   ;  ;  ;  ; // All communication from the worker to the main thread is performed by resolving, rejecting or notifying the output promise
48
-   ;  ;  ;  ; // We may optionally depend on other angular services. These services can be used just as in the main thread.
49
-   ;  ; }]);
50
- </code ></pre >
51
-
52
- 5 . When the workerPromise resolves the worker is initialized with it's own angular context and is ready to use. Like so:
53
- <pre ><code >
41
+ function(input, output /* additional optional deps* /) {
42
+ // This contains the worker body.
43
+ // The function must be self contained. The function body will be
44
+ // converted to source and passed to the worker.
45
+ // The input parameter is what will be passed to the worker when
46
+ // it is executed. It must be a serializable object.
47
+ // The output parameter is a promise and is what the
48
+ // worker will return to the main thread.
49
+ // All communication from the worker to the main thread is performed
50
+ // by resolving, rejecting or notifying the output promise.
51
+ // We may optionally depend on other angular services.
52
+ // These services can be used just as in the main thread.
53
+ // But be aware that no state changes in the angular services in the
54
+ // worker are propagates to the main thread. Workers run in fully isolated
55
+ // contexts. All communication must be performed through the output parameter.
56
+ }] );
57
+
58
+
59
+ * When the workerPromise resolves the worker is initialized with it's own angular context and is ready to use. Like so:
60
+
61
+
54
62
workerPromise.then(function success(angularWorker) {
55
- & nbsp ;& nbsp ;& nbsp ;& nbsp ; //The input must be serializable
56
- & nbsp ;& nbsp ;& nbsp ;& nbsp ; return angularWorker.run(inputObject);
57
- & nbsp ;& nbsp ; }, function error(reason) {
58
- & nbsp ;& nbsp ;& nbsp ;& nbsp ; //for some reason the worker failed to initialize
59
- & nbsp ;& nbsp ;& nbsp ;& nbsp ; //not all browsers support the HTML5 tech that is required, see below.
60
- & nbsp ;& nbsp ; }).then(function success(result) {
61
- & nbsp ;& nbsp ;& nbsp ;& nbsp ; //handle result
62
- & nbsp ;& nbsp ; }, function error(reason) {
63
- & nbsp ;& nbsp ;& nbsp ;& nbsp ; //handle error
64
- & nbsp ;& nbsp ; }, function notify(update) {
65
- & nbsp ;& nbsp ;& nbsp ;& nbsp ; //handle update
66
- & nbsp ;& nbsp ; });
67
- </ pre ></ code >
63
+ //The input must be serializable
64
+ return angularWorker.run(inputObject);
65
+ }, function error(reason) {
66
+ //for some reason the worker failed to initialize
67
+ //not all browsers support the HTML5 tech that is required, see below.
68
+ }).then(function success(result) {
69
+ //handle result
70
+ }, function error(reason) {
71
+ //handle error
72
+ }, function notify(update) {
73
+ //handle update
74
+ });
75
+
68
76
69
77
The same initialized worker can be used many times with different input.
70
78
0 commit comments