@lvce-editor/ipc 7.1.1 → 7.2.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.
package/dist/browser.js CHANGED
@@ -2,24 +2,25 @@ 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);
5
+ const attachEvents = that => {
6
+ const handleMessage = event => {
7
+ const data = that.getData(event);
8
8
  that.dispatchEvent(new MessageEvent('message', {
9
9
  data
10
10
  }));
11
11
  };
12
- if ('onmessage' in rawIpc) {
13
- rawIpc.onmessage = wrapped;
14
- } else if ('on' in rawIpc) {
15
- rawIpc.on('message', wrapped);
16
- }
12
+ that.onMessage(handleMessage);
13
+ const handleClose = event => {
14
+ that.dispatchEvent(new Event('close'));
15
+ };
16
+ that.onClose(handleClose);
17
17
  };
18
+
18
19
  class Ipc extends EventTarget {
19
20
  constructor(rawIpc) {
20
21
  super();
21
- attachEvent(rawIpc, this.getData, this);
22
22
  this._rawIpc = rawIpc;
23
+ attachEvents(this);
23
24
  }
24
25
  }
25
26
 
@@ -50,6 +51,12 @@ class IpcChildWithModuleWorker extends Ipc {
50
51
  dispose() {
51
52
  // ignore
52
53
  }
54
+ onClose(callback) {
55
+ // ignore
56
+ }
57
+ onMessage(callback) {
58
+ this._rawIpc.addEventListener('message', callback);
59
+ }
53
60
  }
54
61
  const wrap$4 = global => {
55
62
  return new IpcChildWithModuleWorker(global);
@@ -323,6 +330,12 @@ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
323
330
  this._rawIpc.close();
324
331
  }
325
332
  }
333
+ onClose(callback) {
334
+ // ignore
335
+ }
336
+ onMessage(callback) {
337
+ this._rawIpc.addEventListener('message', callback);
338
+ }
326
339
  }
327
340
  const wrap$3 = port => {
328
341
  return new IpcChildWithModuleWorkerAndMessagePort(port);
@@ -348,12 +361,17 @@ class IpcChildWithWindow extends Ipc {
348
361
  this._rawIpc.postMessage(message);
349
362
  }
350
363
  sendAndTransfer(message, transfer) {
351
- // TODO set diffrent origin
352
- this._rawIpc.postMessage(message, '*', transfer);
364
+ this._rawIpc.postMessage(message, location.origin, transfer);
353
365
  }
354
366
  dispose() {
355
367
  // ignore
356
368
  }
369
+ onClose(callback) {
370
+ // ignore
371
+ }
372
+ onMessage(callback) {
373
+ this._rawIpc.addEventListener('message', callback);
374
+ }
357
375
  }
358
376
  const wrap$2 = window => {
359
377
  return new IpcChildWithWindow(window);
@@ -497,7 +515,13 @@ class IpcParentWithModuleWorker extends Ipc {
497
515
  this._rawIpc.postMessage(message, transfer);
498
516
  }
499
517
  dispose() {
500
- // igore
518
+ // ignore
519
+ }
520
+ onClose(callback) {
521
+ // ignore
522
+ }
523
+ onMessage(callback) {
524
+ this._rawIpc.addEventListener('message', callback);
501
525
  }
502
526
  }
