From 4acded19aa33e1c3d765fafb90560c560560c9c0 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Mon, 27 Dec 2021 18:13:11 -0500 Subject: [PATCH] add auto function binding --- app.js | 6 ++++++ src/main/mesh-work.js | 5 ++++- src/moto/client.js | 38 ++++++++++++++++++++++++++++++-------- src/moto/worker.js | 3 ++- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/app.js b/app.js index 408cea4e..4b8a7e04 100644 --- a/app.js +++ b/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" ] }; diff --git a/src/main/mesh-work.js b/src/main/mesh-work.js index 4509d061..929295e9 100644 --- a/src/main/mesh-work.js +++ b/src/main/mesh-work.js @@ -4,4 +4,7 @@ (function() { -})(); \ No newline at end of file + // start worker pool + moto.client.start(`/code/mesh_pool?${gapp.version}`, moto.client.max()); + +})(); diff --git a/src/moto/client.js b/src/moto/client.js index b15c79f9..2ff9a228 100644 --- a/src/moto/client.js +++ b/src/moto/client.js @@ -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 { + bind: (name, fn) => { endpoints[name] = fn; + dispatch.send({bind: name}); }, send: (msg) => {