@lvce-editor/extension-management-worker 1.0.0 → 1.3.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.
|
@@ -368,6 +368,100 @@ 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
|
+
resolve,
|
|
388
|
+
promise
|
|
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
|
+
type,
|
|
401
|
+
event
|
|
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
|
+
messagePort,
|
|
412
|
+
isMessagePortOpen
|
|
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
|
+
type,
|
|
426
|
+
event
|
|
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 = '2.0';
|
|
373
467
|
const create$4 = (method, params) => {
|
|
@@ -378,10 +472,10 @@ const create$4 = (method, params) => {
|
|
|
378
472
|
};
|
|
379
473
|
};
|
|
380
474
|
const callbacks = Object.create(null);
|
|
381
|
-
const set$
|
|
475
|
+
const set$3 = (id, fn) => {
|
|
382
476
|
callbacks[id] = fn;
|
|
383
477
|
};
|
|
384
|
-
const get$
|
|
478
|
+
const get$2 = id => {
|
|
385
479
|
return callbacks[id];
|
|
386
480
|
};
|
|
387
481
|
const remove = id => {
|
|
@@ -397,7 +491,7 @@ const registerPromise = () => {
|
|
|
397
491
|
resolve,
|
|
398
492
|
promise
|
|
399
493
|
} = Promise.withResolvers();
|
|
400
|
-
set$
|
|
494
|
+
set$3(id, resolve);
|
|
401
495
|
return {
|
|
402
496
|
id,
|
|
403
497
|
promise
|
|
@@ -562,7 +656,7 @@ const warn = (...args) => {
|
|
|
562
656
|
console.warn(...args);
|
|
563
657
|
};
|
|
564
658
|
const resolve = (id, response) => {
|
|
565
|
-
const fn = get$
|
|
659
|
+
const fn = get$2(id);
|
|
566
660
|
if (!fn) {
|
|
567
661
|
console.log(response);
|
|
568
662
|
warn(`callback ${id} may already be disposed`);
|
|
@@ -625,7 +719,7 @@ const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
|
625
719
|
const errorProperty = getErrorProperty(error, prettyError);
|
|
626
720
|
return create$1$1(id, errorProperty);
|
|
627
721
|
};
|
|
628
|
-
const create$
|
|
722
|
+
const create$6 = (message, result) => {
|
|
629
723
|
return {
|
|
630
724
|
jsonrpc: Two,
|
|
631
725
|
id: message.id,
|
|
@@ -634,7 +728,7 @@ const create$5 = (message, result) => {
|
|
|
634
728
|
};
|
|
635
729
|
const getSuccessResponse = (message, result) => {
|
|
636
730
|
const resultProperty = result ?? null;
|
|
637
|
-
return create$
|
|
731
|
+
return create$6(message, resultProperty);
|
|
638
732
|
};
|
|
639
733
|
const getErrorResponseSimple = (id, error) => {
|
|
640
734
|
return {
|
|
@@ -742,7 +836,7 @@ const send = (transport, method, ...params) => {
|
|
|
742
836
|
const message = create$4(method, params);
|
|
743
837
|
transport.send(message);
|
|
744
838
|
};
|
|
745
|
-
const invoke = (ipc, method, ...params) => {
|
|
839
|
+
const invoke$2 = (ipc, method, ...params) => {
|
|
746
840
|
return invokeHelper(ipc, method, params, false);
|
|
747
841
|
};
|
|
748
842
|
const invokeAndTransfer = (ipc, method, ...params) => {
|
|
@@ -781,7 +875,7 @@ const createRpc = ipc => {
|
|
|
781
875
|
send(ipc, method, ...params);
|
|
782
876
|
},
|
|
783
877
|
invoke(method, ...params) {
|
|
784
|
-
return invoke(ipc, method, ...params);
|
|
878
|
+
return invoke$2(ipc, method, ...params);
|
|
785
879
|
},
|
|
786
880
|
invokeAndTransfer(method, ...params) {
|
|
787
881
|
return invokeAndTransfer(ipc, method, ...params);
|
|
@@ -822,6 +916,26 @@ const listen$1 = async (module, options) => {
|
|
|
822
916
|
const ipc = module.wrap(rawIpc);
|
|
823
917
|
return ipc;
|
|
824
918
|
};
|
|
919
|
+
const create$5 = async ({
|
|
920
|
+
commandMap,
|
|
921
|
+
messagePort
|
|
922
|
+
}) => {
|
|
923
|
+
// TODO create a commandMap per rpc instance
|
|
924
|
+
register(commandMap);
|
|
925
|
+
const rawIpc = await IpcParentWithMessagePort$1.create({
|
|
926
|
+
messagePort,
|
|
927
|
+
isMessagePortOpen: true
|
|
928
|
+
});
|
|
929
|
+
const ipc = IpcParentWithMessagePort$1.wrap(rawIpc);
|
|
930
|
+
handleIpc(ipc);
|
|
931
|
+
const rpc = createRpc(ipc);
|
|
932
|
+
messagePort.start();
|
|
933
|
+
return rpc;
|
|
934
|
+
};
|
|
935
|
+
const PlainMessagePortRpc = {
|
|
936
|
+
__proto__: null,
|
|
937
|
+
create: create$5
|
|
938
|
+
};
|
|
825
939
|
const create$1 = async ({
|
|
826
940
|
commandMap
|
|
827
941
|
}) => {
|
|
@@ -838,48 +952,121 @@ const WebWorkerRpcClient = {
|
|
|
838
952
|
};
|
|
839
953
|
|
|
840
954
|
const RendererWorker = 1;
|
|
955
|
+
const SharedProcess = 1;
|
|
841
956
|
|
|
842
957
|
const rpcs = Object.create(null);
|
|
843
|
-
const set$
|
|
958
|
+
const set$2 = (id, rpc) => {
|
|
844
959
|
rpcs[id] = rpc;
|
|
845
960
|
};
|
|
846
|
-
const get = id => {
|
|
961
|
+
const get$1 = id => {
|
|
847
962
|
return rpcs[id];
|
|
848
963
|
};
|
|
849
964
|
|
|
850
965
|
const create = rpcId => {
|
|
851
966
|
return {
|
|
967
|
+
async dispose() {
|
|
968
|
+
const rpc = get$1(rpcId);
|
|
969
|
+
await rpc.dispose();
|
|
970
|
+
},
|
|
852
971
|
// @ts-ignore
|
|
853
972
|
invoke(method, ...params) {
|
|
854
|
-
const rpc = get(rpcId);
|
|
973
|
+
const rpc = get$1(rpcId);
|
|
855
974
|
// @ts-ignore
|
|
856
975
|
return rpc.invoke(method, ...params);
|
|
857
976
|
},
|
|
858
977
|
// @ts-ignore
|
|
859
978
|
invokeAndTransfer(method, ...params) {
|
|
860
|
-
const rpc = get(rpcId);
|
|
979
|
+
const rpc = get$1(rpcId);
|
|
861
980
|
// @ts-ignore
|
|
862
981
|
return rpc.invokeAndTransfer(method, ...params);
|
|
863
982
|
},
|
|
864
983
|
set(rpc) {
|
|
865
|
-
set$
|
|
866
|
-
},
|
|
867
|
-
async dispose() {
|
|
868
|
-
const rpc = get(rpcId);
|
|
869
|
-
await rpc.dispose();
|
|
984
|
+
set$2(rpcId, rpc);
|
|
870
985
|
}
|
|
871
986
|
};
|
|
872
987
|
};
|
|
873
988
|
|
|
874
989
|
const {
|
|
875
|
-
|
|
990
|
+
invoke: invoke$1,
|
|
991
|
+
set: set$1
|
|
992
|
+
} = create(RendererWorker);
|
|
993
|
+
const getExtension$1 = async id => {
|
|
994
|
+
// @ts-ignore
|
|
995
|
+
return invoke$1('ExtensionManagement.getExtension', id);
|
|
996
|
+
};
|
|
876
997
|
|
|
877
|
-
const
|
|
878
|
-
|
|
998
|
+
const {
|
|
999
|
+
invoke} = create(SharedProcess);
|
|
1000
|
+
|
|
1001
|
+
const invalidateExtensionsCache = async () => {
|
|
1002
|
+
await invoke$1('Extensions.invalidateExtensionsCache');
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
let state = {
|
|
1006
|
+
disabledIds: []
|
|
1007
|
+
};
|
|
1008
|
+
const set = newState => {
|
|
1009
|
+
state = newState;
|
|
1010
|
+
};
|
|
1011
|
+
const get = () => {
|
|
1012
|
+
return state;
|
|
879
1013
|
};
|
|
880
1014
|
|
|
881
|
-
const
|
|
1015
|
+
const disableExtension = async (id, isTest) => {
|
|
1016
|
+
try {
|
|
1017
|
+
if (isTest) {
|
|
1018
|
+
const oldState = get();
|
|
1019
|
+
const newState = {
|
|
1020
|
+
...oldState,
|
|
1021
|
+
disabledIds: [...oldState.disabledIds, id]
|
|
1022
|
+
};
|
|
1023
|
+
set(newState);
|
|
1024
|
+
} else {
|
|
1025
|
+
await invoke('ExtensionManagement.disable', id);
|
|
1026
|
+
}
|
|
1027
|
+
await invalidateExtensionsCache();
|
|
1028
|
+
return undefined;
|
|
1029
|
+
} catch (error) {
|
|
1030
|
+
return error;
|
|
1031
|
+
}
|
|
1032
|
+
};
|
|
1033
|
+
|
|
1034
|
+
const enableExtension = async (id, isTest) => {
|
|
1035
|
+
try {
|
|
1036
|
+
if (isTest) {
|
|
1037
|
+
const oldState = get();
|
|
1038
|
+
const newState = {
|
|
1039
|
+
...oldState,
|
|
1040
|
+
disabledIds: oldState.disabledIds.filter(existing => existing !== id)
|
|
1041
|
+
};
|
|
1042
|
+
set(newState);
|
|
1043
|
+
} else {
|
|
1044
|
+
await invoke('ExtensionManagement.enable', /* id */id);
|
|
1045
|
+
}
|
|
1046
|
+
await invalidateExtensionsCache();
|
|
1047
|
+
return undefined;
|
|
1048
|
+
} catch (error) {
|
|
1049
|
+
return error;
|
|
1050
|
+
}
|
|
1051
|
+
};
|
|
1052
|
+
|
|
1053
|
+
const getExtension = async id => {
|
|
882
1054
|
// TODO
|
|
1055
|
+
const extension = await getExtension$1(id);
|
|
1056
|
+
return extension;
|
|
1057
|
+
};
|
|
1058
|
+
|
|
1059
|
+
const commandMapRef = {};
|
|
1060
|
+
|
|
1061
|
+
const handleMessagePort = async port => {
|
|
1062
|
+
await PlainMessagePortRpc.create({
|
|
1063
|
+
commandMap: commandMapRef,
|
|
1064
|
+
messagePort: port
|
|
1065
|
+
});
|
|
1066
|
+
};
|
|
1067
|
+
|
|
1068
|
+
const initialize = async () => {
|
|
1069
|
+
// TODO create connection to shared process
|
|
883
1070
|
};
|
|
884
1071
|
|
|
885
1072
|
const installExtension = async () => {
|
|
@@ -893,15 +1080,19 @@ const uninstallExtension = async () => {
|
|
|
893
1080
|
const commandMap = {
|
|
894
1081
|
'Extensions.disable': disableExtension,
|
|
895
1082
|
'Extensions.enable': enableExtension,
|
|
1083
|
+
'Extensions.getExtension': getExtension,
|
|
1084
|
+
'Extensions.handleMessagePort': handleMessagePort,
|
|
1085
|
+
'Extensions.initialize': initialize,
|
|
896
1086
|
'Extensions.install': installExtension,
|
|
897
1087
|
'Extensions.uninstall': uninstallExtension
|
|
898
1088
|
};
|
|
899
1089
|
|
|
900
1090
|
const listen = async () => {
|
|
1091
|
+
Object.assign(commandMapRef, commandMap);
|
|
901
1092
|
const rpc = await WebWorkerRpcClient.create({
|
|
902
1093
|
commandMap: commandMap
|
|
903
1094
|
});
|
|
904
|
-
set(rpc);
|
|
1095
|
+
set$1(rpc);
|
|
905
1096
|
};
|
|
906
1097
|
|
|
907
1098
|
const main = async () => {
|