convex
Version:
Client for the Convex Cloud
109 lines (108 loc) • 3.07 kB
JavaScript
"use strict";
import { createError } from "../logging.js";
import {
canonicalizeUdfPath,
serializePathAndArgs
} from "./udf_path_utils.js";
class OptimisticLocalStoreImpl {
constructor(queryResults) {
this.queryResults = queryResults;
this.modifiedQueries = [];
}
getQuery(name, args) {
const query = this.queryResults.get(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 === canonicalizeUdfPath(name)) {
queriesWithName.push({
args: query.args,
value: OptimisticLocalStoreImpl.queryValue(query.result)
});
}
}
return queriesWithName;
}
setQuery(name, args, value) {
const queryToken = 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;
}
}
}
export 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 createError("query", query.udfPath, result.errorMessage);
}
}
}
//# sourceMappingURL=optimistic_updates_impl.js.map