@lvce-editor/iframe-worker 5.12.0 → 5.14.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.
@@ -483,7 +483,7 @@ const create$4 = (method, params) => {
483
483
  };
484
484
  };
485
485
  const callbacks = Object.create(null);
486
- const set = (id, fn) => {
486
+ const set$1 = (id, fn) => {
487
487
  callbacks[id] = fn;
488
488
  };
489
489
  const get = id => {
@@ -502,7 +502,7 @@ const registerPromise = () => {
502
502
  resolve,
503
503
  promise
504
504
  } = Promise.withResolvers();
505
- set(id, resolve);
505
+ set$1(id, resolve);
506
506
  return {
507
507
  id,
508
508
  promise
@@ -1198,6 +1198,9 @@ const getSavedWebViewState = async id => {
1198
1198
  if (item && item.key && item.key === id && item.value && item.value.state) {
1199
1199
  return item.value.state;
1200
1200
  }
1201
+ if (item && item.key && item.key === id && item.value) {
1202
+ return item.value;
1203
+ }
1201
1204
  }
1202
1205
  return undefined;
1203
1206
  };
@@ -1559,6 +1562,14 @@ const getWebViewWorkerRpc = async rpcInfo => {
1559
1562
  return rpc;
1560
1563
  };
1561
1564
 
1565
+ const rpcs = Object.create(null);
1566
+ const set = (id, rpc) => {
1567
+ rpcs[id] = rpc;
1568
+ };
1569
+ const getAll = () => {
1570
+ return rpcs;
1571
+ };
1572
+
1562
1573
  const createWebViewRpc = async (webView, savedState, uri, portId, webViewUid, origin) => {
1563
1574
  if (!webView || !webView.rpc || typeof webView.rpc !== 'string') {
1564
1575
  return;
@@ -1568,6 +1579,12 @@ const createWebViewRpc = async (webView, savedState, uri, portId, webViewUid, or
1568
1579
  throw new Error(`only web worker rpc is supported for webviews`);
1569
1580
  }
1570
1581
  const rpc = await getWebViewWorkerRpc(rpcInfo);
1582
+ const webViewInfo = {
1583
+ rpc,
1584
+ webViewId: webView.id,
1585
+ portId: portId
1586
+ };
1587
+ set(portId, webViewInfo);
1571
1588
  await rpc.invoke('LoadFile.loadFile', rpcInfo.url);
1572
1589
 
1573
1590
  // TODO this connection might not be needed
@@ -1662,10 +1679,6 @@ const create3 = async ({
1662
1679
  const iframeCsp = platform === Web ? csp : '';
1663
1680
  const credentialless = getCredentialLess(locationHost);
1664
1681
  await invoke$3('ExtensionHostManagement.activateByEvent', `onWebView:${webViewId}`);
1665
- const {
1666
- port1,
1667
- port2
1668
- } = getPortTuple();
1669
1682
  const portId = create$1();
1670
1683
  const remotePathPrefix = '/remote';
1671
1684
  await register(previewServerId, webViewPort, frameAncestors, webViewRoot, csp, iframeContent, platform, webViewId, remotePathPrefix, useNewWebViewHandler);
@@ -1676,11 +1689,20 @@ const create3 = async ({
1676
1689
  // port and wait for the first port message
1677
1690
  await invoke$1('WebView.load', id);
1678
1691
  const origin = getWebViewOrigin(webViewPort, platform, webViewScheme, webViewId);
1679
- const portType = '';
1680
- await setPort(id, port1, origin, portType);
1681
- await invokeAndTransfer$2('ExtensionHostWebView.create', webViewId, port2, uri, id, origin, webView);
1692
+ const hasOldRpc = !webView || !webView.rpc || typeof webView.rpc !== 'string';
1693
+ if (hasOldRpc) {
1694
+ const {
1695
+ port1,
1696
+ port2
1697
+ } = getPortTuple();
1698
+ const portType = '';
1699
+ await setPort(id, port1, origin, portType);
1700
+ await invokeAndTransfer$2('ExtensionHostWebView.create', webViewId, port2, uri, id, origin, webView);
1701
+ }
1682
1702
  const savedState = await getSavedWebViewState(webViewId);
1683
- await invoke$2('ExtensionHostWebView.load', webViewId, savedState);
1703
+ if (hasOldRpc) {
1704
+ await invoke$2('ExtensionHostWebView.load', webViewId, savedState);
1705
+ }
1684
1706
  await createWebViewRpc(webView, savedState, uri, portId, id, origin);
1685
1707
  return {
1686
1708
  iframeSrc,
@@ -1691,9 +1713,29 @@ const create3 = async ({
1691
1713
  };
1692
1714
  };
1693
1715
 
1716
+ const saveState = async () => {
1717
+ const all = getAll();
1718
+ const serialized = [];
1719
+ for (const value of Object.values(all)) {
1720
+ try {
1721
+ const savedState = await value.rpc.invoke('WebView.saveState', value.portId);
1722
+ serialized.push({
1723
+ key: value.webViewId,
1724
+ value: savedState
1725
+ });
1726
+ } catch (error) {
1727
+ console.log(error);
1728
+ // TODO maybe log the error
1729
+ // ignore
1730
+ }
1731
+ }
1732
+ return serialized;
1733
+ };
1734
+
1694
1735
  const commandMap = {
1695
1736
  'WebView.create2': create2,
1696
- 'WebView.create3': create3
1737
+ 'WebView.create3': create3,
1738
+ 'WebView.saveState': saveState
1697
1739
  };
1698
1740
 
1699
1741
  const listen = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/iframe-worker",
3
- "version": "5.12.0",
3
+ "version": "5.14.0",
4
4
  "description": "Web Worker to manage creation and lifecycle of iframes in Lvce Editor",
5
5
  "main": "dist/iframeWorkerMain.js",
6
6
  "type": "module",