summaryrefslogtreecommitdiff
path: root/node_modules/webworker-threads/examples/ex04_main.js
diff options
context:
space:
mode:
authoryo mama <pepper@scannerjammer.com>2015-04-04 01:00:59 -0700
committeryo mama <pepper@scannerjammer.com>2015-04-04 01:00:59 -0700
commitc7c22e3db1c826bcfb2bc66651ec480aae0d4ae0 (patch)
tree8546df448afef40d3814d2581f4dacff7cebb87f /node_modules/webworker-threads/examples/ex04_main.js
Diffstat (limited to 'node_modules/webworker-threads/examples/ex04_main.js')
-rw-r--r--node_modules/webworker-threads/examples/ex04_main.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/node_modules/webworker-threads/examples/ex04_main.js b/node_modules/webworker-threads/examples/ex04_main.js
new file mode 100644
index 0000000..be8ca7c
--- /dev/null
+++ b/node_modules/webworker-threads/examples/ex04_main.js
@@ -0,0 +1,40 @@
+/// !example
+/// ## Loading the worker code from a file (main side)
+///
+/// When writing code for threads we have both code that executes in the main thread and code that
+/// executes in worker threads. When the code is small, it may be handy to have all code in a single file
+/// but it is often clearer to split the code apart when the worker code starts to grow.
+///
+/// This example demonstrates how we can package the worker code in a separeate file and
+/// load it with `t.load`.
+///
+/// We are going to keep the same logic as in our previous 'ping pong' example, but just repackage
+/// it slightly differently.
+///
+/// In this file we keep only the ping (main) side:
+// Creating the worker thread
+var Threads = require('webworker-threads');
+var t = Threads.create();
+
+// Listening to 'data' events from the worker thread
+t.on('data', function(n, result) {
+ console.log('fibo(' + n + ') = ' + result);
+ if (n < 40) t.emit('next');
+ else console.log('bye!'), t.destroy();
+});
+/// At this point we load the worker code:
+t.load(__dirname + '/ex04_worker.js');
+/// And we start the game by emitting the first `next` event:
+t.emit('next');
+/// ### Output
+///
+/// ```
+/// fibo(1) = 1
+/// fibo(2) = 2
+/// fibo(3) = 3
+/// fibo(4) = 5
+/// ...
+/// fibo(39) = 102334155
+/// fibo(40) = 165580141
+/// bye!
+/// ``` \ No newline at end of file