@lvce-editor/ipc 9.4.0 → 9.5.0

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 +109 -15
  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,16 +336,106 @@ 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
+ __proto__: null,
345
+ listen: listen$3,
346
+ wrap: wrap$5
347
+ };
348
+
349
+ const listen$2 = () => {
350
+ return window;
351
+ };
352
+ const signal$2 = global => {
353
+ global.postMessage(readyMessage);
354
+ };
355
+ let IpcChildWithWindow$1 = class IpcChildWithWindow extends Ipc {
356
+ getData(event) {
357
+ return getData$1(event);
358
+ }
359
+ send(message) {
360
+ this._rawIpc.postMessage(message);
361
+ }
362
+ sendAndTransfer(message, transfer) {
363
+ this._rawIpc.postMessage(message, location.origin, transfer);
364
+ }
365
+ dispose() {
366
+ // ignore
367
+ }
368
+ onClose(callback) {
369
+ // ignore
370
+ }
371
+ onMessage(callback) {
372
+ const wrapped = event => {
373
+ const {
374
+ ports
375
+ } = event;
376
+ if (ports.length) {
377
+ return;
378
+ }
379
+ callback(event);
380
+ this._rawIpc.removeEventListener('message', wrapped);
381
+ };
382
+ this._rawIpc.addEventListener('message', wrapped);
383
+ }
384
+ };
385
+ const wrap$4 = window => {
386
+ return new IpcChildWithWindow$1(window);
387
+ };
388
+
389
+ const IpcChildWithWindow$2 = {
344
390
  __proto__: null,
345
391
  listen: listen$2,
392
+ signal: signal$2,
346
393
  wrap: wrap$4
347
394
  };
348
395
 
396
+ const isTransferrable = value => {
397
+ return value instanceof MessagePort;
398
+ };
399
+
400
+ const walkValue = (value, transferrables) => {
401
+ if (!value) {
402
+ return value;
403
+ }
404
+ if (isTransferrable(value)) {
405
+ transferrables.push(value);
406
+ return undefined;
407
+ }
408
+ if (Array.isArray(value)) {
409
+ const newItems = [];
410
+ for (const item of value) {
411
+ const newItem = walkValue(item, transferrables);
412
+ newItems.push(newItem);
413
+ }
414
+ return newItems;
415
+ }
416
+ if (typeof value === 'object') {
417
+ const newObject = Object.create(null);
418
+ for (const [key, property] of Object.entries(value)) {
419
+ const newValue = walkValue(property, transferrables);
420
+ newObject[key] = newValue;
421
+ }
422
+ return newObject;
423
+ }
424
+ return value;
425
+ };
426
+
427
+ // workaround for electron not supporting transferrable objects
428
+ // as parameters. If the transferrable object is a parameter, in electron
429
+ // only an empty objected is received in the main process
430
+ const fixElectronParameters = value => {
431
+ const transfer = [];
432
+ const newValue = walkValue(value, transfer);
433
+ return {
434
+ newValue,
435
+ transfer
436
+ };
437
+ };
438
+
349
439
  const listen$1 = () => {
350
440
  return window;
351
441
  };
@@ -359,8 +449,12 @@ class IpcChildWithWindow extends Ipc {
359
449
  send(message) {
360
450
  this._rawIpc.postMessage(message);
361
451
  }
362
- sendAndTransfer(message, transfer) {
363
- this._rawIpc.postMessage(message, location.origin, transfer);
452
+ sendAndTransfer(message, _transfer) {
453
+ const {
454
+ newValue,
455
+ transfer
456
+ } = fixElectronParameters(message);
457
+ this._rawIpc.postMessage(newValue, location.origin, transfer);
364
458
  }
365
459
  dispose() {
366
460
  // ignore
@@ -386,7 +480,7 @@ const wrap$3 = window => {
386
480
  return new IpcChildWithWindow(window);
387
481
  };
388
482
 
389
- const IpcChildWithWindow$1 = {
483
+ const IpcChildWithElectronWindow = {
390
484
  __proto__: null,
391
485
  listen: listen$1,
392
486
  signal: signal$1,
@@ -650,4 +744,4 @@ const IpcParentWithWebSocket$1 = {
650
744
  wrap
651
745
  };
652
746
 
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 };
747
+ export { IpcChildWithElectronWindow, IpcChildWithMessagePort$1 as IpcChildWithMessagePort, IpcChildWithModuleWorker$1 as IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort$1 as IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow$2 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.0",
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": {