@lvce-editor/ipc 6.0.0 → 7.0.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 (3) hide show
  1. package/dist/browser.js +109 -166
  2. package/dist/index.js +91 -114
  3. package/package.json +1 -1
package/dist/browser.js CHANGED
@@ -1,7 +1,28 @@
1
- const getData$2 = event => {
1
+ const getData$1 = event => {
2
2
  return event.data;
3
3
  };
4
4
 
5
+ const attachEvent = (rawIpc, getData, that) => {
6
+ const wrapped = event => {
7
+ const data = getData(event);
8
+ that.dispatchEvent(new MessageEvent('message', {
9
+ data
10
+ }));
11
+ };
12
+ if ('onmessage' in rawIpc) {
13
+ rawIpc.onmessage = wrapped;
14
+ } else if ('on' in rawIpc) {
15
+ rawIpc.on('message', wrapped);
16
+ }
17
+ };
18
+ class Ipc extends EventTarget {
19
+ constructor(rawIpc) {
20
+ super();
21
+ attachEvent(rawIpc, this.getData, this);
22
+ this._rawIpc = rawIpc;
23
+ }
24
+ }
25
+
5
26
  const readyMessage = 'ready';
6
27
 
7
28
  const listen$2 = () => {
@@ -14,43 +35,27 @@ const listen$2 = () => {
14
35
  const signal$1 = global => {
15
36
  global.postMessage(readyMessage);
16
37
  };
38
+ class IpcChildWithModuleWorker extends Ipc {
39
+ getData(event) {
40
+ return getData$1(event);
41
+ }
42
+ send(message) {
43
+ // @ts-ignore
44
+ this._rawIpc.postMessage(message);
45
+ }
46
+ sendAndTransfer(message, transfer) {
47
+ // @ts-ignore
48
+ this._rawIpc.postMessage(message, transfer);
49
+ }
50
+ dispose() {
51
+ // ignore
52
+ }
53
+ }
17
54
  const wrap$4 = global => {
18
- return {
19
- global,
20
- /**
21
- * @type {any}
22
- */
23
- listener: undefined,
24
- send(message) {
25
- this.global.postMessage(message);
26
- },
27
- sendAndTransfer(message, transferables) {
28
- this.global.postMessage(message, transferables);
29
- },
30
- get onmessage() {
31
- return this.listener;
32
- },
33
- set onmessage(listener) {
34
- const wrappedListener = event => {
35
- const data = getData$2(event);
36
- // @ts-expect-error
37
- listener({
38
- data,
39
- target: this
40
- });
41
- };
42
- this.listener = listener;
43
- this.global.onmessage = wrappedListener;
44
- },
45
- dispose() {
46
- // @ts-ignore
47
- this.listener = null;
48
- this.global.onmessage = null;
49
- }
50
- };
55
+ return new IpcChildWithModuleWorker(global);
51
56
  };
52
57
 
53
- const IpcChildWithModuleWorker = {
58
+ const IpcChildWithModuleWorker$1 = {
54
59
  __proto__: null,
55
60
  listen: listen$2,
56
61
  signal: signal$1,
@@ -303,94 +308,58 @@ const listen$1 = async () => {
303
308
  }
304
309
  return globalThis;
305
310
  };
306
- const wrap$3 = port => {
307
- return {
308
- port,
309
- /**
310
- * @type {any}
311
- */
312
- wrappedListener: undefined,
313
- send(message) {
314
- this.port.postMessage(message);
315
- },
316
- sendAndTransfer(message, transferables) {
317
- this.port.postMessage(message, transferables);
318
- },
319
- get onmessage() {
320
- return this.wrappedListener;
321
- },
322
- set onmessage(listener) {
323
- if (listener) {
324
- // @ts-expect-error
325
- this.wrappedListener = event => {
326
- const data = getData$2(event);
327
- // @ts-expect-error
328
- listener({
329
- data,
330
- target: this
331
- });
332
- };
333
- } else {
334
- this.wrappedListener = undefined;
335
- }
336
- this.port.onmessage = this.wrappedListener;
311
+ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
312
+ getData(event) {
313
+ return getData$1(event);
314
+ }
315
+ send(message) {
316
+ this._rawIpc.postMessage(message);
317
+ }
318
+ sendAndTransfer(message, transfer) {
319
+ this._rawIpc.postMessage(message, transfer);
320
+ }
321
+ dispose() {
322
+ if (this._rawIpc.close) {
323
+ this._rawIpc.close();
337
324
  }
338
- };
325
+ }
326
+ }
327
+ const wrap$3 = port => {
328
+ return new IpcChildWithModuleWorkerAndMessagePort(port);
339
329
  };
340
330
 
341
- const IpcChildWithModuleWorkerAndMessagePort = {
331
+ const IpcChildWithModuleWorkerAndMessagePort$1 = {
342
332
  __proto__: null,
343
333
  listen: listen$1,
344
334
  wrap: wrap$3
345
335
  };
346
336
 
347
337
  const listen = () => {
348
- // @ts-ignore
349
338
  return window;
350
339
  };
351
340
  const signal = global => {
352
341
  global.postMessage(readyMessage);
353
342
  };
343
+ class IpcChildWithWindow extends Ipc {
344
+ getData(event) {
345
+ return getData$1(event);
346
+ }
347
+ send(message) {
348
+ this._rawIpc.postMessage(message);
349
+ }
350
+ sendAndTransfer(message, transfer) {
351
+ // TODO set diffrent origin
352
+ this._rawIpc.postMessage(message, '*', transfer);
353
+ }
354
+ dispose() {
355
+ // ignore
356
+ }
357
+ }
354
358
  const wrap$2 = window => {
355
- return {
356
- window,
357
- /**
358
- * @type {any}
359
- */
360
- listener: undefined,
361
- get onmessage() {
362
- return this.listener;
363
- },
364
- set onmessage(listener) {
365
- this.listener = listener;
366
- const wrappedListener = event => {
367
- const data = event.data;
368
- if ('method' in data) {
369
- return;
370
- }
371
- // @ts-ignore
372
- listener({
373
- data,
374
- target: this
375
- });
376
- };
377
- this.window.onmessage = wrappedListener;
378
- },
379
- send(message) {
380
- this.window.postMessage(message);
381
- },
382
- sendAndTransfer(message, transfer) {
383
- this.window.postMessage(message, '*', transfer);
384
- },
385
- dispose() {
386
- this.window.onmessage = null;
387
- this.window = undefined;
388
- this.listener = undefined;
389
- }
390
- };
359
+ return new IpcChildWithWindow(window);
391
360
  };
392
361
 
393
- const IpcChildWithWindow = {
362
+ const IpcChildWithWindow$1 = {
394
363
  __proto__: null,
395
364
  listen,
396
365
  signal,
@@ -510,43 +479,32 @@ const create$1 = async ({
510
479
  }
511
480
  return worker;
512
481
  };
513
- const getData$1 = event => {
482
+ const getData = event => {
514
483
  // TODO why are some events not instance of message event?
515
484
  if (event instanceof MessageEvent) {
516
485
  return event.data;
517
486
  }
518
487
  return event;
519
488
  };
489
+ class IpcParentWithModuleWorker extends Ipc {
490
+ getData(event) {
491
+ return getData(event);
492
+ }
493
+ send(message) {
494
+ this._rawIpc.postMessage(message);
495
+ }
496
+ sendAndTransfer(message, transfer) {
497
+ this._rawIpc.postMessage(message, transfer);
498
+ }
499
+ dispose() {
500
+ // igore
501
+ }
502
+ }
520
503
  const wrap$1 = worker => {
521
- let handleMessage;
522
- return {
523
- get onmessage() {
524
- return handleMessage;
525
- },
526
- set onmessage(listener) {
527
- if (listener) {
528
- handleMessage = event => {
529
- const data = getData$1(event);
530
- listener({
531
- data,
532
- target: this
533
- });
534
- };
535
- } else {
536
- handleMessage = null;
537
- }
538
- worker.onmessage = handleMessage;
539
- },
540
- send(message) {
541
- worker.postMessage(message);
542
- },
543
- sendAndTransfer(message, transfer) {
544
- worker.postMessage(message, transfer);
545
- }
546
- };
504
+ return new IpcParentWithModuleWorker(worker);
547
505
  };
548
506
 
549
- const IpcParentWithModuleWorker = {
507
+ const IpcParentWithModuleWorker$1 = {
550
508
  __proto__: null,
551
509
  create: create$1,
552
510
  wrap: wrap$1
@@ -586,43 +544,28 @@ const create = async ({
586
544
  }
587
545
  return webSocket;
588
546
  };
589
- const getData = event => {
590
- return parse(event.data);
591
- };
547
+ class IpcParentWithWebSocket extends Ipc {
548
+ getData(event) {
549
+ return parse(event.data);
550
+ }
551
+ send(message) {
552
+ this._rawIpc.send(stringifyCompact(message));
553
+ }
554
+ sendAndTransfer(message, transfer) {
555
+ throw new Error('sendAndTransfer not supported');
556
+ }
557
+ dispose() {
558
+ this._rawIpc.close();
559
+ }
560
+ }
592
561
  const wrap = webSocket => {
593
- return {
594
- webSocket,
595
- /**
596
- * @type {any}
597
- */
598
- listener: undefined,
599
- get onmessage() {
600
- return this.listener;
601
- },
602
- set onmessage(listener) {
603
- this.listener = listener;
604
- const wrappedListener = event => {
605
- const data = getData(event);
606
- const syntheticEvent = {
607
- data,
608
- target: this
609
- };
610
- // @ts-ignore
611
- listener(syntheticEvent);
612
- };
613
- this.webSocket.onmessage = wrappedListener;
614
- },
615
- send(message) {
616
- const stringifiedMessage = stringifyCompact(message);
617
- this.webSocket.send(stringifiedMessage);
618
- }
619
- };
562
+ return new IpcParentWithWebSocket(webSocket);
620
563
  };
621
564
 
622
- const IpcParentWithWebSocket = {
565
+ const IpcParentWithWebSocket$1 = {
623
566
  __proto__: null,
624
567
  create,
625
568
  wrap
626
569
  };
627
570
 
628
- export { IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow, IpcParentWithModuleWorker, IpcParentWithWebSocket };
571
+ export { IpcChildWithModuleWorker$1 as IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort$1 as IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow$1 as IpcChildWithWindow, IpcParentWithModuleWorker$1 as IpcParentWithModuleWorker, IpcParentWithWebSocket$1 as IpcParentWithWebSocket };
package/dist/index.js CHANGED
@@ -15,44 +15,11 @@ const attachEvent = (rawIpc, getData, that) => {
15
15
  }
16
16
  };
17
17
  class Ipc extends EventTarget {
18
- constructor(rawIpc, getData) {
18
+ constructor(rawIpc) {
19
19
  super();
20
- attachEvent(rawIpc, getData, this);
20
+ attachEvent(rawIpc, this.getData, this);
21
21
  this._rawIpc = rawIpc;
22
22
  }
23
-
24
- /**
25
- * @deprecated use addEventListener instead of getter/setter
26
- */
27
- set onmessage(listener) {
28
- this.addEventListener('message', listener);
29
- }
30
- send(message) {
31
- if ('postMessage' in this._rawIpc) {
32
- this._rawIpc.postMessage(message);
33
- return;
34
- }
35
- if ('send' in this._rawIpc) {
36
- this._rawIpc.send(message);
37
- return;
38
- }
39
- throw new Error('send not supported');
40
- }
41
- sendAndTransfer(message, transfer) {
42
- if ('postMessage' in this._rawIpc) {
43
- this._rawIpc.postMessage(message, transfer);
44
- return;
45
- }
46
- throw new Error('sendAndTransfer not supported');
47
- }
48
- dispose() {
49
- if ('close' in this._rawIpc) {
50
- this._rawIpc.close();
51
- }
52
- if ('kill' in this._rawIpc) {
53
- this._rawIpc.kill();
54
- }
55
- }
56
23
  }
57
24
 
58
25
  const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
@@ -228,11 +195,28 @@ const getActualData$1 = event => {
228
195
  params: [...ports, ...data.params]
229
196
  };
230
197
  };
198
+ class IpcChildWithElectronMessagePort extends Ipc {
199
+ constructor(port) {
200
+ super(port);
201
+ }
202
+ getData(event) {
203
+ return getActualData$1(event);
204
+ }
205
+ send(message) {
206
+ this._rawIpc.postMessage(message);
207
+ }
208
+ sendAndTransfer(message, transfer) {
209
+ this._rawIpc.postMessage(message, transfer);
210
+ }
211
+ dispose() {
212
+ this._rawIpc.close();
213
+ }
214
+ }
231
215
  const wrap$a = messagePort => {
232
- return new Ipc(messagePort, getActualData$1);
216
+ return new IpcChildWithElectronMessagePort(messagePort);
233
217
  };
234
218
 
235
- const IpcChildWithElectronMessagePort = {
219
+ const IpcChildWithElectronMessagePort$1 = {
236
220
  __proto__: null,
237
221
  listen: listen$7,
238
222
  signal: signal$6,
@@ -269,18 +253,32 @@ const listen$6 = () => {
269
253
  const signal$5 = parentPort => {
270
254
  parentPort.postMessage(readyMessage);
271
255
  };
256
+ class IpcChildWithElectronUtilityProcess extends Ipc {
257
+ getData(event) {
258
+ return getUtilityProcessPortData(event);
259
+ }
260
+ send(message) {
261
+ this._rawIpc.postMessage(message);
262
+ }
263
+ sendAndTransfer(message, transfer) {
264
+ this._rawIpc.postMessage(message, transfer);
265
+ }
266
+ dispose() {
267
+ this._rawIpc.close();
268
+ }
269
+ }
272
270
  const wrap$9 = parentPort => {
273
- return new Ipc(parentPort, getUtilityProcessPortData);
271
+ return new IpcChildWithElectronUtilityProcess(parentPort);
274
272
  };
275
273
 
276
- const IpcChildWithElectronUtilityProcess = {
274
+ const IpcChildWithElectronUtilityProcess$1 = {
277
275
  __proto__: null,
278
276
  listen: listen$6,
279
277
  signal: signal$5,
280
278
  wrap: wrap$9
281
279
  };
282
280
 
283
- const getData$1 = event => {
281
+ const getData = event => {
284
282
  return event.data;
285
283
  };
286
284
 
@@ -294,43 +292,27 @@ const listen$5 = () => {
294
292
  const signal$4 = global => {
295
293
  global.postMessage(readyMessage);
296
294
  };
295
+ class IpcChildWithModuleWorker extends Ipc {
296
+ getData(event) {
297
+ return getData(event);
298
+ }
299
+ send(message) {
300
+ // @ts-ignore
301
+ this._rawIpc.postMessage(message);
302
+ }
303
+ sendAndTransfer(message, transfer) {
304
+ // @ts-ignore
305
+ this._rawIpc.postMessage(message, transfer);
306
+ }
307
+ dispose() {
308
+ // ignore
309
+ }
310
+ }
297
311
  const wrap$8 = global => {
298
- return {
299
- global,
300
- /**
301
- * @type {any}
302
- */
303
- listener: undefined,
304
- send(message) {
305
- this.global.postMessage(message);
306
- },
307
- sendAndTransfer(message, transferables) {
308
- this.global.postMessage(message, transferables);
309
- },
310
- get onmessage() {
311
- return this.listener;
312
- },
313
- set onmessage(listener) {
314
- const wrappedListener = event => {
315
- const data = getData$1(event);
316
- // @ts-expect-error
317
- listener({
318
- data,
319
- target: this
320
- });
321
- };
322
- this.listener = listener;
323
- this.global.onmessage = wrappedListener;
324
- },
325
- dispose() {
326
- // @ts-ignore
327
- this.listener = null;
328
- this.global.onmessage = null;
329
- }
330
- };
312
+ return new IpcChildWithModuleWorker(global);
331
313
  };
332
314
 
333
- const IpcChildWithModuleWorker = {
315
+ const IpcChildWithModuleWorker$1 = {
334
316
  __proto__: null,
335
317
  listen: listen$5,
336
318
  signal: signal$4,
@@ -382,42 +364,27 @@ const listen$4 = async () => {
382
364
  }
383
365
  return globalThis;
384
366
  };
385
- const wrap$7 = port => {
386
- return {
387
- port,
388
- /**
389
- * @type {any}
390
- */
391
- wrappedListener: undefined,
392
- send(message) {
393
- this.port.postMessage(message);
394
- },
395
- sendAndTransfer(message, transferables) {
396
- this.port.postMessage(message, transferables);
397
- },
398
- get onmessage() {
399
- return this.wrappedListener;
400
- },
401
- set onmessage(listener) {
402
- if (listener) {
403
- // @ts-expect-error
404
- this.wrappedListener = event => {
405
- const data = getData$1(event);
406
- // @ts-expect-error
407
- listener({
408
- data,
409
- target: this
410
- });
411
- };
412
- } else {
413
- this.wrappedListener = undefined;
414
- }
415
- this.port.onmessage = this.wrappedListener;
367
+ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
368
+ getData(event) {
369
+ return getData(event);
370
+ }
371
+ send(message) {
372
+ this._rawIpc.postMessage(message);
373
+ }
374
+ sendAndTransfer(message, transfer) {
375
+ this._rawIpc.postMessage(message, transfer);
376
+ }
377
+ dispose() {
378
+ if (this._rawIpc.close) {
379
+ this._rawIpc.close();
416
380
  }
417
- };
381
+ }
382
+ }
383
+ const wrap$7 = port => {
384
+ return new IpcChildWithModuleWorkerAndMessagePort(port);
418
385
  };
419
386
 
420
- const IpcChildWithModuleWorkerAndMessagePort = {
387
+ const IpcChildWithModuleWorkerAndMessagePort$1 = {
421
388
  __proto__: null,
422
389
  listen: listen$4,
423
390
  wrap: wrap$7
@@ -842,7 +809,6 @@ const create$2 = async ({
842
809
  }) => {
843
810
  string(path);
844
811
  const actualArgv = ['--ipc-type=electron-utility-process', ...argv];
845
- // @ts-ignore
846
812
  const {
847
813
  utilityProcess
848
814
  } = await import('electron');
@@ -866,14 +832,25 @@ const create$2 = async ({
866
832
  childProcess.stderr.pipe(process.stderr);
867
833
  return childProcess;
868
834
  };
869
- const getData = data => {
870
- return data;
871
- };
835
+ class IpcParentWithElectronUtilityProcess extends Ipc {
836
+ getData(data) {
837
+ return data;
838
+ }
839
+ send(message) {
840
+ this._rawIpc.postMessage(message);
841
+ }
842
+ sendAndTransfer(message, transfer) {
843
+ this._rawIpc.postMessage(message, transfer);
844
+ }
845
+ dispose() {
846
+ this._rawIpc.kill();
847
+ }
848
+ }
872
849
  const wrap$2 = process => {
873
- return new Ipc(process, getData);
850
+ return new IpcParentWithElectronUtilityProcess(process);
874
851
  };
875
852
 
876
- const IpcParentWithElectronUtilityProcess = {
853
+ const IpcParentWithElectronUtilityProcess$1 = {
877
854
  __proto__: null,
878
855
  create: create$2,
879
856
  wrap: wrap$2
@@ -1143,4 +1120,4 @@ const IpcParentWithNodeWorker = {
1143
1120
  wrap
1144
1121
  };
1145
1122
 
1146
- export { IpcChildWithElectronMessagePort, IpcChildWithElectronUtilityProcess, IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort, IpcChildWithNodeForkedProcess, IpcChildWithNodeMessagePort, IpcChildWithNodeWorker, IpcChildWithWebSocket, IpcParentWithElectronUtilityProcess, IpcParentWithNodeForkedProcess, IpcParentWithNodeWorker };
1123
+ export { IpcChildWithElectronMessagePort$1 as IpcChildWithElectronMessagePort, IpcChildWithElectronUtilityProcess$1 as IpcChildWithElectronUtilityProcess, IpcChildWithModuleWorker$1 as IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort$1 as IpcChildWithModuleWorkerAndMessagePort, IpcChildWithNodeForkedProcess, IpcChildWithNodeMessagePort, IpcChildWithNodeWorker, IpcChildWithWebSocket, IpcParentWithElectronUtilityProcess$1 as IpcParentWithElectronUtilityProcess, IpcParentWithNodeForkedProcess, IpcParentWithNodeWorker };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/ipc",
3
- "version": "6.0.0",
3
+ "version": "7.0.0",
4
4
  "description": "Inter Process Communication for Lvce Editor",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",