UNPKG

convex

Version:

Client for the Convex Cloud

53 lines (52 loc) 1.6 kB
"use strict"; import { convexToJson, jsonToConvex } from "../../values/index.js"; import { createError, logToConsole } from "../logging.js"; export class ActionManager { constructor() { this.inflightActions = /* @__PURE__ */ new Map(); } request(udfPath, args, actionId) { const message = { type: "Action", actionId, udfPath, args: convexToJson(args) }; const result = new Promise((resolve, reject) => { this.inflightActions.set(actionId, { message, onResult: resolve, onFailure: reject }); }); return { message, result }; } onResponse(response) { const actionInfo = this.inflightActions.get(response.actionId); if (actionInfo === void 0) { return; } this.inflightActions.delete(response.actionId); const udfPath = actionInfo.message.udfPath; for (const line of response.logLines) { logToConsole("info", "action", udfPath, line); } if (response.success) { actionInfo.onResult(jsonToConvex(response.result)); } else { logToConsole("error", "action", udfPath, response.result); actionInfo.onFailure(createError("action", udfPath, response.result)); } } hasInflightActions() { return this.inflightActions.size > 0; } restart() { for (const [actionId, actionInfo] of this.inflightActions) { this.inflightActions.delete(actionId); const udfPath = actionInfo.message.udfPath; actionInfo.onFailure(createError("action", udfPath, "Transient error")); } } } //# sourceMappingURL=action_manager.js.map