@lvce-editor/ipc 9.4.0 → 9.5.1

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.
Files changed (2) hide show
  1. package/dist/browser.js +117 -17
  2. package/package.json +2 -2
package/dist/browser.js CHANGED
@@ -26,14 +26,14 @@ class Ipc extends EventTarget {
26
26
 
27
27
  const readyMessage = 'ready';
28
28
 
29
- const listen$3 = () => {
29
+ const listen$4 = () => {
30
30
  // @ts-ignore
31
31
  if (typeof WorkerGlobalScope === 'undefined') {
32
32
  throw new TypeError('module is not in web worker scope');
33
33
  }
34
34
  return globalThis;
35
35
  };
36
- const signal$2 = global => {
36
+ const signal$3 = global => {
37
37
  global.postMessage(readyMessage);
38
38
  };
39
39
  class IpcChildWithModuleWorker extends Ipc {
@@ -58,15 +58,15 @@ class IpcChildWithModuleWorker extends Ipc {
58
58
  this._rawIpc.addEventListener('message', callback);
59
59
  }
60
60
  }
61
- const wrap$5 = global => {
61
+ const wrap$6 = global => {
62
62
  return new IpcChildWithModuleWorker(global);
63
63
  };
64
64
 
65
65
  const IpcChildWithModuleWorker$1 = {
66
66
  __proto__: null,
67
- listen: listen$3,
68
- signal: signal$2,
69
- wrap: wrap$5
67
+ listen: listen$4,
68
+ signal: signal$3,
69
+ wrap: wrap$6
70
70
  };
71
71
 
72
72
  const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
@@ -294,10 +294,10 @@ const waitForFirstMessage = async port => {
294
294
  return event.data;
295
295
  };
296
296
 
297
- const listen$2 = async () => {
298
- const parentIpcRaw = listen$3();
299
- signal$2(parentIpcRaw);
300
- const parentIpc = wrap$5(parentIpcRaw);
297
+ const listen$3 = async () => {
298
+ const parentIpcRaw = listen$4();
299
+ signal$3(parentIpcRaw);
300
+ const parentIpc = wrap$6(parentIpcRaw);
301
301
  const firstMessage = await waitForFirstMessage(parentIpc);
302
302
  if (firstMessage.method !== 'initialize') {
303
303
  throw new IpcError('unexpected first message');
@@ -336,20 +336,20 @@ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
336
336
  this._rawIpc.start();
337
337
  }
338
338
  }
339
- const wrap$4 = port => {
339
+ const wrap$5 = port => {
340
340
  return new IpcChildWithModuleWorkerAndMessagePort(port);
341
341
  };
342
342
 
343
343
  const IpcChildWithModuleWorkerAndMessagePort$1 = {
344
344
  __proto__: null,
345
- listen: listen$2,
346
- wrap: wrap$4
345
+ listen: listen$3,
346
+ wrap: wrap$5
347
347
  };
348
348
 
349
- const listen$1 = () => {
349
+ const listen$2 = () => {
350
350
  return window;
351
351
  };
352
- const signal$1 = global => {
352
+ const signal$2 = global => {
353
353
  global.postMessage(readyMessage);
354
354
  };
355
355
  class IpcChildWithWindow extends Ipc {
@@ -382,11 +382,111 @@ class IpcChildWithWindow extends Ipc {
382
382
  this._rawIpc.addEventListener('message', wrapped);
383
383
  }
384
384
  }
385
- const wrap$3 = window => {
385
+ const wrap$4 = window => {
386
386
  return new IpcChildWithWindow(window);
387
387
  };
388
388
 
389
389
  const IpcChildWithWindow$1 = {
390
+ __proto__: null,
391
+ listen: listen$2,
392
+ signal: signal$2,
393
+ wrap: wrap$4
394
+ };
395
+
396
+ const isTransferrable = value => {
397
+ return value instanceof MessagePort;
398
+ };
399
+
400
+ const UntransferrableValue = {};
401
+
402
+ const walkValue = (value, transferrables) => {
403
+ if (!value) {
404
+ return value;
405
+ }
406
+ if (isTransferrable(value)) {
407
+ transferrables.push(value);
408
+ return UntransferrableValue;
409
+ }
410
+ if (Array.isArray(value)) {
411
+ const newItems = [];
412
+ for (const item of value) {
413
+ const newItem = walkValue(item, transferrables);
414
+ if (newItem !== UntransferrableValue) {
415
+ newItems.push(newItem);
416
+ }
417
+ }
418
+ return newItems;
419
+ }
420
+ if (typeof value === 'object') {
421
+ const newObject = Object.create(null);
422
+ for (const [key, property] of Object.entries(value)) {
423
+ const newValue = walkValue(property, transferrables);
424
+ if (newValue !== UntransferrableValue) {
425
+ newObject[key] = newValue;
426
+ }
427
+ }
428
+ return newObject;
429
+ }
430
+ return value;
431
+ };
432
+
433
+ // workaround for electron not supporting transferrable objects
434
+ // as parameters. If the transferrable object is a parameter, in electron
435
+ // only an empty objected is received in the main process
436
+ const fixElectronParameters = value => {
437
+ const transfer = [];
438
+ const newValue = walkValue(value, transfer);
439
+ return {
440
+ newValue,
441
+ transfer
442
+ };
443
+ };
444
+
445
+ const listen$1 = () => {
446
+ return window;
447
+ };
448
+ const signal$1 = global => {
449
+ global.postMessage(readyMessage);
450
+ };
451
+ class IpcChildWithElectronWindow extends Ipc {
452
+ getData(event) {
453
+ return getData$1(event);
454
+ }
455
+ send(message) {
456
+ this._rawIpc.postMessage(message);
457
+ }
458
+ sendAndTransfer(message, _transfer) {
459
+ const {
460
+ newValue,
461
+ transfer
462
+ } = fixElectronParameters(message);
463
+ this._rawIpc.postMessage(newValue, location.origin, transfer);
464
+ }
465
+ dispose() {
466
+ // ignore
467
+ }
468
+ onClose(callback) {
469
+ // ignore
470
+ }
471
+ onMessage(callback) {
472
+ const wrapped = event => {
473
+ const {
474
+ ports
475
+ } = event;
476
+ if (ports.length) {
477
+ return;
478
+ }
479
+ callback(event);
480
+ this._rawIpc.removeEventListener('message', wrapped);
481
+ };
482
+ this._rawIpc.addEventListener('message', wrapped);
483
+ }
484
+ }
485
+ const wrap$3 = window => {
486
+ return new IpcChildWithElectronWindow(window);
487
+ };
488
+
489
+ const IpcChildWithElectronWindow$1 = {
390
490
  __proto__: null,
391
491
  listen: listen$1,
392
492
  signal: signal$1,
@@ -650,4 +750,4 @@ const IpcParentWithWebSocket$1 = {
650
750
  wrap
651
751
  };
652
752
 
653
- export { IpcChildWithMessagePort$1 as IpcChildWithMessagePort, IpcChildWithModuleWorker$1 as IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort$1 as IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow$1 as IpcChildWithWindow, IpcParentWithModuleWorker$1 as IpcParentWithModuleWorker, IpcParentWithWebSocket$1 as IpcParentWithWebSocket };
753
+ export { IpcChildWithElectronWindow$1 as IpcChildWithElectronWindow, IpcChildWithMessagePort$1 as IpcChildWithMessagePort, IpcChildWithModuleWorker$1 as IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort$1 as IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow$1 as IpcChildWithWindow, IpcParentWithModuleWorker$1 as IpcParentWithModuleWorker, IpcParentWithWebSocket$1 as IpcParentWithWebSocket };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/ipc",
3
- "version": "9.4.0",
3
+ "version": "9.5.1",
4
4
  "description": "Inter Process Communication for Lvce Editor",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@lvce-editor/assert": "^1.2.0",
18
- "@lvce-editor/verror": "^1.3.0",
18
+ "@lvce-editor/verror": "^1.4.0",
19
19
  "@lvce-editor/web-socket-server": "^1.2.0"
20
20
  },
21
21
  "engines": {