@lvce-editor/opener-worker 1.0.0 → 1.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/README.md +1 -1
- package/dist/openerWorkerMain.js +196 -41
- 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
|
|
@@ -857,12 +981,13 @@ const createMockRpc = ({
|
|
|
857
981
|
const Electron = 2;
|
|
858
982
|
|
|
859
983
|
const RendererWorker = 1;
|
|
984
|
+
const SharedProcess = 1492;
|
|
860
985
|
|
|
861
986
|
const rpcs = Object.create(null);
|
|
862
987
|
const set$2 = (id, rpc) => {
|
|
863
988
|
rpcs[id] = rpc;
|
|
864
989
|
};
|
|
865
|
-
const get = id => {
|
|
990
|
+
const get$1 = id => {
|
|
866
991
|
return rpcs[id];
|
|
867
992
|
};
|
|
868
993
|
const remove = id => {
|
|
@@ -873,18 +998,18 @@ const remove = id => {
|
|
|
873
998
|
const create = rpcId => {
|
|
874
999
|
return {
|
|
875
1000
|
async dispose() {
|
|
876
|
-
const rpc = get(rpcId);
|
|
1001
|
+
const rpc = get$1(rpcId);
|
|
877
1002
|
await rpc.dispose();
|
|
878
1003
|
},
|
|
879
1004
|
// @ts-ignore
|
|
880
1005
|
invoke(method, ...params) {
|
|
881
|
-
const rpc = get(rpcId);
|
|
1006
|
+
const rpc = get$1(rpcId);
|
|
882
1007
|
// @ts-ignore
|
|
883
1008
|
return rpc.invoke(method, ...params);
|
|
884
1009
|
},
|
|
885
1010
|
// @ts-ignore
|
|
886
1011
|
invokeAndTransfer(method, ...params) {
|
|
887
|
-
const rpc = get(rpcId);
|
|
1012
|
+
const rpc = get$1(rpcId);
|
|
888
1013
|
// @ts-ignore
|
|
889
1014
|
return rpc.invokeAndTransfer(method, ...params);
|
|
890
1015
|
},
|
|
@@ -907,19 +1032,52 @@ const create = rpcId => {
|
|
|
907
1032
|
};
|
|
908
1033
|
|
|
909
1034
|
const {
|
|
1035
|
+
invoke: invoke$2,
|
|
910
1036
|
set: set$1
|
|
911
1037
|
} = create(RendererWorker);
|
|
912
1038
|
|
|
1039
|
+
const {
|
|
1040
|
+
invoke: invoke$1} = create(SharedProcess);
|
|
1041
|
+
|
|
1042
|
+
const handleMessagePort = async (port, rpcId) => {
|
|
1043
|
+
const rpc = await PlainMessagePortRpcParent.create({
|
|
1044
|
+
commandMap: {},
|
|
1045
|
+
messagePort: port
|
|
1046
|
+
});
|
|
1047
|
+
if (rpcId) {
|
|
1048
|
+
set$2(rpcId, rpc);
|
|
1049
|
+
}
|
|
1050
|
+
};
|
|
1051
|
+
|
|
913
1052
|
const openNew = async url => {
|
|
914
|
-
|
|
1053
|
+
await invoke$1('OpenExternal.openExternal', url);
|
|
915
1054
|
};
|
|
916
1055
|
|
|
917
|
-
|
|
1056
|
+
let enabled = false;
|
|
1057
|
+
let text = '';
|
|
1058
|
+
const set = value => {
|
|
1059
|
+
enabled = value;
|
|
1060
|
+
};
|
|
1061
|
+
const get = () => {
|
|
1062
|
+
return enabled;
|
|
1063
|
+
};
|
|
1064
|
+
const writeUrl = value => {
|
|
1065
|
+
text = value;
|
|
1066
|
+
};
|
|
1067
|
+
const readUrl = () => {
|
|
1068
|
+
return text;
|
|
1069
|
+
};
|
|
1070
|
+
|
|
1071
|
+
const invoke = async (method, ...params) => {
|
|
918
1072
|
// TODO
|
|
919
1073
|
};
|
|
920
1074
|
|
|
921
|
-
const openExternal$1 = url => {
|
|
922
|
-
|
|
1075
|
+
const openExternal$1 = async url => {
|
|
1076
|
+
if (get()) {
|
|
1077
|
+
writeUrl(url);
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
1080
|
+
return invoke('OpenExternal.openExternal', url);
|
|
923
1081
|
};
|
|
924
1082
|
|
|
925
1083
|
const OpenExternal = {
|
|
@@ -927,28 +1085,27 @@ const OpenExternal = {
|
|
|
927
1085
|
openExternal: openExternal$1
|
|
928
1086
|
};
|
|
929
1087
|
|
|
930
|
-
const platform = 0;
|
|
931
|
-
|
|
932
|
-
const invoke = async (method, ...params) => {
|
|
933
|
-
// TODO
|
|
934
|
-
};
|
|
935
|
-
|
|
936
1088
|
const openUrlWeb = async url => {
|
|
937
1089
|
try {
|
|
938
|
-
|
|
1090
|
+
// TODO make platform argument required
|
|
1091
|
+
await invoke$2('Open.openUrl', url);
|
|
939
1092
|
} catch (error) {
|
|
940
1093
|
throw new VError(error, `Failed to open url ${url}`);
|
|
941
1094
|
}
|
|
942
1095
|
};
|
|
943
1096
|
const openUrlElectron = async url => {
|
|
944
|
-
await openNew();
|
|
1097
|
+
await openNew(url);
|
|
945
1098
|
};
|
|
946
1099
|
|
|
947
1100
|
// TODO add required platform argument
|
|
948
|
-
const openUrl = async url => {
|
|
1101
|
+
const openUrl = async (url, platform) => {
|
|
1102
|
+
if (get()) {
|
|
1103
|
+
writeUrl(url);
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
949
1106
|
switch (platform) {
|
|
950
1107
|
case Electron:
|
|
951
|
-
return openUrlElectron();
|
|
1108
|
+
return openUrlElectron(url);
|
|
952
1109
|
default:
|
|
953
1110
|
return openUrlWeb(url);
|
|
954
1111
|
}
|
|
@@ -957,34 +1114,32 @@ const {
|
|
|
957
1114
|
openExternal
|
|
958
1115
|
} = OpenExternal;
|
|
959
1116
|
|
|
960
|
-
let text = '';
|
|
961
|
-
const set = value => {
|
|
962
|
-
};
|
|
963
|
-
const readUrl = () => {
|
|
964
|
-
return text;
|
|
965
|
-
};
|
|
966
|
-
|
|
967
1117
|
const readOpenedMemory = () => {
|
|
968
1118
|
return readUrl();
|
|
969
1119
|
};
|
|
970
1120
|
const enable = () => {
|
|
971
|
-
return set();
|
|
1121
|
+
return set(true);
|
|
972
1122
|
};
|
|
973
1123
|
|
|
974
1124
|
const commandMap = {
|
|
1125
|
+
'HandleMessagePort.handleMessagePort': handleMessagePort,
|
|
975
1126
|
'Open.enableMemoryOpener': enable,
|
|
976
1127
|
'Open.openExternal': openExternal,
|
|
977
1128
|
'Open.openUrl': openUrl,
|
|
978
1129
|
'Open.readOpenedUrl': readOpenedMemory
|
|
979
1130
|
};
|
|
980
1131
|
|
|
981
|
-
const
|
|
1132
|
+
const initializeRendererWorker = async () => {
|
|
982
1133
|
const rpc = await WebWorkerRpcClient.create({
|
|
983
1134
|
commandMap: commandMap
|
|
984
1135
|
});
|
|
985
1136
|
set$1(rpc);
|
|
986
1137
|
};
|
|
987
1138
|
|
|
1139
|
+
const listen = async () => {
|
|
1140
|
+
await initializeRendererWorker();
|
|
1141
|
+
};
|
|
1142
|
+
|
|
988
1143
|
const main = async () => {
|
|
989
1144
|
await listen();
|
|
990
1145
|
};
|
package/package.json
CHANGED