@lvce-editor/panel-worker 2.8.0 → 2.10.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.
@@ -423,6 +423,100 @@ const IpcChildWithModuleWorkerAndMessagePort$1 = {
423
423
  listen: listen$6,
424
424
  wrap: wrap$e
425
425
  };
426
+ const addListener = (emitter, type, callback) => {
427
+ if ('addEventListener' in emitter) {
428
+ emitter.addEventListener(type, callback);
429
+ } else {
430
+ emitter.on(type, callback);
431
+ }
432
+ };
433
+ const removeListener = (emitter, type, callback) => {
434
+ if ('removeEventListener' in emitter) {
435
+ emitter.removeEventListener(type, callback);
436
+ } else {
437
+ emitter.off(type, callback);
438
+ }
439
+ };
440
+ const getFirstEvent = (eventEmitter, eventMap) => {
441
+ const {
442
+ promise,
443
+ resolve
444
+ } = Promise.withResolvers();
445
+ const listenerMap = Object.create(null);
446
+ const cleanup = value => {
447
+ for (const event of Object.keys(eventMap)) {
448
+ removeListener(eventEmitter, event, listenerMap[event]);
449
+ }
450
+ resolve(value);
451
+ };
452
+ for (const [event, type] of Object.entries(eventMap)) {
453
+ const listener = event => {
454
+ cleanup({
455
+ event,
456
+ type
457
+ });
458
+ };
459
+ addListener(eventEmitter, event, listener);
460
+ listenerMap[event] = listener;
461
+ }
462
+ return promise;
463
+ };
464
+ const Message$1 = 3;
465
+ const create$5$1 = async ({
466
+ isMessagePortOpen,
467
+ messagePort
468
+ }) => {
469
+ if (!isMessagePort(messagePort)) {
470
+ throw new IpcError('port must be of type MessagePort');
471
+ }
472
+ if (isMessagePortOpen) {
473
+ return messagePort;
474
+ }
475
+ const eventPromise = getFirstEvent(messagePort, {
476
+ message: Message$1
477
+ });
478
+ messagePort.start();
479
+ const {
480
+ event,
481
+ type
482
+ } = await eventPromise;
483
+ if (type !== Message$1) {
484
+ throw new IpcError('Failed to wait for ipc message');
485
+ }
486
+ if (event.data !== readyMessage) {
487
+ throw new IpcError('unexpected first message');
488
+ }
489
+ return messagePort;
490
+ };
491
+ const signal$1 = messagePort => {
492
+ messagePort.start();
493
+ };
494
+ class IpcParentWithMessagePort extends Ipc {
495
+ getData = getData$2;
496
+ send(message) {
497
+ this._rawIpc.postMessage(message);
498
+ }
499
+ sendAndTransfer(message) {
500
+ const transfer = getTransferrables(message);
501
+ this._rawIpc.postMessage(message, transfer);
502
+ }
503
+ dispose() {
504
+ this._rawIpc.close();
505
+ }
506
+ onMessage(callback) {
507
+ this._rawIpc.addEventListener('message', callback);
508
+ }
509
+ onClose(callback) {}
510
+ }
511
+ const wrap$5 = messagePort => {
512
+ return new IpcParentWithMessagePort(messagePort);
513
+ };
514
+ const IpcParentWithMessagePort$1 = {
515
+ __proto__: null,
516
+ create: create$5$1,
517
+ signal: signal$1,
518
+ wrap: wrap$5
519
+ };
426
520
 
