SugarWorkerWebWorker 语法糖
SugarWorker —— 轻松的使用web多线程技术;
webWorker
var lsitWorker = new Worker('core/listWorker.js'); lsitWorker.onmessage = function(e){ if(e.data){ listWorker.postMessage('new test data'); } ...(response) console.log(e); lsitWorker.terminate(); }; listWorker.onerror = function(e){ ...(response) console.error(e); }; listWorker.postMessage('test data');
SugarWorker
work('core/listWorker.js') .back(function(e){ if(e.data){ e.post('new test data'); } ...(response) console.log(e); e.end(); }) .err(function(e){ ...(response) console.error(e); }) .post('test data');
SugarWorker还支持事件定义,方便监听另一条线程的状态
work('core/test.js') .back(function(e){ console.log(e); }) .set('loading',function(e){ console.log('loading'); console.log(e); }) .post('test data');
//test.js onmessage = function(e){ postMessage({ eventType : 'loading', data : {...} //some datas }); }
通过worker线程返回对象,添加eventType属性,可以在主线程触发定义的事件;
添加eventType的返回数据,不会触发主线程back方法;
更多使用方法请查看:https://github.com/kirakiray/SugarWorker
评论