add auto function binding
This commit is contained in:
parent
ea830a681a
commit
4acded19aa
4 changed files with 42 additions and 10 deletions
6
app.js
6
app.js
|
|
@ -449,6 +449,7 @@ const script = {
|
|||
"main/mesh-ui"
|
||||
],
|
||||
mesh_work : [
|
||||
"moto/client",
|
||||
"moto/worker",
|
||||
"moto/license",
|
||||
"ext/jszip",
|
||||
|
|
@ -470,6 +471,11 @@ const script = {
|
|||
"moto/broker",
|
||||
"moto/idb",
|
||||
"main/mesh-work"
|
||||
],
|
||||
mesh_pool : [
|
||||
"moto/worker",
|
||||
"moto/license",
|
||||
"main/mesh-pool"
|
||||
]
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -4,4 +4,7 @@
|
|||
|
||||
(function() {
|
||||
|
||||
})();
|
||||
// start worker pool
|
||||
moto.client.start(`/code/mesh_pool?${gapp.version}`, moto.client.max());
|
||||
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -19,10 +19,28 @@ if (moto.client) return;
|
|||
// code is running in the browser / client context
|
||||
let client = moto.client = {
|
||||
|
||||
active: () => {
|
||||
live: () => {
|
||||
return workers.length;
|
||||
},
|
||||
|
||||
free: () => {
|
||||
return workers.filter(w => w.run === null).length;
|
||||
},
|
||||
|
||||
max: () => {
|
||||
return ccmax;
|
||||
},
|
||||
|
||||
fn: {},
|
||||
|
||||
bind: (task) => {
|
||||
if (!client.fn[task]) {
|
||||
client.fn[task] = (data, options) => {
|
||||
return client.call(task, data, options);
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
call: (task, data, options = {}) => {
|
||||
let { direct, stream } = options;
|
||||
return new Promise((resolve, reject) => {
|
||||
|
|
@ -131,9 +149,9 @@ let client = moto.client = {
|
|||
},
|
||||
|
||||
// same as restart
|
||||
start: function(url = workurl, cc = workcc) {
|
||||
start: function(url = workurl, cc = workcc || 1) {
|
||||
workcc = cc;
|
||||
worknum = cc ? ccmax : 1;
|
||||
worknum = Math.min(ccmax, workcc);
|
||||
workurl = url;
|
||||
client.restart(url, cc);
|
||||
},
|
||||
|
|
@ -150,16 +168,20 @@ let client = moto.client = {
|
|||
|
||||
for (let i=0; i<worknum; i++) {
|
||||
let worker = client.spawn(workurl);
|
||||
|
||||
worker.run = null;
|
||||
worker.index = i;
|
||||
worker.onmessage = function(e) {
|
||||
let reply = e.data;
|
||||
if (reply.bind) {
|
||||
client.bind(reply.bind);
|
||||
return;
|
||||
}
|
||||
|
||||
if (worker.run === null) {
|
||||
console.log({unhandled: e});
|
||||
console.log({client_unhandled: e});
|
||||
throw "message on idle worker";
|
||||
}
|
||||
|
||||
let reply = e.data;
|
||||
let { done, data, error } = reply;
|
||||
let { ondone, ondata, onerror } = worker.run;
|
||||
let handled = false;
|
||||
|
|
@ -184,8 +206,8 @@ let client = moto.client = {
|
|||
}
|
||||
|
||||
if (!handled) {
|
||||
console.log({unhandled: e});
|
||||
throw "unhandled reply";
|
||||
console.log({client_unhandled: e});
|
||||
throw "client unhandled reply";
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -18,8 +18,9 @@ let endpoints = {};
|
|||
// code is running in the worker / server context
|
||||
const dispatch = moto.worker = {
|
||||
|
||||
register: (name, fn) => {
|
||||
bind: (name, fn) => {
|
||||
endpoints[name] = fn;
|
||||
dispatch.send({bind: name});
|
||||
},
|
||||
|
||||
send: (msg) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue