@lvce-editor/ipc 4.0.1 → 4.1.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 +104 -20
- package/dist/index.js +16 -5
- package/package.json +1 -1
package/dist/browser.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const getData$
|
|
1
|
+
const getData$2 = event => {
|
|
2
2
|
return event.data;
|
|
3
3
|
};
|
|
4
4
|
|
|
@@ -14,7 +14,7 @@ const listen$2 = () => {
|
|
|
14
14
|
const signal$1 = global => {
|
|
15
15
|
global.postMessage(readyMessage);
|
|
16
16
|
};
|
|
17
|
-
const wrap$
|
|
17
|
+
const wrap$4 = global => {
|
|
18
18
|
return {
|
|
19
19
|
global,
|
|
20
20
|
/**
|
|
@@ -32,7 +32,7 @@ const wrap$3 = global => {
|
|
|
32
32
|
},
|
|
33
33
|
set onmessage(listener) {
|
|
34
34
|
const wrappedListener = event => {
|
|
35
|
-
const data = getData$
|
|
35
|
+
const data = getData$2(event);
|
|
36
36
|
// @ts-expect-error
|
|
37
37
|
listener({
|
|
38
38
|
data,
|
|
@@ -54,7 +54,7 @@ const IpcChildWithModuleWorker = {
|
|
|
54
54
|
__proto__: null,
|
|
55
55
|
listen: listen$2,
|
|
56
56
|
signal: signal$1,
|
|
57
|
-
wrap: wrap$
|
|
57
|
+
wrap: wrap$4
|
|
58
58
|
};
|
|
59
59
|
|
|
60
60
|
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
@@ -290,7 +290,7 @@ const waitForFirstMessage = async port => {
|
|
|
290
290
|
const listen$1 = async () => {
|
|
291
291
|
const parentIpcRaw = listen$2();
|
|
292
292
|
signal$1(parentIpcRaw);
|
|
293
|
-
const parentIpc = wrap$
|
|
293
|
+
const parentIpc = wrap$4(parentIpcRaw);
|
|
294
294
|
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
295
295
|
if (firstMessage.method !== 'initialize') {
|
|
296
296
|
throw new IpcError('unexpected first message');
|
|
@@ -303,7 +303,7 @@ const listen$1 = async () => {
|
|
|
303
303
|
}
|
|
304
304
|
return globalThis;
|
|
305
305
|
};
|
|
306
|
-
const wrap$
|
|
306
|
+
const wrap$3 = port => {
|
|
307
307
|
return {
|
|
308
308
|
port,
|
|
309
309
|
/**
|
|
@@ -323,7 +323,7 @@ const wrap$2 = port => {
|
|
|
323
323
|
if (listener) {
|
|
324
324
|
// @ts-expect-error
|
|
325
325
|
this.wrappedListener = event => {
|
|
326
|
-
const data = getData$
|
|
326
|
+
const data = getData$2(event);
|
|
327
327
|
// @ts-expect-error
|
|
328
328
|
listener({
|
|
329
329
|
data,
|
|
@@ -341,7 +341,7 @@ const wrap$2 = port => {
|
|
|
341
341
|
const IpcChildWithModuleWorkerAndMessagePort = {
|
|
342
342
|
__proto__: null,
|
|
343
343
|
listen: listen$1,
|
|
344
|
-
wrap: wrap$
|
|
344
|
+
wrap: wrap$3
|
|
345
345
|
};
|
|
346
346
|
|
|
347
347
|
const listen = () => {
|
|
@@ -351,7 +351,7 @@ const listen = () => {
|
|
|
351
351
|
const signal = global => {
|
|
352
352
|
global.postMessage(readyMessage);
|
|
353
353
|
};
|
|
354
|
-
const wrap$
|
|
354
|
+
const wrap$2 = window => {
|
|
355
355
|
return {
|
|
356
356
|
window,
|
|
357
357
|
/**
|
|
@@ -394,35 +394,46 @@ const IpcChildWithWindow = {
|
|
|
394
394
|
__proto__: null,
|
|
395
395
|
listen,
|
|
396
396
|
signal,
|
|
397
|
-
wrap: wrap$
|
|
397
|
+
wrap: wrap$2
|
|
398
398
|
};
|
|
399
399
|
|
|
400
400
|
const Message = 'message';
|
|
401
401
|
const Error$1 = 'error';
|
|
402
402
|
|
|
403
|
+
const addListener = (emitter, type, callback) => {
|
|
404
|
+
if ('addEventListener' in emitter) {
|
|
405
|
+
emitter.addEventListener(type, callback);
|
|
406
|
+
} else {
|
|
407
|
+
emitter.on(type, callback);
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
const removeListener = (emitter, type, callback) => {
|
|
411
|
+
if ('removeEventListener' in emitter) {
|
|
412
|
+
emitter.removeEventListener(type, callback);
|
|
413
|
+
} else {
|
|
414
|
+
emitter.off(type, callback);
|
|
415
|
+
}
|
|
416
|
+
};
|
|
403
417
|
const getFirstEvent = (eventEmitter, eventMap) => {
|
|
404
418
|
const {
|
|
405
419
|
resolve,
|
|
406
420
|
promise
|
|
407
421
|
} = withResolvers();
|
|
408
422
|
const listenerMap = Object.create(null);
|
|
409
|
-
// @ts-ignore
|
|
410
423
|
const cleanup = value => {
|
|
411
424
|
for (const event of Object.keys(eventMap)) {
|
|
412
|
-
eventEmitter
|
|
425
|
+
removeListener(eventEmitter, event, listenerMap[event]);
|
|
413
426
|
}
|
|
414
|
-
// @ts-ignore
|
|
415
427
|
resolve(value);
|
|
416
428
|
};
|
|
417
429
|
for (const [event, type] of Object.entries(eventMap)) {
|
|
418
|
-
// @ts-ignore
|
|
419
430
|
const listener = event => {
|
|
420
431
|
cleanup({
|
|
421
432
|
type,
|
|
422
433
|
event
|
|
423
434
|
});
|
|
424
435
|
};
|
|
425
|
-
eventEmitter
|
|
436
|
+
addListener(eventEmitter, event, listener);
|
|
426
437
|
listenerMap[event] = listener;
|
|
427
438
|
}
|
|
428
439
|
return promise;
|
|
@@ -470,7 +481,7 @@ ${relevant}`;
|
|
|
470
481
|
|
|
471
482
|
const Module = 'module';
|
|
472
483
|
|
|
473
|
-
const create = async ({
|
|
484
|
+
const create$1 = async ({
|
|
474
485
|
url,
|
|
475
486
|
name
|
|
476
487
|
}) => {
|
|
@@ -499,14 +510,14 @@ const create = async ({
|
|
|
499
510
|
}
|
|
500
511
|
return worker;
|
|
501
512
|
};
|
|
502
|
-
const getData = event => {
|
|
513
|
+
const getData$1 = event => {
|
|
503
514
|
// TODO why are some events not instance of message event?
|
|
504
515
|
if (event instanceof MessageEvent) {
|
|
505
516
|
return event.data;
|
|
506
517
|
}
|
|
507
518
|
return event;
|
|
508
519
|
};
|
|
509
|
-
const wrap = worker => {
|
|
520
|
+
const wrap$1 = worker => {
|
|
510
521
|
let handleMessage;
|
|
511
522
|
return {
|
|
512
523
|
get onmessage() {
|
|
@@ -515,7 +526,7 @@ const wrap = worker => {
|
|
|
515
526
|
set onmessage(listener) {
|
|
516
527
|
if (listener) {
|
|
517
528
|
handleMessage = event => {
|
|
518
|
-
const data = getData(event);
|
|
529
|
+
const data = getData$1(event);
|
|
519
530
|
listener({
|
|
520
531
|
data,
|
|
521
532
|
target: this
|
|
@@ -536,9 +547,82 @@ const wrap = worker => {
|
|
|
536
547
|
};
|
|
537
548
|
|
|
538
549
|
const IpcParentWithModuleWorker = {
|
|
550
|
+
__proto__: null,
|
|
551
|
+
create: create$1,
|
|
552
|
+
wrap: wrap$1
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
const Open = 1;
|
|
556
|
+
const Close = 2;
|
|
557
|
+
|
|
558
|
+
const stringifyCompact = value => {
|
|
559
|
+
return JSON.stringify(value);
|
|
560
|
+
};
|
|
561
|
+
const parse = content => {
|
|
562
|
+
if (content === 'undefined') {
|
|
563
|
+
return null;
|
|
564
|
+
}
|
|
565
|
+
try {
|
|
566
|
+
return JSON.parse(content);
|
|
567
|
+
} catch (error) {
|
|
568
|
+
throw new VError(error, 'failed to parse json');
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
|
|
572
|
+
const waitForWebSocketToBeOpen = webSocket => {
|
|
573
|
+
return getFirstEvent(webSocket, {
|
|
574
|
+
open: Open,
|
|
575
|
+
close: Close
|
|
576
|
+
});
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
const create = async ({
|
|
580
|
+
webSocket
|
|
581
|
+
}) => {
|
|
582
|
+
const firstWebSocketEvent = await waitForWebSocketToBeOpen(webSocket);
|
|
583
|
+
// @ts-ignore
|
|
584
|
+
if (firstWebSocketEvent.type === Close) {
|
|
585
|
+
throw new IpcError('Websocket connection was immediately closed');
|
|
586
|
+
}
|
|
587
|
+
return webSocket;
|
|
588
|
+
};
|
|
589
|
+
const getData = event => {
|
|
590
|
+
return parse(event.data);
|
|
591
|
+
};
|
|
592
|
+
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
|
+
};
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
const IpcParentWithWebSocket = {
|
|
539
623
|
__proto__: null,
|
|
540
624
|
create,
|
|
541
625
|
wrap
|
|
542
626
|
};
|
|
543
627
|
|
|
544
|
-
export { IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow, IpcParentWithModuleWorker };
|
|
628
|
+
export { IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow, IpcParentWithModuleWorker, IpcParentWithWebSocket };
|
package/dist/index.js
CHANGED
|
@@ -603,29 +603,40 @@ const IpcChildWithNodeWorker = {
|
|
|
603
603
|
const Open = 1;
|
|
604
604
|
const Close = 2;
|
|
605
605
|
|
|
606
|
+
const addListener = (emitter, type, callback) => {
|
|
607
|
+
if ('addEventListener' in emitter) {
|
|
608
|
+
emitter.addEventListener(type, callback);
|
|
609
|
+
} else {
|
|
610
|
+
emitter.on(type, callback);
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
const removeListener = (emitter, type, callback) => {
|
|
614
|
+
if ('removeEventListener' in emitter) {
|
|
615
|
+
emitter.removeEventListener(type, callback);
|
|
616
|
+
} else {
|
|
617
|
+
emitter.off(type, callback);
|
|
618
|
+
}
|
|
619
|
+
};
|
|
606
620
|
const getFirstEvent = (eventEmitter, eventMap) => {
|
|
607
621
|
const {
|
|
608
622
|
resolve,
|
|
609
623
|
promise
|
|
610
624
|
} = withResolvers();
|
|
611
625
|
const listenerMap = Object.create(null);
|
|
612
|
-
// @ts-ignore
|
|
613
626
|
const cleanup = value => {
|
|
614
627
|
for (const event of Object.keys(eventMap)) {
|
|
615
|
-
eventEmitter
|
|
628
|
+
removeListener(eventEmitter, event, listenerMap[event]);
|
|
616
629
|
}
|
|
617
|
-
// @ts-ignore
|
|
618
630
|
resolve(value);
|
|
619
631
|
};
|
|
620
632
|
for (const [event, type] of Object.entries(eventMap)) {
|
|
621
|
-
// @ts-ignore
|
|
622
633
|
const listener = event => {
|
|
623
634
|
cleanup({
|
|
624
635
|
type,
|
|
625
636
|
event
|
|
626
637
|
});
|
|
627
638
|
};
|
|
628
|
-
eventEmitter
|
|
639
|
+
addListener(eventEmitter, event, listener);
|
|
629
640
|
listenerMap[event] = listener;
|
|
630
641
|
}
|
|
631
642
|
return promise;
|