UNPKG

convex

Version:

Client for the Convex Cloud

225 lines (224 loc) 6.43 kB
"use strict"; 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 local_state_exports = {}; __export(local_state_exports, { LocalSyncState: () => LocalSyncState }); module.exports = __toCommonJS(local_state_exports); var import_values = require("../../values/index.js"); var import_udf_path_utils = require("./udf_path_utils.js"); class LocalSyncState { constructor() { this.nextQueryId = 0; this.querySetVersion = 0; this.identityVersion = 0; this.querySet = /* @__PURE__ */ new Map(); this.queryIdToToken = /* @__PURE__ */ new Map(); } subscribe(udfPath, args, journal) { const canonicalizedUdfPath = (0, import_udf_path_utils.canonicalizeUdfPath)(udfPath); const queryToken = (0, import_udf_path_utils.serializePathAndArgs)(canonicalizedUdfPath, args); const existingEntry = this.querySet.get(queryToken); if (existingEntry !== void 0) { existingEntry.numSubscribers += 1; return { queryToken, modification: null, unsubscribe: () => this.removeSubscriber(queryToken) }; } else { const queryId = this.nextQueryId++; const query = { id: queryId, canonicalizedUdfPath, args, numSubscribers: 1, journal }; this.querySet.set(queryToken, query); this.queryIdToToken.set(queryId, queryToken); const baseVersion = this.querySetVersion; const newVersion = ++this.querySetVersion; const add = { type: "Add", queryId, udfPath: canonicalizedUdfPath, args: args.map(import_values.convexToJson), journal }; const modification = { type: "ModifyQuerySet", baseVersion, newVersion, modifications: [add] }; return { queryToken, modification, unsubscribe: () => this.removeSubscriber(queryToken) }; } } saveQueryJournals(transition) { for (const modification of transition.modifications) { switch (modification.type) { case "QueryUpdated": case "QueryFailed": { const journal = modification.journal; if (journal !== void 0) { const queryToken = this.queryIdToToken.get(modification.queryId); if (queryToken !== void 0) { this.querySet.get(queryToken).journal = journal; } } break; } case "QueryRemoved": { break; } default: { const _ = modification; throw new Error(`Invalid modification ${modification}`); } } } } queryId(udfPath, args) { const canonicalizedUdfPath = (0, import_udf_path_utils.canonicalizeUdfPath)(udfPath); const queryToken = (0, import_udf_path_utils.serializePathAndArgs)(canonicalizedUdfPath, args); const existingEntry = this.querySet.get(queryToken); if (existingEntry !== void 0) { return existingEntry.id; } return null; } setAuth(value) { this.auth = { tokenType: "User", value }; const baseVersion = this.identityVersion++; return { type: "Authenticate", baseVersion, ...this.auth }; } setAdminAuth(value) { this.auth = { tokenType: "Admin", value }; const baseVersion = this.identityVersion++; return { type: "Authenticate", baseVersion, ...this.auth }; } clearAuth() { this.auth = void 0; const baseVersion = this.identityVersion++; return { type: "Authenticate", tokenType: "None", baseVersion }; } hasAuth() { return !!this.auth; } isNewAuth(value) { return this.auth?.value !== value; } queryPath(queryId) { const pathAndArgs = this.queryIdToToken.get(queryId); if (pathAndArgs) { return this.querySet.get(pathAndArgs).canonicalizedUdfPath; } return null; } queryArgs(queryId) { const pathAndArgs = this.queryIdToToken.get(queryId); if (pathAndArgs) { return this.querySet.get(pathAndArgs).args; } return null; } queryToken(queryId) { return this.queryIdToToken.get(queryId) ?? null; } queryJournal(queryToken) { return this.querySet.get(queryToken)?.journal; } restart() { const modifications = []; for (const localQuery of this.querySet.values()) { const add = { type: "Add", queryId: localQuery.id, udfPath: localQuery.canonicalizedUdfPath, args: localQuery.args.map(import_values.convexToJson), journal: localQuery.journal }; modifications.push(add); } this.querySetVersion = 1; const querySet = { type: "ModifyQuerySet", baseVersion: 0, newVersion: 1, modifications }; if (!this.auth) { this.identityVersion = 0; return [querySet, void 0]; } const authenticate = { type: "Authenticate", baseVersion: 0, ...this.auth }; this.identityVersion = 1; return [querySet, authenticate]; } removeSubscriber(queryToken) { const localQuery = this.querySet.get(queryToken); if (localQuery.numSubscribers > 1) { localQuery.numSubscribers -= 1; return null; } else { this.querySet.delete(queryToken); this.queryIdToToken.delete(localQuery.id); const baseVersion = this.querySetVersion; const newVersion = ++this.querySetVersion; const remove = { type: "Remove", queryId: localQuery.id }; return { type: "ModifyQuerySet", baseVersion, newVersion, modifications: [remove] }; } } } //# sourceMappingURL=local_state.js.map