1.0.0 • Published 6 years ago

webworkio v1.0.0

Weekly downloads
6
License
ISC
Repository
github
Last release
6 years ago

Webworkio

Purpose

This class helps you deal with web-workers in a way which looks like the way socket.io does with websockets.

The purpose of this class is to greatly simplify the use of web workers and to mimic socket.io's callback feature.

Basically, an application emits a message to a webworker, providing a callback function (CB). The webworker will call CB after completing its task. The application will then be notify back and receive any data passed as argument to CB.

example

Application.js

class Application {
	run() {
	    this.wwio = new Webworkio();
    	this.wwio.worker('./myworker.js');
    	this.wwio.emit('test', {msg: 'hello'}, function(result) {
			console.log('web worker initialized', result);
		});
	}
}

myworker.js

const wwio = new Webworkio();
wwio.worker();
wwio.on('test', function ({msg}, cb) {
  if (msg === 'hello') {
    cb('hello world');
  } else {
    cb('');
  }
});