503
527
  const wrap$1 = worker => {
@@ -557,6 +581,12 @@ class IpcParentWithWebSocket extends Ipc {
557
581
  dispose() {
558
582
  this._rawIpc.close();
559
583
  }
584
+ onClose(callback) {
585
+ this._rawIpc.addEventListener('close', callback);
586
+ }
587
+ onMessage(callback) {
588
+ this._rawIpc.addEventListener('message', callback);
589
+ }
560
590
  }
561
591
  const wrap = webSocket => {
562
592
  return new IpcParentWithWebSocket(webSocket);
package/dist/index.d.ts CHANGED
@@ -1,8 +1,6 @@
1
- interface Ipc {
1
+ interface Ipc extends EventTarget {
2
2
  readonly send: (message: any) => void
3
3
  readonly sendAndTransfer: (message: any, transfer: any) => void
4
- readonly on: any
5
- readonly onmessage: any
6
4
  readonly dispose: () => void
7
5
  readonly isDisposed: () => void
8
6
  }
package/dist/index.js CHANGED
@@ -1,24 +1,25 @@
1
1
  import { VError } from '@lvce-editor/verror';
2
2
  import { string, array } from '@lvce-editor/assert';
3
3
 
4
- const attachEvent = (rawIpc, getData, that) => {
5
- const wrapped = event => {
6
- const data = getData(event);
4
+ const attachEvents = that => {
5
+ const handleMessage = event => {
6
+ const data = that.getData(event);
7
7
  that.dispatchEvent(new MessageEvent('message', {
8
8
  data
9
9
  }));
10
10
  };
11
- if ('onmessage' in rawIpc) {
12
- rawIpc.onmessage = wrapped;
13
- } else if ('on' in rawIpc) {
14
- rawIpc.on('message', wrapped);
15
- }
11
+ that.onMessage(handleMessage);
12
+ const handleClose = event => {
13
+ that.dispatchEvent(new Event('close'));
14
+ };
15
+ that.onClose(handleClose);
16
16
  };
17
+
17
18
  class Ipc extends EventTarget {
18
19
  constructor(rawIpc) {
19
20
  super();
20
- attachEvent(rawIpc, this.getData, this);
21
21
  this._rawIpc = rawIpc;
22
+ attachEvents(this);
22
23
  }
23
24
  }
24
25
 
@@ -211,6 +212,12 @@ class IpcChildWithElectronMessagePort extends Ipc {
211
212
  dispose() {
212
213
  this._rawIpc.close();
213
214
  }
215
+ onMessage(callback) {
216
+ this._rawIpc.on('message', callback);
217
+ }
218
+ onClose(callback) {
219
+ this._rawIpc.on('close', callback);
220
+ }
214
221
  }
215
222
  const wrap$a = messagePort => {
216
223
  return new IpcChildWithElectronMessagePort(messagePort);
@@ -266,6 +273,12 @@ class IpcChildWithElectronUtilityProcess extends Ipc {
266
273
  dispose() {
267
274
  this._rawIpc.close();
268
275
  }
276
+ onClose(callback) {
277
+ this._rawIpc.on('close', callback);
278
+ }
279
+ onMessage(callback) {
280
+ this._rawIpc.on('message', callback);
281
+ }
269
282
  }
270
283
  const wrap$9 = parentPort => {
271
284
  return new IpcChildWithElectronUtilityProcess(parentPort);
@@ -307,6 +320,12 @@ class IpcChildWithModuleWorker extends Ipc {
307
320
  dispose() {
308
321
  // ignore
309
322
  }
323
+ onClose(callback) {
324
+ // ignore
325
+ }
326
+ onMessage(callback) {
327
+ this._rawIpc.addEventListener('message', callback);
328
+ }
310
329
  }
311
330
  const wrap$8 = global => {
312
331
  return new IpcChildWithModuleWorker(global);
@@ -379,6 +398,12 @@ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
379
398
  this._rawIpc.close();
380
399
  }
381
400
  }
401
+ onClose(callback) {
402
+ // ignore
403
+ }
404
+ onMessage(callback) {
405
+ this._rawIpc.addEventListener('message', callback);
406
+ }
382
407
  }
383
408
  const wrap$7 = port => {
384
409
  return new IpcChildWithModuleWorkerAndMessagePort(port);
@@ -845,6 +870,12 @@ class IpcParentWithElectronUtilityProcess extends Ipc {
845
870
  dispose() {
846
871
  this._rawIpc.kill();
847
872
  }
873
+ onClose(callback) {
874
+ this._rawIpc.on('exit', callback);
875
+ }
876
+ onMessage(callback) {
877
+ this._rawIpc.on('message', callback);
878
+ }
848
879
  }
849
880
  const wrap$2 = process => {
850
881
  return new IpcParentWithElectronUtilityProcess(process);
@@ -1012,6 +1043,12 @@ class IpcParentWithNodeForkedProcess extends Ipc {
1012
1043
  dispose() {
1013
1044
  this._rawIpc.kill();
1014
1045
  }
1046
+ onClose(callback) {
1047
+ this._rawIpc.on('close', callback);
1048
+ }
1049
+ onMessage(callback) {
1050
+ this._rawIpc.on('message', callback);
1051
+ }
1015
1052
  }
1016
1053
  const wrap$1 = childProcess => {
1017
1054
  return new IpcParentWithNodeForkedProcess(childProcess);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/ipc",
3
- "version": "7.1.1",
3
+ "version": "7.2.0",
4
4
  "description": "Inter Process Communication for Lvce Editor",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",