@remix_labs/machine-starter 2.2830.0 → 2.2833.0-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/common.js CHANGED
@@ -4,8 +4,10 @@ import { MixcoreTauri, filterFFINames } from "./mixcore.lib.mjs";
4
4
 
5
5
  let workers = {};
6
6
  let children = {};
7
+ let ffiSubscriptions = {};
7
8
 
8
9
  function terminateAll() {
10
+ console.log("[groovebox-starter] terminate all VMs");
9
11
  for (let [vmID, worker] of Object.entries(workers)) {
10
12
  worker.terminate();
11
13
  };
@@ -14,12 +16,16 @@ function terminateAll() {
14
16
  }
15
17
 
16
18
  function terminateVM(vmID) {
19
+ console.log("[groovebox-starter] terminate VM", vmID);
17
20
  let worker = workers[vmID];
18
21
  if (worker) {
19
22
  let vmChildren = children[vmID];
23
+ let ffiSubscription = ffiSubscriptions[vmID];
20
24
  delete workers[vmID];
21
25
  delete children[vmID];
26
+ delete ffiSubscriptions[vmID];
22
27
  worker.terminate();
28
+ ffiSubscription.terminate();
23
29
  if (vmChildren) {
24
30
  for (let subID of vmChildren) {
25
31
  terminateVM(subID)
@@ -35,7 +41,7 @@ if (globalThis.Process) {
35
41
  })
36
42
  };
37
43
 
38
- let initialMask = 64 + 1024; // default for web
44
+ let initialMask = 1024; // default for web
39
45
 
40
46
  globalThis.MixSetMask = (m => {
41
47
  initialMask = m;
@@ -58,14 +64,16 @@ function StartWASM(hub, baseURL, org, workspace, vmID, user, token, noOutputViaM
58
64
  }
59
65
 
60
66
  async function StartWASM2(hub, config) {
61
- console.log("GROOVEBOX_BUILD (machine-starter)", GROOVEBOX_BUILD);
67
+ console.log("[groovebox-starter] start VM", GROOVEBOX_BUILD, config.vmID, config.mixcore);
62
68
  let channel = await hub.newChannel();
63
69
  // injected by either index.js or node.js
64
70
  let worker = globalThis.GetMachineWASMWorker();
71
+ if (workers[config.vmID]) throw new Error("Cannot start VM, the ID is used: " + config.vmID);
65
72
  workers[config.vmID] = worker;
66
73
  let localFFIs = config.localFFIs || {};
67
74
  let mixcoreFFIs = await getMixcoreFFIs(config.mixcore);
68
75
  if (mixcoreFFIs) {
76
+ console.log("[groovebox-starter] mixcore FFIs", Object.keys(mixcoreFFIs));
69
77
  Object.assign(localFFIs, mixcoreFFIs);
70
78
  delete config.mixcore;
71
79
  }
@@ -107,7 +115,7 @@ async function StartWASM2(hub, config) {
107
115
  // (http, https, data)
108
116
  };
109
117
  worker.postMessage(config_msg, [ channel.port ]);
110
- setupLocalFFIs(hub, config, localFFIfuns); // don't await!
118
+ setupLocalFFIs(hub, config, localFFIfuns);
111
119
  return worker;
112
120
  }
113
121
 
@@ -253,7 +261,16 @@ function decode(val) {
253
261
  }
254
262
  }
255
263
 
256
- async function setupLocalFFIs(hub, config, localFFIs) {
264
+ // set the ffiSubscription and spawn the messaging loop
265
+ function setupLocalFFIs(hub, config, localFFIs) {
266
+ let vmID = config.vmID;
267
+ let terminate = Promise.withResolvers();
268
+ if (ffiSubscriptions[vmID]) ffiSubscriptions[vmID].terminate();
269
+ ffiSubscriptions[vmID] = {terminate: terminate.resolve};
270
+ localFFIsMessageLoop(hub, config, localFFIs, terminate.promise); // don't wait
271
+ }
272
+
273
+ async function localFFIsMessageLoop(hub, config, localFFIs, terminate) {
257
274
  let vmID = config.vmID;
258
275
  let channel = await hub.newChannel();
259
276
  let comm = new FFIComm(vmID, hub, channel);
@@ -261,13 +278,21 @@ async function setupLocalFFIs(hub, config, localFFIs) {
261
278
  await channel.setLocalPubTopic("/local/" + vmID);
262
279
  let sub = await channel.subscribe("/local/" + vmID + "/ffi/call");
263
280
  while (true) {
264
- let msg = await sub.next();
281
+ let {msg, terminated} = await Promise.race([
282
+ sub.next().then((msg) => ({msg})),
283
+ terminate.then(() => ({terminated: true})),
284
+ ]);
285
+ if (terminated) {
286
+ console.log("[groovebox-starter] terminate FFI loop", vmID);
287
+ return;
288
+ }
265
289
  let name = msg.payload.name;
266
290
  let call_id = msg.payload.call_id;
267
291
  let args = decode(msg.payload.args);
268
292
  let fun = localFFIs[name];
293
+ console.log("[groovebox-starter] local FFI call", vmID, call_id, name);
269
294
  try {
270
- if (!fun) throw new Error("no such local FFI: " + name);
295
+ if (!fun) throw new Error("No such local FFI in groovebox starter: " + name);
271
296
  let connector =
272
297
  { call_id: call_id,
273
298
  hub: hub,
@@ -373,8 +398,7 @@ async function getMixcoreFFIs(config) {
373
398
  return localFFIs;
374
399
  }
375
400
 
376
- // use worker.terminate() to shut a worker down!
377
-
401
+ // use terminateVM to shut a worker down!
378
402
  export { StartWASM, StartWASM2, terminateAll, terminateVM,
379
403
  Token, Case, Opaque
380
404
  }
@@ -1,2 +1,2 @@
1
- var GROOVEBOX_BUILD = "3109";
1
+ var GROOVEBOX_BUILD = "3114";
2
2
  export { GROOVEBOX_BUILD }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remix_labs/machine-starter",
3
- "version": "2.2830.0",
3
+ "version": "2.2833.0-dev",
4
4
  "description": "start the groove",
5
5
  "main": "node.js",
6
6
  "browser": "index.js",
@@ -11,7 +11,7 @@
11
11
  "author": "Remixlabs staff",
12
12
  "license": "ISC",
13
13
  "dependencies": {
14
- "@remix_labs/hub-client": "2.2830.0",
14
+ "@remix_labs/hub-client": "2.2833.0-dev",
15
15
  "nanoid": "^5.0.2",
16
16
  "web-worker": "^1.2.0"
17
17
  },