uupaa.thread.js

0.0.26 • Public • Published

Thread.js Build Status

npm

Thread and ThreadPool functions.

This module made of WebModule.

Documentation

Browser, NW.js and Electron

// main.html
 
<script src="<module-dir>/lib/WebModule.js"></script>
<script src="<module-dir>/lib/Thread.js"></script>
<script>
 
var thread = new WebModule.Thread("worker.js", function postMessageHandler(args) {
        ;
    }, function(exitCode) {
 
        switch (exitCode) {
        case Thread.EXIT_OK:      console.log("Closed."); break;
        case Thread.EXIT_ERROR:   console.log("Terminates with an error from WorkerThread. " + errorMessage); break;
        case Thread.EXIT_FORCE:   console.log("Forced termination by user."); break;
        case Thread.EXIT_TIMEOUT: console.log("Watchdog barked.");
        }
    });
 
thread.post(["HELLO", 123], null, function postbackMessageHandler(args) {
    console.log(args[0]); // "HELLO WORLD"
    thread.close();
});
 
</script>

WebWorkers

// worker.js
 
importScripts("<module-dir>/lib/WebModule.js");
importScripts("<module-dir>/lib/ThreadProxy.js");
 
var thread = new WebModule.ThreadProxy(function postMessageHandler(args, event) {
        console.log(args[0]); // "HELLO"
        console.log(args[1]); // 123
 
        event.postback(args[0] + " WORLD"); // call to postbackMessageHandler
 
    }, function closeRequestHandler(yes, ng) {
        // .... destruction process...
 
        yes(); // -> closed as usual.
    });
 

Package Sidebar

Install

npm i uupaa.thread.js

Weekly Downloads

0

Version

0.0.26

License

MIT

Last publish

Collaborators

  • uupaa