@lvce-editor/iframe-worker 5.27.0 → 5.28.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.
@@ -1467,6 +1467,23 @@ const createWebViewConnection = async (uid, origin) => {
1467
1467
  return rpc;
1468
1468
  };
1469
1469
 
1470
+ const getPortQuadruple = () => {
1471
+ const {
1472
+ port1,
1473
+ port2
1474
+ } = new MessageChannel();
1475
+ const {
1476
+ port1: port3,
1477
+ port2: port4
1478
+ } = new MessageChannel();
1479
+ return {
1480
+ port1,
1481
+ port2,
1482
+ port3,
1483
+ port4
1484
+ };
1485
+ };
1486
+
1470
1487
  const readFile = uri => {
1471
1488
  return invoke('FileSystem.readFile', uri);
1472
1489
  };
@@ -1487,7 +1504,7 @@ const set = (id, rpc) => {
1487
1504
  const get = id => {
1488
1505
  return rpcs[id];
1489
1506
  };
1490
- const getAll = () => {
1507
+ const getAll$1 = () => {
1491
1508
  return rpcs;
1492
1509
  };
1493
1510
 
@@ -1577,6 +1594,47 @@ const getWebViewWorkerRpc2 = async rpcInfo => {
1577
1594
  // through the iframe worker. However that could introduce some overhead / latency
1578
1595
  // compared to direct connections
1579
1596
 
1597
+ const interceptors = Object.create(null);
1598
+
1599
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
1600
+ const add = (id, port) => {
1601
+ interceptors[id] = port;
1602
+ };
1603
+ const remove = id => {
1604
+ delete interceptors[id];
1605
+ };
1606
+ const getAll = () => {
1607
+ return Object.values(interceptors);
1608
+ };
1609
+ const isEmpty = () => {
1610
+ return Object.keys(interceptors).length === 0;
1611
+ };
1612
+
1613
+ const notifyInterceptors = message => {
1614
+ if (isEmpty()) {
1615
+ return;
1616
+ }
1617
+ const ports = getAll();
1618
+ // TODO use rpc.invoke
1619
+ for (const port of ports) {
1620
+ port.postMessage({
1621
+ jsonrpc: '2.0',
1622
+ method: 'handleMessageCalled',
1623
+ params: [message]
1624
+ });
1625
+ }
1626
+ };
1627
+
1628
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
1629
+ const proxyPorts = (port1, port2) => {
1630
+ port1.addEventListener('message', event => {
1631
+ port2.postMessage(event.data);
1632
+ });
1633
+ port2.addEventListener('message', event => {
1634
+ port1.postMessage(event.data);
1635
+ });
1636
+ };
1637
+
1580
1638
  const createWebWorkerRpc2 = async (rpcInfo, webView, savedState, uri, portId, webViewUid, origin) => {
1581
1639
  const rpc = await getWebViewWorkerRpc2(rpcInfo);
1582
1640
  const webViewInfo = {
@@ -1592,10 +1650,25 @@ const createWebWorkerRpc2 = async (rpcInfo, webView, savedState, uri, portId, we
1592
1650
  await createWebViewConnection(webViewUid, origin);
1593
1651
  const {
1594
1652
  port1,
1595
- port2
1596
- } = getPortTuple();
1653
+ port2,
1654
+ port3,
1655
+ port4
1656
+ } = getPortQuadruple();
1597
1657
  await createSecondaryWebViewConnection(webViewUid, origin, port1);
1598
- await rpc.invokeAndTransfer('_WebView.setPort', portId, port2);
1658
+
1659
+ // TODO remove message listeners on dispose
1660
+ port2.addEventListener('message', event => {
1661
+ notifyInterceptors(event.data);
1662
+ });
1663
+ port3.addEventListener('message', event => {
1664
+ notifyInterceptors(event.data);
1665
+ });
1666
+ proxyPorts(port2, port3);
1667
+ port1.start();
1668
+ port2.start();
1669
+ port3.start();
1670
+ port4.start();
1671
+ await rpc.invokeAndTransfer('_WebView.setPort', portId, port4);
1599
1672
  await rpc.invoke('_WebView.create', {
1600
1673
  id: portId,
1601
1674
  savedState,
@@ -1779,7 +1852,7 @@ const getSecret = async key => {
1779
1852
  };
1780
1853
 
1781
1854
  const getWebViewInfo = webViewId => {
1782
- const rpcs = getAll();
1855
+ const rpcs = getAll$1();
1783
1856
  for (const value of Object.values(rpcs)) {
1784
1857
  if (value.webViewId === webViewId) {
1785
1858
  return {
@@ -1792,7 +1865,7 @@ const getWebViewInfo = webViewId => {
1792
1865
  };
1793
1866
 
1794
1867
  const saveState = async () => {
1795
- const all = getAll();
1868
+ const all = getAll$1();
1796
1869
  const serialized = [];
1797
1870
  for (const value of Object.values(all)) {
1798
1871
  try {
@@ -1810,16 +1883,6 @@ const saveState = async () => {
1810
1883
  return serialized;
1811
1884
  };
1812
1885
 
1813
- const interceptors = Object.create(null);
1814
-
1815
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
1816
- const add = (id, port) => {
1817
- interceptors[id] = port;
1818
- };
1819
- const remove = id => {
1820
- delete interceptors[id];
1821
- };
1822
-
1823
1886
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
1824
1887
  const registerInterceptor = async (id, port) => {
1825
1888
  add(id, port);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/iframe-worker",
3
- "version": "5.27.0",
3
+ "version": "5.28.0",
4
4
  "description": "Web Worker to manage creation and lifecycle of iframes in Lvce Editor",
5
5
  "keywords": [
6
6
  "iframe"