427
521
  class CommandNotFoundError extends Error {
428
522
  constructor(command) {
@@ -700,7 +794,7 @@ const getErrorResponse = (id, error, preparePrettyError, logError) => {
700
794
  const errorProperty = getErrorProperty(error, prettyError);
701
795
  return create$1$1(id, errorProperty);
702
796
  };
703
- const create$7 = (message, result) => {
797
+ const create$8 = (message, result) => {
704
798
  return {
705
799
  id: message.id,
706
800
  jsonrpc: Two$1,
@@ -709,7 +803,7 @@ const create$7 = (message, result) => {
709
803
  };
710
804
  const getSuccessResponse = (message, result) => {
711
805
  const resultProperty = result ?? null;
712
- return create$7(message, resultProperty);
806
+ return create$8(message, resultProperty);
713
807
  };
714
808
  const getErrorResponseSimple = (id, error) => {
715
809
  return {
@@ -803,7 +897,7 @@ const handleJsonRpcMessage = async (...args) => {
803
897
 
804
898
  const Two = '2.0';
805
899
 
806
- const create$6 = (method, params) => {
900
+ const create$7 = (method, params) => {
807
901
  return {
808
902
  jsonrpc: Two,
809
903
  method,
@@ -811,7 +905,7 @@ const create$6 = (method, params) => {
811
905
  };
812
906
  };
813
907
 
814
- const create$5 = (id, method, params) => {
908
+ const create$6 = (id, method, params) => {
815
909
  const message = {
816
910
  id,
817
911
  jsonrpc: Two,
@@ -822,12 +916,12 @@ const create$5 = (id, method, params) => {
822
916
  };
823
917
 
824
918
  let id = 0;
825
- const create$4 = () => {
919
+ const create$5 = () => {
826
920
  return ++id;
827
921
  };
828
922
 
829
923
  const registerPromise = map => {
830
- const id = create$4();
924
+ const id = create$5();
831
925
  const {
832
926
  promise,
833
927
  resolve
@@ -844,7 +938,7 @@ const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer)
844
938
  id,
845
939
  promise
846
940
  } = registerPromise(callbacks);
847
- const message = create$5(id, method, params);
941
+ const message = create$6(id, method, params);
848
942
  if (useSendAndTransfer && ipc.sendAndTransfer) {
849
943
  ipc.sendAndTransfer(message);
850
944
  } else {
@@ -880,7 +974,7 @@ const createRpc = ipc => {
880
974
  * @deprecated
881
975
  */
882
976
  send(method, ...params) {
883
- const message = create$6(method, params);
977
+ const message = create$7(method, params);
884
978
  ipc.send(message);
885
979
  }
886
980
  };
@@ -920,6 +1014,24 @@ const listen$1 = async (module, options) => {
920
1014
  return ipc;
921
1015
  };
922
1016
 
1017
+ const create$4 = async ({
1018
+ commandMap,
1019
+ isMessagePortOpen = true,
1020
+ messagePort
1021
+ }) => {
1022
+ // TODO create a commandMap per rpc instance
1023
+ register(commandMap);
1024
+ const rawIpc = await IpcParentWithMessagePort$1.create({
1025
+ isMessagePortOpen,
1026
+ messagePort
1027
+ });
1028
+ const ipc = IpcParentWithMessagePort$1.wrap(rawIpc);
1029
+ handleIpc(ipc);
1030
+ const rpc = createRpc(ipc);
1031
+ messagePort.start();
1032
+ return rpc;
1033
+ };
1034
+
923
1035
  const create$3 = async ({
924
1036
  commandMap
925
1037
  }) => {
@@ -952,7 +1064,7 @@ const createMockRpc = ({
952
1064
  };
953
1065
 
954
1066
  const rpcs = Object.create(null);
955
- const set$2 = (id, rpc) => {
1067
+ const set$4 = (id, rpc) => {
956
1068
  rpcs[id] = rpc;
957
1069
  };
958
1070
  const get$1 = id => {
@@ -985,7 +1097,7 @@ const create$2 = rpcId => {
985
1097
  const mockRpc = createMockRpc({
986
1098
  commandMap
987
1099
  });
988
- set$2(rpcId, mockRpc);
1100
+ set$4(rpcId, mockRpc);
989
1101
  // @ts-ignore
990
1102
  mockRpc[Symbol.dispose] = () => {
991
1103
  remove(rpcId);
@@ -994,7 +1106,7 @@ const create$2 = rpcId => {
994
1106
  return mockRpc;
995
1107
  },
996
1108
  set(rpc) {
997
- set$2(rpcId, rpc);
1109
+ set$4(rpcId, rpc);
998
1110
  }
999
1111
  };
1000
1112
  };
@@ -1174,13 +1286,19 @@ const VirtualDomElements = {
1174
1286
 
1175
1287
  const TargetName = 'event.target.name';
1176
1288
 
1289
+ const RendererProcess = 1670;
1177
1290
  const RendererWorker = 1;
1178
1291
 
1179
1292
  const SetPatches = 'Viewlet.setPatches';
1180
1293
 
1181
1294
  const {
1182
- invoke,
1183
- set: set$1
1295
+ invoke: invoke$2,
1296
+ set: set$3
1297
+ } = create$2(RendererProcess);
1298
+
1299
+ const {
1300
+ invoke: invoke$1,
1301
+ set: set$2
1184
1302
  } = create$2(RendererWorker);
1185
1303
 
1186
1304
  const toCommandId = key => {
@@ -1433,7 +1551,7 @@ const {
1433
1551
  get,
1434
1552
  getCommandIds,
1435
1553
  registerCommands,
1436
- set,
1554
+ set: set$1,
1437
1555
  wrapCommand,
1438
1556
  wrapGetter
1439
1557
  } = create$1();
@@ -1460,7 +1578,7 @@ const create = (uid, uri, x, y, width, height, platform, assetDir) => {
1460
1578
  x,
1461
1579
  y
1462
1580
  };
1463
- set(uid, state, state);
1581
+ set$1(uid, state, state);
1464
1582
  };
1465
1583
 
1466
1584
  const isEqual$2 = (oldState, newState) => {
@@ -1503,6 +1621,39 @@ const diff2 = uid => {
1503
1621
  return result;
1504
1622
  };
1505
1623
 
1624
+ const state = {
1625
+ connected: false
1626
+ };
1627
+ const isConnected = () => {
1628
+ const {
1629
+ connected
1630
+ } = state;
1631
+ return connected;
1632
+ };
1633
+ const invoke = (method, ...params) => invoke$2(method, ...params);
1634
+ const set = rpc => {
1635
+ set$3(rpc);
1636
+ state.connected = true;
1637
+ };
1638
+
1639
+ const handleMessagePort = async (port, viewletCommandMap) => {
1640
+ const executeViewletCommand = async (uid, command, ...args) => {
1641
+ const fn = viewletCommandMap[`Panel.${command}`];
1642
+ if (typeof fn !== 'function') {
1643
+ throw new TypeError(`Viewlet command not found: ${command}`);
1644
+ }
1645
+ await fn(uid, ...args);
1646
+ await invoke$1('Viewlet.requestRender', uid);
1647
+ };
1648
+ const rpc = await create$4({
1649
+ commandMap: {
1650
+ 'Viewlet.executeViewletCommand': executeViewletCommand
1651
+ },
1652
+ messagePort: port
1653
+ });
1654
+ set(rpc);
1655
+ };
1656
+
1506
1657
  const setBadgeCount = (state, id, count) => {
1507
1658
  const {
1508
1659
  badgeCounts
@@ -1534,16 +1685,16 @@ const handleProblemsSummaryChange = (state, summary) => {
1534
1685
  };
1535
1686
 
1536
1687
  const handleClickClose = async state => {
1537
- await invoke('Layout.hidePanel');
1688
+ await invoke$1('Layout.hidePanel');
1538
1689
  return state;
1539
1690
  };
1540
1691
 
1541
1692
  const handleClickMaximize = async state => {
1542
- await invoke('Layout.maximizePanel');
1693
+ await invoke$1('Layout.maximizePanel');
1543
1694
  return state;
1544
1695
  };
1545
1696
  const handleClickUnmaximize = async state => {
1546
- await invoke('Layout.unmaximizePanel');
1697
+ await invoke$1('Layout.unmaximizePanel');
1547
1698
  return state;
1548
1699
  };
1549
1700
 
@@ -1574,7 +1725,7 @@ const handleFilterInput = async (state, value) => {
1574
1725
  currentViewletId
1575
1726
  } = state;
1576
1727
  const fullCommand = `${currentViewletId}.handleFilterInput`;
1577
- await invoke(fullCommand, value);
1728
+ await invoke$1(fullCommand, value);
1578
1729
  return state;
1579
1730
  };
1580
1731
 
@@ -1591,7 +1742,7 @@ const getSavedViewletId = savedState => {
1591
1742
  };
1592
1743
 
1593
1744
  const createViewlet = async (viewletModuleId, editorUid, tabId, actionsUid, bounds, uri, focus) => {
1594
- await invoke('Layout.createPanelViewlet', viewletModuleId, editorUid, tabId, actionsUid, bounds, uri, focus);
1745
+ await invoke$1('Layout.createPanelViewlet', viewletModuleId, editorUid, tabId, actionsUid, bounds, uri, focus);
1595
1746
  };
1596
1747
 
1597
1748
  const getContentDimensions = dimensions => {
@@ -1620,7 +1771,7 @@ const saveViewletState = async (childUid, viewletId) => {
1620
1771
  return;
1621
1772
  }
1622
1773
  try {
1623
- await invoke(command, childUid, viewletId);
1774
+ await invoke$1(command, childUid, viewletId);
1624
1775
  } catch (error) {
1625
1776
  if (!isCommandNotFoundError(error)) {
1626
1777
  throw error;
@@ -2201,9 +2352,14 @@ const render2 = (uid, diffResult) => {
2201
2352
  newState,
2202
2353
  oldState
2203
2354
  } = get(uid);
2204
- set(uid, newState, newState);
2355
+ set$1(uid, newState, newState);
2205
2356
  const commands = applyRender(oldState, newState, diffResult);
2206
- return commands;
2357
+ if (!isConnected()) return commands;
2358
+ return renderDirect(uid, commands);
2359
+ };
2360
+ const renderDirect = async (uid, commands) => {
2361
+ const transactionId = await invoke('Viewlet.queueCommands', uid, commands);
2362
+ return [['Viewlet.commitPending', uid, transactionId]];
2207
2363
  };
2208
2364
 
2209
2365
  const renderEventListeners = () => {
@@ -2241,7 +2397,7 @@ const resize = async (state, dimensions) => {
2241
2397
  };
2242
2398
  }
2243
2399
  // @ts-ignore
2244
- await invoke('Viewlet.resize', childUid, dimensions);
2400
+ await invoke$1('Viewlet.resize', childUid, dimensions);
2245
2401
  return {
2246
2402
  ...newState
2247
2403
  };
@@ -2272,6 +2428,7 @@ const selectName = async (state, name) => {
2272
2428
  return selectIndex(state, index);
2273
2429
  };
2274
2430
 
2431
+ const handleDirectMessagePort = port => handleMessagePort(port, commandMap);
2275
2432
  const commandMap = {
2276
2433
  'Panel.create': create,
2277
2434
  'Panel.diff2': diff2,
@@ -2281,6 +2438,7 @@ const commandMap = {
2281
2438
  'Panel.handleClickMaximize': wrapCommand(handleClickMaximize),
2282
2439
  'Panel.handleClickUnmaximize': wrapCommand(handleClickUnmaximize),
2283
2440
  'Panel.handleFilterInput': wrapCommand(handleFilterInput),
2441
+ 'Panel.handleMessagePort': handleDirectMessagePort,
2284
2442
  'Panel.handlePanelLayoutChanged': wrapCommand(handlePanelLayoutChanged),
2285
2443
  'Panel.handleProblemsSummaryChange': wrapCommand(handleProblemsSummaryChange),
2286
2444
  'Panel.loadContent': wrapCommand(loadContent),
@@ -2302,7 +2460,7 @@ const listen = async () => {
2302
2460
  const rpc = await create$3({
2303
2461
  commandMap: commandMap
2304
2462
  });
2305
- set$1(rpc);
2463
+ set$2(rpc);
2306
2464
  };
2307
2465
 
2308
2466
  const main = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/panel-worker",
3
- "version": "2.8.0",
3
+ "version": "2.10.0",
4
4
  "description": "Panel Worker",
5
5
  "repository": {
6
6
  "type": "git",