convex
Version:
Client for the Convex Cloud
128 lines (127 loc) • 4.14 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 optimistic_updates_impl_exports = {};
__export(optimistic_updates_impl_exports, {
OptimisticQueryResults: () => OptimisticQueryResults
});
module.exports = __toCommonJS(optimistic_updates_impl_exports);
var import_logging = require("../logging.js");
var import_udf_path_utils = require("./udf_path_utils.js");
class OptimisticLocalStoreImpl {
constructor(queryResults) {
this.queryResults = queryResults;
this.modifiedQueries = [];
}
getQuery(name, args) {
const query = this.queryResults.get((0, import_udf_path_utils.serializePathAndArgs)(name, args));
if (query === void 0) {
return void 0;
}
return OptimisticLocalStoreImpl.queryValue(query.result);
}
getAllQueries(name) {
const queriesWithName = [];
for (const query of this.queryResults.values()) {
if (query.udfPath === (0, import_udf_path_utils.canonicalizeUdfPath)(name)) {
queriesWithName.push({
args: query.args,
value: OptimisticLocalStoreImpl.queryValue(query.result)
});
}
}
return queriesWithName;
}
setQuery(name, args, value) {
const queryToken = (0, import_udf_path_utils.serializePathAndArgs)(name, args);
let result;
if (value === void 0) {
result = void 0;
} else {
result = {
success: true,
value
};
}
const query = {
udfPath: name,
args,
result
};
this.queryResults.set(queryToken, query);
this.modifiedQueries.push(queryToken);
}
static queryValue(result) {
if (result === void 0) {
return void 0;
} else if (result.success) {
return result.value;
} else {
return void 0;
}
}
}
class OptimisticQueryResults {
constructor() {
this.queryResults = /* @__PURE__ */ new Map();
this.optimisticUpdates = [];
}
ingestQueryResultsFromServer(serverQueryResults, optimisticUpdatesToDrop) {
this.optimisticUpdates = this.optimisticUpdates.filter((updateAndId) => {
return !optimisticUpdatesToDrop.has(updateAndId.mutationId);
});
const oldQueryResults = this.queryResults;
this.queryResults = new Map(serverQueryResults);
const localStore = new OptimisticLocalStoreImpl(this.queryResults);
for (const updateAndId of this.optimisticUpdates) {
updateAndId.update(localStore);
}
const changedQueries = [];
for (const [queryToken, query] of this.queryResults) {
const oldQuery = oldQueryResults.get(queryToken);
if (oldQuery === void 0 || oldQuery.result !== query.result) {
changedQueries.push(queryToken);
}
}
return changedQueries;
}
applyOptimisticUpdate(update, mutationId) {
this.optimisticUpdates.push({
update,
mutationId
});
const localStore = new OptimisticLocalStoreImpl(this.queryResults);
update(localStore);
return localStore.modifiedQueries;
}
queryResult(queryToken) {
const query = this.queryResults.get(queryToken);
if (query === void 0) {
return void 0;
}
const result = query.result;
if (result === void 0) {
return void 0;
} else if (result.success) {
return result.value;
} else {
throw (0, import_logging.createError)("query", query.udfPath, result.errorMessage);
}
}
}
//# sourceMappingURL=optimistic_updates_impl.js.map