convex
Version:
Client for the Convex Cloud
106 lines (105 loc) • 3.53 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var request_manager_exports = {};
__export(request_manager_exports, {
RequestManager: () => RequestManager
});
module.exports = __toCommonJS(request_manager_exports);
var import_values = require("../../values/index.js");
var import_logging = require("../logging.js");
class RequestManager {
constructor() {
this.inflightRequests = /* @__PURE__ */ new Map();
}
request(message) {
const result = new Promise((resolve, reject) => {
this.inflightRequests.set(message.requestId, {
message,
status: { status: "Requested", onResult: resolve, onFailure: reject }
});
});
return result;
}
onResponse(response) {
const requestInfo = this.inflightRequests.get(response.requestId);
if (requestInfo === void 0) {
return null;
}
if (requestInfo.status.status !== "Requested") {
return null;
}
const udfType = "mutation";
const udfPath = requestInfo.message.udfPath;
for (const line of response.logLines) {
(0, import_logging.logToConsole)("info", udfType, udfPath, line);
}
const status = requestInfo.status;
let onResolve;
if (response.success) {
onResolve = () => status.onResult((0, import_values.jsonToConvex)(response.result));
} else {
(0, import_logging.logToConsole)("error", udfType, udfPath, response.result);
onResolve = () => status.onFailure((0, import_logging.createError)(udfType, udfPath, response.result));
}
if (!response.success) {
onResolve();
this.inflightRequests.delete(response.requestId);
return response.requestId;
}
requestInfo.status = {
status: "Completed",
ts: response.ts,
onResolve
};
return null;
}
removeCompleted(ts) {
const completeMutations = /* @__PURE__ */ new Set();
for (const [requestId, requestInfo] of this.inflightRequests.entries()) {
const status = requestInfo.status;
if (status.status === "Completed" && status.ts.lessThanOrEqual(ts)) {
status.onResolve();
if (requestInfo.message.type === "Mutation") {
completeMutations.add(requestId);
}
this.inflightRequests.delete(requestId);
}
}
return completeMutations;
}
restart() {
const allMessages = [];
for (const value of this.inflightRequests.values()) {
allMessages.push(value.message);
}
return allMessages;
}
hasIncompleteRequests() {
for (const requestInfo of this.inflightRequests.values()) {
if (requestInfo.status.status === "Requested") {
return true;
}
}
return false;
}
hasInflightRequests() {
return this.inflightRequests.size > 0;
}
}
//# sourceMappingURL=request_manager.js.map