@lvce-editor/iframe-worker 5.27.0 → 5.29.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.
@@ -975,6 +975,9 @@ const getIframeSrcRemoteBaseUrl = (webViewRoot, locationOrigin, platform, webVie
975
975
  if (webViewRoot && (webViewRoot.startsWith('http:') || webViewRoot.startsWith('https:'))) {
976
976
  if (webViewRoot.startsWith(locationOrigin)) {
977
977
  const baseUrl = webViewRoot.slice(locationOrigin.length);
978
+ if (baseUrl.startsWith('/remote')) {
979
+ return baseUrl;
980
+ }
978
981
  return `/remote${root}${baseUrl}`;
979
982
  }
980
983
  return webViewRoot;
@@ -1467,6 +1470,23 @@ const createWebViewConnection = async (uid, origin) => {
1467
1470
  return rpc;
1468
1471
  };
1469
1472
 
1473
+ const getPortQuadruple = () => {
1474
+ const {
1475
+ port1,
1476
+ port2
1477
+ } = new MessageChannel();
1478
+ const {
1479
+ port1: port3,
1480
+ port2: port4
1481
+ } = new MessageChannel();
1482
+ return {
1483
+ port1,
1484
+ port2,
1485
+ port3,
1486
+ port4
1487
+ };
1488
+ };
1489
+
1470
1490
  const readFile = uri => {
1471
1491
  return invoke('FileSystem.readFile', uri);
1472
1492
  };
@@ -1487,7 +1507,7 @@ const set = (id, rpc) => {
1487
1507
  const get = id => {
1488
1508
  return rpcs[id];
1489
1509
  };
1490
- const getAll = () => {
1510
+ const getAll$1 = () => {
1491
1511
  return rpcs;
1492
1512
  };
1493
1513
 
@@ -1577,6 +1597,47 @@ const getWebViewWorkerRpc2 = async rpcInfo => {
1577
1597
  // through the iframe worker. However that could introduce some overhead / latency
1578
1598
  // compared to direct connections
1579
1599
 
1600
+ const interceptors = Object.create(null);
1601
+
1602
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
1603
+ const add = (id, port) => {
1604
+ interceptors[id] = port;
1605
+ };
1606
+ const remove = id => {
1607
+ delete interceptors[id];
1608
+ };
1609
+ const getAll = () => {
1610
+ return Object.values(interceptors);
1611
+ };
1612
+ const isEmpty = () => {
1613
+ return Object.keys(interceptors).length === 0;
1614
+ };
1615
+
1616
+ const notifyInterceptors = message => {
1617
+ if (isEmpty()) {
1618
+ return;
1619
+ }
1620
+ const ports = getAll();
1621
+ // TODO use rpc.invoke
1622
+ for (const port of ports) {
1623
+ port.postMessage({
1624
+ jsonrpc: '2.0',
1625
+ method: 'handleMessageCalled',
1626
+ params: [message]
1627
+ });
1628
+ }
1629
+ };
1630
+
1631
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
1632
+ const proxyPorts = (port1, port2) => {
1633
+ port1.addEventListener('message', event => {
1634
+ port2.postMessage(event.data);
1635
+ });
1636
+ port2.addEventListener('message', event => {
1637
+ port1.postMessage(event.data);
1638
+ });
1639
+ };
1640
+
1580
1641
  const createWebWorkerRpc2 = async (rpcInfo, webView, savedState, uri, portId, webViewUid, origin) => {
1581
1642
  const rpc = await getWebViewWorkerRpc2(rpcInfo);
1582
1643
  const webViewInfo = {
@@ -1592,10 +1653,25 @@ const createWebWorkerRpc2 = async (rpcInfo, webView, savedState, uri, portId, we
1592
1653
  await createWebViewConnection(webViewUid, origin);
1593
1654
  const {
1594
1655
  port1,
1595
- port2
1596
- } = getPortTuple();
1656
+ port2,
1657
+ port3,
1658
+ port4
1659
+ } = getPortQuadruple();
1597
1660
  await createSecondaryWebViewConnection(webViewUid, origin, port1);
1598
- await rpc.invokeAndTransfer('_WebView.setPort', portId, port2);
1661
+
1662
+ // TODO remove message listeners on dispose
1663
+ port2.addEventListener('message', event => {
1664
+ notifyInterceptors(event.data);
1665
+ });
1666
+ port3.addEventListener('message', event => {
1667
+ notifyInterceptors(event.data);
1668
+ });
1669
+ proxyPorts(port2, port3);
1670
+ port1.start();
1671
+ port2.start();
1672
+ port3.start();
1673
+ port4.start();
1674
+ await rpc.invokeAndTransfer('_WebView.setPort', portId, port4);
1599
1675
  await rpc.invoke('_WebView.create', {
1600
1676
  id: portId,
1601
1677
  savedState,
@@ -1779,7 +1855,7 @@ const getSecret = async key => {
1779
1855
  };
1780
1856
 
1781
1857
  const getWebViewInfo = webViewId => {
1782
- const rpcs = getAll();
1858
+ const rpcs = getAll$1();
1783
1859
  for (const value of Object.values(rpcs)) {
1784
1860
  if (value.webViewId === webViewId) {
1785
1861
  return {
@@ -1792,7 +1868,7 @@ const getWebViewInfo = webViewId => {
1792
1868
  };
1793
1869
 
1794
1870
  const saveState = async () => {
1795
- const all = getAll();
1871
+ const all = getAll$1();
1796
1872
  const serialized = [];
1797
1873
  for (const value of Object.values(all)) {
1798
1874
  try {
@@ -1810,16 +1886,6 @@ const saveState = async () => {
1810
1886
  return serialized;
1811
1887
  };
1812
1888
 
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
1889
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
1824
1890
  const registerInterceptor = async (id, port) => {
1825
1891
  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.29.0",
4
4
  "description": "Web Worker to manage creation and lifecycle of iframes in Lvce Editor",
5
5
  "keywords": [
6
6
  "iframe"