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