@lvce-editor/opener-worker 1.0.0 → 1.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/README.md +1 -1
- package/dist/openerWorkerMain.js +183 -34
- package/package.json +2 -2
package/README.md
CHANGED
package/dist/openerWorkerMain.js
CHANGED
|
@@ -368,10 +368,104 @@ const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
|
368
368
|
listen: listen$6,
|
|
369
369
|
wrap: wrap$e
|
|
370
370
|
};
|
|
371
|
+
const addListener = (emitter, type, callback) => {
|
|
372
|
+
if ('addEventListener' in emitter) {
|
|
373
|
+
emitter.addEventListener(type, callback);
|
|
374
|
+
} else {
|
|
375
|
+
emitter.on(type, callback);
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
const removeListener = (emitter, type, callback) => {
|
|
379
|
+
if ('removeEventListener' in emitter) {
|
|
380
|
+
emitter.removeEventListener(type, callback);
|
|
381
|
+
} else {
|
|
382
|
+
emitter.off(type, callback);
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
const getFirstEvent = (eventEmitter, eventMap) => {
|
|
386
|
+
const {
|
|
387
|
+
promise,
|
|
388
|
+
resolve
|
|
389
|
+
} = Promise.withResolvers();
|
|
390
|
+
const listenerMap = Object.create(null);
|
|
391
|
+
const cleanup = value => {
|
|
392
|
+
for (const event of Object.keys(eventMap)) {
|
|
393
|
+
removeListener(eventEmitter, event, listenerMap[event]);
|
|
394
|
+
}
|
|
395
|
+
resolve(value);
|
|
396
|
+
};
|
|
397
|
+
for (const [event, type] of Object.entries(eventMap)) {
|
|
398
|
+
const listener = event => {
|
|
399
|
+
cleanup({
|
|
400
|
+
event,
|
|
401
|
+
type
|
|
402
|
+
});
|
|
403
|
+
};
|
|
404
|
+
addListener(eventEmitter, event, listener);
|
|
405
|
+
listenerMap[event] = listener;
|
|
406
|
+
}
|
|
407
|
+
return promise;
|
|
408
|
+
};
|
|
409
|
+
const Message$1 = 3;
|
|
410
|
+
const create$5$1 = async ({
|
|
411
|
+
isMessagePortOpen,
|
|
412
|
+
messagePort
|
|
413
|
+
}) => {
|
|
414
|
+
if (!isMessagePort(messagePort)) {
|
|
415
|
+
throw new IpcError('port must be of type MessagePort');
|
|
416
|
+
}
|
|
417
|
+
if (isMessagePortOpen) {
|
|
418
|
+
return messagePort;
|
|
419
|
+
}
|
|
420
|
+
const eventPromise = getFirstEvent(messagePort, {
|
|
421
|
+
message: Message$1
|
|
422
|
+
});
|
|
423
|
+
messagePort.start();
|
|
424
|
+
const {
|
|
425
|
+
event,
|
|
426
|
+
type
|
|
427
|
+
} = await eventPromise;
|
|
428
|
+
if (type !== Message$1) {
|
|
429
|
+
throw new IpcError('Failed to wait for ipc message');
|
|
430
|
+
}
|
|
431
|
+
if (event.data !== readyMessage) {
|
|
432
|
+
throw new IpcError('unexpected first message');
|
|
433
|
+
}
|
|
434
|
+
return messagePort;
|
|
435
|
+
};
|
|
436
|
+
const signal$1 = messagePort => {
|
|
437
|
+
messagePort.start();
|
|
438
|
+
};
|
|
439
|
+
class IpcParentWithMessagePort extends Ipc {
|
|
440
|
+
getData = getData$2;
|
|
441
|
+
send(message) {
|
|
442
|
+
this._rawIpc.postMessage(message);
|
|
443
|
+
}
|
|
444
|
+
sendAndTransfer(message) {
|
|
445
|
+
const transfer = getTransferrables(message);
|
|
446
|
+
this._rawIpc.postMessage(message, transfer);
|
|
447
|
+
}
|
|
448
|
+
dispose() {
|
|
449
|
+
this._rawIpc.close();
|
|
450
|
+
}
|
|
451
|
+
onMessage(callback) {
|
|
452
|
+
this._rawIpc.addEventListener('message', callback);
|
|
453
|
+
}
|
|
454
|
+
onClose(callback) {}
|
|
455
|
+
}
|
|
456
|
+
const wrap$5 = messagePort => {
|
|
457
|
+
return new IpcParentWithMessagePort(messagePort);
|
|
458
|
+
};
|
|
459
|
+
const IpcParentWithMessagePort$1 = {
|
|
460
|
+
__proto__: null,
|
|
461
|
+
create: create$5$1,
|
|
462
|
+
signal: signal$1,
|
|
463
|
+
wrap: wrap$5
|
|
464
|
+
};
|
|
371
465
|
|
|
372
466
|
const Two$1 = '2.0';
|
|
373
467
|
const callbacks = Object.create(null);
|
|
374
|
-
const get$
|
|
468
|
+
const get$2 = id => {
|
|
375
469
|
return callbacks[id];
|
|
376
470
|
};
|
|
377
471
|
const remove$1 = id => {
|
|
@@ -520,7 +614,7 @@ const warn = (...args) => {
|
|
|
520
614
|
console.warn(...args);
|
|
521
615
|
};
|
|
522
616
|
const resolve = (id, response) => {
|
|
523
|
-
const fn = get$
|
|
617
|
+
const fn = get$2(id);
|
|
524
618
|
if (!fn) {
|
|
525
619
|
console.log(response);
|
|
526
620
|
warn(`callback ${id} may already be disposed`);
|
|
@@ -570,7 +664,7 @@ const getErrorProperty = (error, prettyError) => {
|
|
|
570
664
|
}
|
|
571
665
|
};
|
|
572
666
|
};
|
|
573
|
-
const create$1
|
|
667
|
+
const create$1 = (id, error) => {
|
|
574
668
|
return {
|
|
575
669
|
jsonrpc: Two$1,
|
|
576
670
|
id,
|
|
@@ -581,9 +675,9 @@ const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
|
581
675
|
const prettyError = preparePrettyError(error);
|
|
582
676
|
logError(error, prettyError);
|
|
583
677
|
const errorProperty = getErrorProperty(error, prettyError);
|
|
584
|
-
return create$1
|
|
678
|
+
return create$1(id, errorProperty);
|
|
585
679
|
};
|
|
586
|
-
const create$
|
|
680
|
+
const create$3 = (message, result) => {
|
|
587
681
|
return {
|
|
588
682
|
jsonrpc: Two$1,
|
|
589
683
|
id: message.id,
|
|
@@ -592,7 +686,7 @@ const create$2 = (message, result) => {
|
|
|
592
686
|
};
|
|
593
687
|
const getSuccessResponse = (message, result) => {
|
|
594
688
|
const resultProperty = result ?? null;
|
|
595
|
-
return create$
|
|
689
|
+
return create$3(message, resultProperty);
|
|
596
690
|
};
|
|
597
691
|
const getErrorResponseSimple = (id, error) => {
|
|
598
692
|
return {
|
|
@@ -706,14 +800,14 @@ const execute = (command, ...args) => {
|
|
|
706
800
|
};
|
|
707
801
|
|
|
708
802
|
const Two = '2.0';
|
|
709
|
-
const create$
|
|
803
|
+
const create$t = (method, params) => {
|
|
710
804
|
return {
|
|
711
805
|
jsonrpc: Two,
|
|
712
806
|
method,
|
|
713
807
|
params
|
|
714
808
|
};
|
|
715
809
|
};
|
|
716
|
-
const create$
|
|
810
|
+
const create$s = (id, method, params) => {
|
|
717
811
|
const message = {
|
|
718
812
|
id,
|
|
719
813
|
jsonrpc: Two,
|
|
@@ -723,14 +817,14 @@ const create$r = (id, method, params) => {
|
|
|
723
817
|
return message;
|
|
724
818
|
};
|
|
725
819
|
let id = 0;
|
|
726
|
-
const create$
|
|
820
|
+
const create$r = () => {
|
|
727
821
|
return ++id;
|
|
728
822
|
};
|
|
729
823
|
|
|
730
824
|
/* eslint-disable n/no-unsupported-features/es-syntax */
|
|
731
825
|
|
|
732
826
|
const registerPromise = map => {
|
|
733
|
-
const id = create$
|
|
827
|
+
const id = create$r();
|
|
734
828
|
const {
|
|
735
829
|
promise,
|
|
736
830
|
resolve
|
|
@@ -748,7 +842,7 @@ const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer)
|
|
|
748
842
|
id,
|
|
749
843
|
promise
|
|
750
844
|
} = registerPromise(callbacks);
|
|
751
|
-
const message = create$
|
|
845
|
+
const message = create$s(id, method, params);
|
|
752
846
|
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
753
847
|
ipc.sendAndTransfer(message);
|
|
754
848
|
} else {
|
|
@@ -784,7 +878,7 @@ const createRpc = ipc => {
|
|
|
784
878
|
* @deprecated
|
|
785
879
|
*/
|
|
786
880
|
send(method, ...params) {
|
|
787
|
-
const message = create$
|
|
881
|
+
const message = create$t(method, params);
|
|
788
882
|
ipc.send(message);
|
|
789
883
|
}
|
|
790
884
|
};
|
|
@@ -820,7 +914,37 @@ const listen$1 = async (module, options) => {
|
|
|
820
914
|
const ipc = module.wrap(rawIpc);
|
|
821
915
|
return ipc;
|
|
822
916
|
};
|
|
823
|
-
const create$
|
|
917
|
+
const create$5 = async ({
|
|
918
|
+
commandMap,
|
|
919
|
+
isMessagePortOpen = true,
|
|
920
|
+
messagePort
|
|
921
|
+
}) => {
|
|
922
|
+
// TODO create a commandMap per rpc instance
|
|
923
|
+
register(commandMap);
|
|
924
|
+
const rawIpc = await IpcParentWithMessagePort$1.create({
|
|
925
|
+
isMessagePortOpen,
|
|
926
|
+
messagePort
|
|
927
|
+
});
|
|
928
|
+
const ipc = IpcParentWithMessagePort$1.wrap(rawIpc);
|
|
929
|
+
handleIpc(ipc);
|
|
930
|
+
const rpc = createRpc(ipc);
|
|
931
|
+
messagePort.start();
|
|
932
|
+
return rpc;
|
|
933
|
+
};
|
|
934
|
+
const create$4 = async ({
|
|
935
|
+
commandMap,
|
|
936
|
+
messagePort
|
|
937
|
+
}) => {
|
|
938
|
+
return create$5({
|
|
939
|
+
commandMap,
|
|
940
|
+
messagePort
|
|
941
|
+
});
|
|
942
|
+
};
|
|
943
|
+
const PlainMessagePortRpcParent = {
|
|
944
|
+
__proto__: null,
|
|
945
|
+
create: create$4
|
|
946
|
+
};
|
|
947
|
+
const create$2 = async ({
|
|
824
948
|
commandMap
|
|
825
949
|
}) => {
|
|
826
950
|
// TODO create a commandMap per rpc instance
|
|
@@ -832,7 +956,7 @@ const create$1 = async ({
|
|
|
832
956
|
};
|
|
833
957
|
const WebWorkerRpcClient = {
|
|
834
958
|
__proto__: null,
|
|
835
|
-
create: create$
|
|
959
|
+
create: create$2
|
|
836
960
|
};
|
|
837
961
|
const createMockRpc = ({
|
|
838
962
|
commandMap
|
|
@@ -862,7 +986,7 @@ const rpcs = Object.create(null);
|
|
|
862
986
|
const set$2 = (id, rpc) => {
|
|
863
987
|
rpcs[id] = rpc;
|
|
864
988
|
};
|
|
865
|
-
const get = id => {
|
|
989
|
+
const get$1 = id => {
|
|
866
990
|
return rpcs[id];
|
|
867
991
|
};
|
|
868
992
|
const remove = id => {
|
|
@@ -873,18 +997,18 @@ const remove = id => {
|
|
|
873
997
|
const create = rpcId => {
|
|
874
998
|
return {
|
|
875
999
|
async dispose() {
|
|
876
|
-
const rpc = get(rpcId);
|
|
1000
|
+
const rpc = get$1(rpcId);
|
|
877
1001
|
await rpc.dispose();
|
|
878
1002
|
},
|
|
879
1003
|
// @ts-ignore
|
|
880
1004
|
invoke(method, ...params) {
|
|
881
|
-
const rpc = get(rpcId);
|
|
1005
|
+
const rpc = get$1(rpcId);
|
|
882
1006
|
// @ts-ignore
|
|
883
1007
|
return rpc.invoke(method, ...params);
|
|
884
1008
|
},
|
|
885
1009
|
// @ts-ignore
|
|
886
1010
|
invokeAndTransfer(method, ...params) {
|
|
887
|
-
const rpc = get(rpcId);
|
|
1011
|
+
const rpc = get$1(rpcId);
|
|
888
1012
|
// @ts-ignore
|
|
889
1013
|
return rpc.invokeAndTransfer(method, ...params);
|
|
890
1014
|
},
|
|
@@ -907,19 +1031,49 @@ const create = rpcId => {
|
|
|
907
1031
|
};
|
|
908
1032
|
|
|
909
1033
|
const {
|
|
1034
|
+
invoke: invoke$1,
|
|
910
1035
|
set: set$1
|
|
911
1036
|
} = create(RendererWorker);
|
|
912
1037
|
|
|
1038
|
+
const handleMessagePort = async (port, rpcId) => {
|
|
1039
|
+
const rpc = await PlainMessagePortRpcParent.create({
|
|
1040
|
+
commandMap: {},
|
|
1041
|
+
messagePort: port
|
|
1042
|
+
});
|
|
1043
|
+
if (rpcId) {
|
|
1044
|
+
set$2(rpcId, rpc);
|
|
1045
|
+
}
|
|
1046
|
+
};
|
|
1047
|
+
|
|
913
1048
|
const openNew = async url => {
|
|
914
1049
|
// TODO
|
|
915
1050
|
};
|
|
916
1051
|
|
|
917
|
-
|
|
1052
|
+
let enabled = false;
|
|
1053
|
+
let text = '';
|
|
1054
|
+
const set = value => {
|
|
1055
|
+
enabled = value;
|
|
1056
|
+
};
|
|
1057
|
+
const get = () => {
|
|
1058
|
+
return enabled;
|
|
1059
|
+
};
|
|
1060
|
+
const writeUrl = value => {
|
|
1061
|
+
text = value;
|
|
1062
|
+
};
|
|
1063
|
+
const readUrl = () => {
|
|
1064
|
+
return text;
|
|
1065
|
+
};
|
|
1066
|
+
|
|
1067
|
+
const invoke = async (method, ...params) => {
|
|
918
1068
|
// TODO
|
|
919
1069
|
};
|
|
920
1070
|
|
|
921
|
-
const openExternal$1 = url => {
|
|
922
|
-
|
|
1071
|
+
const openExternal$1 = async url => {
|
|
1072
|
+
if (get()) {
|
|
1073
|
+
writeUrl(url);
|
|
1074
|
+
return;
|
|
1075
|
+
}
|
|
1076
|
+
return invoke('OpenExternal.openExternal', url);
|
|
923
1077
|
};
|
|
924
1078
|
|
|
925
1079
|
const OpenExternal = {
|
|
@@ -929,13 +1083,10 @@ const OpenExternal = {
|
|
|
929
1083
|
|
|
930
1084
|
const platform = 0;
|
|
931
1085
|
|
|
932
|
-
const invoke = async (method, ...params) => {
|
|
933
|
-
// TODO
|
|
934
|
-
};
|
|
935
|
-
|
|
936
1086
|
const openUrlWeb = async url => {
|
|
937
1087
|
try {
|
|
938
|
-
|
|
1088
|
+
// TODO make platform argument required
|
|
1089
|
+
await invoke$1('Open.openUrl', url);
|
|
939
1090
|
} catch (error) {
|
|
940
1091
|
throw new VError(error, `Failed to open url ${url}`);
|
|
941
1092
|
}
|
|
@@ -946,6 +1097,10 @@ const openUrlElectron = async url => {
|
|
|
946
1097
|
|
|
947
1098
|
// TODO add required platform argument
|
|
948
1099
|
const openUrl = async url => {
|
|
1100
|
+
if (get()) {
|
|
1101
|
+
writeUrl(url);
|
|
1102
|
+
return;
|
|
1103
|
+
}
|
|
949
1104
|
switch (platform) {
|
|
950
1105
|
case Electron:
|
|
951
1106
|
return openUrlElectron();
|
|
@@ -957,21 +1112,15 @@ const {
|
|
|
957
1112
|
openExternal
|
|
958
1113
|
} = OpenExternal;
|
|
959
1114
|
|
|
960
|
-
let text = '';
|
|
961
|
-
const set = value => {
|
|
962
|
-
};
|
|
963
|
-
const readUrl = () => {
|
|
964
|
-
return text;
|
|
965
|
-
};
|
|
966
|
-
|
|
967
1115
|
const readOpenedMemory = () => {
|
|
968
1116
|
return readUrl();
|
|
969
1117
|
};
|
|
970
1118
|
const enable = () => {
|
|
971
|
-
return set();
|
|
1119
|
+
return set(true);
|
|
972
1120
|
};
|
|
973
1121
|
|
|
974
1122
|
const commandMap = {
|
|
1123
|
+
'HandleMessagePort.handleMessagePort': handleMessagePort,
|
|
975
1124
|
'Open.enableMemoryOpener': enable,
|
|
976
1125
|
'Open.openExternal': openExternal,
|
|
977
1126
|
'Open.openUrl': openUrl,
|
package/package.json
CHANGED