add auto function binding

This commit is contained in:
Stewart Allen 2021-12-27 18:13:11 -05:00
commit 4acded19aa
4 changed files with 42 additions and 10 deletions

6
app.js
View file

@ -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"
]
};

View file

@ -4,4 +4,7 @@
(function() {
})();
// start worker pool
moto.client.start(`/code/mesh_pool?${gapp.version}`, moto.client.max());
})();

View file

@ -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";
}
};

View file

@ -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) => {