@pulse-editor/react-api 0.1.1-beta.78 → 0.1.1-beta.80

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.
@@ -17,7 +17,7 @@ import { DependencyList } from "react";
17
17
  export default function useActionEffect(params: {
18
18
  actionName: string;
19
19
  beforeAction?: (args: any) => Promise<any>;
20
- afterAction?: (args: any, result: any) => Promise<any>;
20
+ afterAction?: (args: any) => Promise<any>;
21
21
  }, deps: DependencyList, isExtReady?: boolean): {
22
22
  isReady: boolean;
23
23
  runAppAction: ((args: any) => Promise<any>) | undefined;
package/dist/main.js CHANGED
@@ -5651,9 +5651,21 @@ function useActionEffect(params, deps, isExtReady = true) {
5651
5651
  // Queue to hold commands until extension is ready
5652
5652
  const commandQueue = useRef([]);
5653
5653
  const isCommandExecuting = useRef(false);
5654
+ const [remoteOrigin, setRemoteOrigin] = useState(undefined);
5654
5655
  const [beforeAction, setBeforeAction] = useState(params.beforeAction);
5655
5656
  const [afterAction, setAfterAction] = useState(params.afterAction);
5656
- const [handler, setHandler] = useState(undefined);
5657
+ const [actionHandler, setActionHandler] = useState(undefined);
5658
+ useEffect(() => {
5659
+ async function fetchRemoteOrigin() {
5660
+ const origin = await imc?.sendMessage(IMCMessageTypeEnum.EditorGetAppOrigin, {
5661
+ actionName: params.actionName,
5662
+ });
5663
+ setRemoteOrigin(origin);
5664
+ }
5665
+ if (isReady) {
5666
+ fetchRemoteOrigin();
5667
+ }
5668
+ }, [isReady]);
5657
5669
  // Flush queued commands when isExtReady becomes true
5658
5670
  useEffect(() => {
5659
5671
  async function runQueuedCommands() {
@@ -5690,14 +5702,18 @@ function useActionEffect(params, deps, isExtReady = true) {
5690
5702
  };
5691
5703
  }
5692
5704
  async function updateAction() {
5705
+ if (!remoteOrigin) {
5706
+ console.error("Remote origin is not set yet");
5707
+ throw new Error("Remote origin is not set yet");
5708
+ }
5693
5709
  // Register or update action.
5694
5710
  // This will only pass signature info to the editor.
5695
5711
  // The actual handler is stored in this hook,
5696
5712
  // so the execution happens inside the extension app.
5697
5713
  const { appId, version, actionInfo } = await getActionInfo(params.actionName);
5698
5714
  // Setup handler
5699
- const func = await loadAppAction(actionInfo.name, appId, "/", version);
5700
- setHandler(() => func);
5715
+ const func = await loadAppAction(actionInfo.name, appId, remoteOrigin, version);
5716
+ setActionHandler(() => func);
5701
5717
  await imc?.sendMessage(IMCMessageTypeEnum.EditorRegisterAction, {
5702
5718
  name: actionInfo.name,
5703
5719
  description: actionInfo.description,
@@ -5705,30 +5721,40 @@ function useActionEffect(params, deps, isExtReady = true) {
5705
5721
  returns: actionInfo.returns,
5706
5722
  });
5707
5723
  // Update receiver
5708
- imc?.updateReceiverHandlerMap(getReceiverHandlerMap());
5724
+ imc?.updateReceiverHandlerMap(getReceiverHandlerMap(func));
5709
5725
  }
5710
- if (isExtReady) {
5726
+ if (isExtReady && remoteOrigin) {
5711
5727
  updateAction();
5712
5728
  }
5713
- }, [params.actionName, imc, isExtReady]);
5729
+ }, [
5730
+ params.actionName,
5731
+ imc,
5732
+ isExtReady,
5733
+ remoteOrigin,
5734
+ beforeAction,
5735
+ afterAction,
5736
+ ]);
5714
5737
  useEffect(() => {
5715
5738
  setBeforeAction(() => params.beforeAction ?? (async () => { }));
5716
5739
  setAfterAction(() => params.afterAction ?? (async () => { }));
5717
5740
  }, [...deps]);
5718
- async function executeAction(args) {
5741
+ async function executeAction(args, actionHandlerFunc) {
5742
+ const handler = actionHandlerFunc ?? actionHandler;
5719
5743
  if (!handler) {
5720
5744
  throw new Error("Action handler is not set");
5721
5745
  }
5746
+ let beforeRes;
5722
5747
  if (beforeAction) {
5723
- args = await beforeAction(args);
5748
+ beforeRes = await beforeAction(args);
5724
5749
  }
5725
- let res = await handler(args);
5750
+ let res = await handler(beforeRes !== undefined ? beforeRes : args);
5751
+ let afterRes;
5726
5752
  if (afterAction) {
5727
- res = await afterAction(args, res);
5753
+ afterRes = await afterAction(res);
5728
5754
  }
5729
- return res;
5755
+ return afterRes !== undefined ? afterRes : res;
5730
5756
  }
5731
- function getReceiverHandlerMap() {
5757
+ function getReceiverHandlerMap(actionHandlerFunc) {
5732
5758
  const receiverHandlerMap = new Map([
5733
5759
  [
5734
5760
  IMCMessageTypeEnum.EditorRunAppAction,
@@ -5773,7 +5799,7 @@ function useActionEffect(params, deps, isExtReady = true) {
5773
5799
  } */
5774
5800
  // If extension is ready, execute immediately
5775
5801
  if (isExtReady) {
5776
- const result = await executeAction(args);
5802
+ const result = await executeAction(args, actionHandlerFunc);
5777
5803
  return result;
5778
5804
  }
5779
5805
  // Otherwise, queue the command and return when executed
@@ -5797,7 +5823,6 @@ function useActionEffect(params, deps, isExtReady = true) {
5797
5823
  },
5798
5824
  ],
5799
5825
  });
5800
- console.log("Loaded remote from", `${remoteOrigin}/${appId}/${version}/client/remoteEntry.js`);
5801
5826
  const loadedFunc =
5802
5827
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
5803
5828
  (await instance.loadRemote(`${appId}_client/skill/${func}`)).default;
@@ -5805,7 +5830,7 @@ function useActionEffect(params, deps, isExtReady = true) {
5805
5830
  }
5806
5831
  return {
5807
5832
  isReady,
5808
- runAppAction: handler,
5833
+ runAppAction: actionHandler,
5809
5834
  };
5810
5835
  }
5811
5836