@hotmeshio/hotmesh
Version:
Permanent-Memory Workflows & AI Agents
57 lines (56 loc) • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.waitFor = void 0;
const common_1 = require("./common");
const didRun_1 = require("./didRun");
/**
* Pauses the workflow until a signal with the given `signalId` is received.
* This method is commonly used to coordinate between the main workflow and hook functions,
* or to wait for external events.
*
* @example
* // Basic usage - wait for a single signal
* const payload = await MemFlow.workflow.waitFor<PayloadType>('abcdefg');
*
* @example
* // Wait for multiple signals in parallel
* const [signal1, signal2] = await Promise.all([
* MemFlow.workflow.waitFor<Record<string, any>>('signal1'),
* MemFlow.workflow.waitFor<Record<string, any>>('signal2')
* ]);
*
* @example
* // Typical pattern with hook functions
* // In main workflow:
* await MemFlow.workflow.waitFor<ResponseType>('hook-complete');
*
* // In hook function:
* await MemFlow.workflow.signal('hook-complete', { data: result });
*
* @template T - The type of data expected in the signal payload
* @param {string} signalId - A unique signal identifier shared by the sender and receiver.
* @returns {Promise<T>} The data payload associated with the received signal.
*/
async function waitFor(signalId) {
const [didRunAlready, execIndex, result] = await (0, didRun_1.didRun)('wait');
if (didRunAlready) {
return result.data.data;
}
const store = common_1.asyncLocalStorage.getStore();
const interruptionRegistry = store.get('interruptionRegistry');
const workflowId = store.get('workflowId');
const workflowDimension = store.get('workflowDimension') ?? '';
const interruptionMessage = {
workflowId,
signalId,
index: execIndex,
workflowDimension,
type: 'MemFlowWaitForError',
code: common_1.HMSH_CODE_MEMFLOW_WAIT,
};
interruptionRegistry.push(interruptionMessage);
await (0, common_1.sleepImmediate)();
//if you are seeing this error in the logs, you might have forgotten to `await waitFor(...)`
throw new common_1.MemFlowWaitForError(interruptionMessage);
}
exports.waitFor = waitFor;