UNPKG

convex

Version:

Client for the Convex Cloud

92 lines (91 loc) 2.63 kB
"use strict"; import { createError } from "../logging.js"; import { 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; } const result = query.result; if (result === void 0) { return void 0; } else if (result.success) { return result.value; } else { return void 0; } } 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); } } 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_query_set.js.map