|
| 1 | +import { |
| 2 | + Action, |
| 3 | + IMCMessage, |
| 4 | + IMCMessageTypeEnum, |
| 5 | + ReceiverHandler, |
| 6 | + TypedVariable, |
| 7 | +} from "@pulse-editor/shared-utils"; |
| 8 | +import { useEffect, useRef, useState } from "react"; |
| 9 | +import useIMC from "../../lib/use-imc"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Register an app action to listen to IMC messages from the core, |
| 13 | + * and pass to the action to handle. |
| 14 | + * |
| 15 | + * @param name Name of the command. |
| 16 | + * @param description Description of the command. |
| 17 | + * @param parameters Parameters of the command. |
| 18 | + * @param returns Return values of the command. |
| 19 | + * @param callbackHandler Callback handler function to handle the command. |
| 20 | + * @param isExtReady Whether the extension is ready to receive commands. |
| 21 | + * |
| 22 | + */ |
| 23 | +export default function useRegisterAction( |
| 24 | + name: string, |
| 25 | + description: string, |
| 26 | + parameters: Record<string, TypedVariable>, |
| 27 | + returns: Record<string, TypedVariable>, |
| 28 | + callbackHandler?: (args: any) => Promise<string | void>, |
| 29 | + isExtReady: boolean = true |
| 30 | +) { |
| 31 | + const { isReady, imc } = useIMC(getReceiverHandlerMap()); |
| 32 | + |
| 33 | + // Queue to hold commands until extension is ready |
| 34 | + const commandQueue = useRef<{ args: any; resolve: (v: any) => void }[]>([]); |
| 35 | + |
| 36 | + const [action, setAction] = useState<Action>({ |
| 37 | + name, |
| 38 | + description, |
| 39 | + parameters, |
| 40 | + returns, |
| 41 | + handler: callbackHandler, |
| 42 | + }); |
| 43 | + |
| 44 | + // Flush queued commands when isExtReady becomes true |
| 45 | + useEffect(() => { |
| 46 | + if (isExtReady && commandQueue.current.length > 0) { |
| 47 | + const pending = [...commandQueue.current]; |
| 48 | + commandQueue.current = []; |
| 49 | + pending.forEach(async ({ args, resolve }) => { |
| 50 | + const res = await executeAction(args); |
| 51 | + resolve(res); |
| 52 | + }); |
| 53 | + } |
| 54 | + }, [isExtReady]); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + async function updateAction() { |
| 58 | + // Register or update action. |
| 59 | + // This will only pass signature info to the editor. |
| 60 | + // The actual handler is stored in this hook, |
| 61 | + // so the execution happens inside the extension app. |
| 62 | + await imc?.sendMessage(IMCMessageTypeEnum.EditorRegisterAction, { |
| 63 | + name: action.name, |
| 64 | + description: action.description, |
| 65 | + parameters: action.parameters, |
| 66 | + returns: action.returns, |
| 67 | + }); |
| 68 | + |
| 69 | + // Update receiver |
| 70 | + imc?.updateReceiverHandlerMap(getReceiverHandlerMap()); |
| 71 | + } |
| 72 | + |
| 73 | + if (isExtReady) { |
| 74 | + updateAction(); |
| 75 | + } |
| 76 | + }, [action, imc, isExtReady]); |
| 77 | + |
| 78 | + useEffect(() => { |
| 79 | + setAction((prev) => ({ ...prev, name, description, parameters, returns })); |
| 80 | + }, [callbackHandler]); |
| 81 | + |
| 82 | + async function executeAction(args: any) { |
| 83 | + if (!action.handler) return; |
| 84 | + |
| 85 | + const res = await action.handler(args); |
| 86 | + return res; |
| 87 | + } |
| 88 | + |
| 89 | + function getReceiverHandlerMap() { |
| 90 | + const receiverHandlerMap = new Map<IMCMessageTypeEnum, ReceiverHandler>([ |
| 91 | + [ |
| 92 | + IMCMessageTypeEnum.EditorRunAppAction, |
| 93 | + async (_senderWindow: Window, message: IMCMessage) => { |
| 94 | + const { name: requestedName, args }: { name: string; args: any } = |
| 95 | + message.payload; |
| 96 | + |
| 97 | + if (name === requestedName) { |
| 98 | + // Validate parameters |
| 99 | + const actionParams = parameters; |
| 100 | + if (Object.keys(args).length !== Object.keys(actionParams).length) { |
| 101 | + throw new Error( |
| 102 | + `Invalid number of parameters: expected ${ |
| 103 | + Object.keys(actionParams).length |
| 104 | + }, got ${Object.keys(args).length}` |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + for (const [key, value] of Object.entries(args)) { |
| 109 | + if (parameters[key] === undefined) { |
| 110 | + throw new Error(`Invalid parameter: ${key}`); |
| 111 | + } |
| 112 | + if (typeof value !== parameters[key].type) { |
| 113 | + throw new Error( |
| 114 | + `Invalid type for parameter ${key}: expected ${ |
| 115 | + parameters[key].type |
| 116 | + }, got ${typeof value}. Value received: ${value}` |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // If extension is ready, execute immediately |
| 122 | + if (isExtReady) { |
| 123 | + return await executeAction(args); |
| 124 | + } |
| 125 | + |
| 126 | + // Otherwise, queue the command and return when executed |
| 127 | + return new Promise((resolve) => { |
| 128 | + commandQueue.current.push({ args, resolve }); |
| 129 | + }); |
| 130 | + } |
| 131 | + }, |
| 132 | + ], |
| 133 | + ]); |
| 134 | + return receiverHandlerMap; |
| 135 | + } |
| 136 | + |
| 137 | + return { |
| 138 | + isReady, |
| 139 | + }; |
| 140 | +} |
0 commit comments