'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var globals = require('../utilities/globals'); var tslib = require('tslib'); var optimism = require('optimism'); var utilities = require('../utilities'); var caches = require('@wry/caches'); var equal = require('@wry/equality'); var trie = require('@wry/trie'); var graphql = require('graphql'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; } var equal__default = /*#__PURE__*/_interopDefaultLegacy(equal); var getInMemoryCacheMemoryInternals = globalThis.__DEV__ !== false ? _getInMemoryCacheMemoryInternals : undefined; var getApolloCacheMemoryInternals = globalThis.__DEV__ !== false ? _getApolloCacheMemoryInternals : undefined; function _getApolloCacheMemoryInternals() { return { cache: { fragmentQueryDocuments: getWrapperInformation(this["getFragmentDoc"]), }, }; } function _getInMemoryCacheMemoryInternals() { var fragments = this.config.fragments; return tslib.__assign(tslib.__assign({}, _getApolloCacheMemoryInternals.apply(this)), { addTypenameDocumentTransform: transformInfo(this["addTypenameTransform"]), inMemoryCache: { executeSelectionSet: getWrapperInformation(this["storeReader"]["executeSelectionSet"]), executeSubSelectedArray: getWrapperInformation(this["storeReader"]["executeSubSelectedArray"]), maybeBroadcastWatch: getWrapperInformation(this["maybeBroadcastWatch"]), }, fragmentRegistry: { findFragmentSpreads: getWrapperInformation(fragments === null || fragments === void 0 ? void 0 : fragments.findFragmentSpreads), lookup: getWrapperInformation(fragments === null || fragments === void 0 ? void 0 : fragments.lookup), transform: getWrapperInformation(fragments === null || fragments === void 0 ? void 0 : fragments.transform), } }); } function isWrapper(f) { return !!f && "dirtyKey" in f; } function getWrapperInformation(f) { return isWrapper(f) ? f.size : undefined; } function isDefined(value) { return value != null; } function transformInfo(transform) { return recurseTransformInfo(transform).map(function (cache) { return ({ cache: cache }); }); } function recurseTransformInfo(transform) { return transform ? tslib.__spreadArray(tslib.__spreadArray([ getWrapperInformation(transform === null || transform === void 0 ? void 0 : transform["performWork"]) ], recurseTransformInfo(transform === null || transform === void 0 ? void 0 : transform["left"]), true), recurseTransformInfo(transform === null || transform === void 0 ? void 0 : transform["right"]), true).filter(isDefined) : []; } function equalByQuery(query, _a, _b, variables) { var aData = _a.data, aRest = tslib.__rest(_a, ["data"]); var bData = _b.data, bRest = tslib.__rest(_b, ["data"]); return (equal__default(aRest, bRest) && equalBySelectionSet(utilities.getMainDefinition(query).selectionSet, aData, bData, { fragmentMap: utilities.createFragmentMap(utilities.getFragmentDefinitions(query)), variables: variables, })); } function equalBySelectionSet(selectionSet, aResult, bResult, context) { if (aResult === bResult) { return true; } var seenSelections = new Set(); return selectionSet.selections.every(function (selection) { if (seenSelections.has(selection)) return true; seenSelections.add(selection); if (!utilities.shouldInclude(selection, context.variables)) return true; if (selectionHasNonreactiveDirective(selection)) return true; if (utilities.isField(selection)) { var resultKey = utilities.resultKeyNameFromField(selection); var aResultChild = aResult && aResult[resultKey]; var bResultChild = bResult && bResult[resultKey]; var childSelectionSet = selection.selectionSet; if (!childSelectionSet) { return equal__default(aResultChild, bResultChild); } var aChildIsArray = Array.isArray(aResultChild); var bChildIsArray = Array.isArray(bResultChild); if (aChildIsArray !== bChildIsArray) return false; if (aChildIsArray && bChildIsArray) { var length_1 = aResultChild.length; if (bResultChild.length !== length_1) { return false; } for (var i = 0; i < length_1; ++i) { if (!equalBySelectionSet(childSelectionSet, aResultChild[i], bResultChild[i], context)) { return false; } } return true; } return equalBySelectionSet(childSelectionSet, aResultChild, bResultChild, context); } else { var fragment = utilities.getFragmentFromSelection(selection, context.fragmentMap); if (fragment) { if (selectionHasNonreactiveDirective(fragment)) return true; return equalBySelectionSet(fragment.selectionSet, aResult, bResult, context); } } }); } function selectionHasNonreactiveDirective(selection) { return (!!selection.directives && selection.directives.some(directiveIsNonreactive)); } function directiveIsNonreactive(dir) { return dir.name.value === "nonreactive"; } var ApolloCache = (function () { function ApolloCache() { this.assumeImmutableResults = false; this.getFragmentDoc = optimism.wrap(utilities.getFragmentQueryDocument, { max: utilities.cacheSizes["cache.fragmentQueryDocuments"] || 1000 , cache: caches.WeakCache, }); } ApolloCache.prototype.batch = function (options) { var _this = this; var optimisticId = typeof options.optimistic === "string" ? options.optimistic : options.optimistic === false ? null : void 0; var updateResult; this.performTransaction(function () { return (updateResult = options.update(_this)); }, optimisticId); return updateResult; }; ApolloCache.prototype.recordOptimisticTransaction = function (transaction, optimisticId) { this.performTransaction(transaction, optimisticId); }; ApolloCache.prototype.transformDocument = function (document) { return document; }; ApolloCache.prototype.transformForLink = function (document) { return document; }; ApolloCache.prototype.identify = function (object) { return; }; ApolloCache.prototype.gc = function () { return []; }; ApolloCache.prototype.modify = function (options) { return false; }; ApolloCache.prototype.readQuery = function (options, optimistic) { if (optimistic === void 0) { optimistic = !!options.optimistic; } return this.read(tslib.__assign(tslib.__assign({}, options), { rootId: options.id || "ROOT_QUERY", optimistic: optimistic })); }; ApolloCache.prototype.watchFragment = function (options) { var _this = this; var fragment = options.fragment, fragmentName = options.fragmentName, from = options.from, _a = options.optimistic, optimistic = _a === void 0 ? true : _a, otherOptions = tslib.__rest(options, ["fragment", "fragmentName", "from", "optimistic"]); var query = this.getFragmentDoc(fragment, fragmentName); var diffOptions = tslib.__assign(tslib.__assign({}, otherOptions), { returnPartialData: true, id: typeof from === "undefined" || typeof from === "string" ? from : this.identify(from), query: query, optimistic: optimistic }); var latestDiff; return new utilities.Observable(function (observer) { return _this.watch(tslib.__assign(tslib.__assign({}, diffOptions), { immediate: true, callback: function (diff) { if ( latestDiff && equalByQuery(query, { data: latestDiff === null || latestDiff === void 0 ? void 0 : latestDiff.result }, { data: diff.result })) { return; } var result = { data: diff.result, complete: !!diff.complete, }; if (diff.missing) { result.missing = utilities.mergeDeepArray(diff.missing.map(function (error) { return error.missing; })); } latestDiff = diff; observer.next(result); } })); }); }; ApolloCache.prototype.readFragment = function (options, optimistic) { if (optimistic === void 0) { optimistic = !!options.optimistic; } return this.read(tslib.__assign(tslib.__assign({}, options), { query: this.getFragmentDoc(options.fragment, options.fragmentName), rootId: options.id, optimistic: optimistic })); }; ApolloCache.prototype.writeQuery = function (_a) { var id = _a.id, data = _a.data, options = tslib.__rest(_a, ["id", "data"]); return this.write(Object.assign(options, { dataId: id || "ROOT_QUERY", result: data, })); }; ApolloCache.prototype.writeFragment = function (_a) { var id = _a.id, data = _a.data, fragment = _a.fragment, fragmentName = _a.fragmentName, options = tslib.__rest(_a, ["id", "data", "fragment", "fragmentName"]); return this.write(Object.assign(options, { query: this.getFragmentDoc(fragment, fragmentName), dataId: id, result: data, })); }; ApolloCache.prototype.updateQuery = function (options, update) { return this.batch({ update: function (cache) { var value = cache.readQuery(options); var data = update(value); if (data === void 0 || data === null) return value; cache.writeQuery(tslib.__assign(tslib.__assign({}, options), { data: data })); return data; }, }); }; ApolloCache.prototype.updateFragment = function (options, update) { return this.batch({ update: function (cache) { var value = cache.readFragment(options); var data = update(value); if (data === void 0 || data === null) return value; cache.writeFragment(tslib.__assign(tslib.__assign({}, options), { data: data })); return data; }, }); }; return ApolloCache; }()); if (globalThis.__DEV__ !== false) { ApolloCache.prototype.getMemoryInternals = getApolloCacheMemoryInternals; } exports.Cache = void 0; (function (Cache) { })(exports.Cache || (exports.Cache = {})); var MissingFieldError = (function (_super) { tslib.__extends(MissingFieldError, _super); function MissingFieldError(message, path, query, variables) { var _a; var _this = _super.call(this, message) || this; _this.message = message; _this.path = path; _this.query = query; _this.variables = variables; if (Array.isArray(_this.path)) { _this.missing = _this.message; for (var i = _this.path.length - 1; i >= 0; --i) { _this.missing = (_a = {}, _a[_this.path[i]] = _this.missing, _a); } } else { _this.missing = _this.path; } _this.__proto__ = MissingFieldError.prototype; return _this; } return MissingFieldError; }(Error)); var hasOwn = Object.prototype.hasOwnProperty; function isNullish(value) { return value === null || value === void 0; } function defaultDataIdFromObject(_a, context) { var __typename = _a.__typename, id = _a.id, _id = _a._id; if (typeof __typename === "string") { if (context) { context.keyObject = !isNullish(id) ? { id: id } : !isNullish(_id) ? { _id: _id } : void 0; } if (isNullish(id) && !isNullish(_id)) { id = _id; } if (!isNullish(id)) { return "".concat(__typename, ":").concat(typeof id === "number" || typeof id === "string" ? id : JSON.stringify(id)); } } } var defaultConfig = { dataIdFromObject: defaultDataIdFromObject, addTypename: true, resultCaching: true, canonizeResults: false, }; function normalizeConfig(config) { return utilities.compact(defaultConfig, config); } function shouldCanonizeResults(config) { var value = config.canonizeResults; return value === void 0 ? defaultConfig.canonizeResults : value; } function getTypenameFromStoreObject(store, objectOrReference) { return utilities.isReference(objectOrReference) ? store.get(objectOrReference.__ref, "__typename") : objectOrReference && objectOrReference.__typename; } var TypeOrFieldNameRegExp = /^[_a-z][_0-9a-z]*/i; function fieldNameFromStoreName(storeFieldName) { var match = storeFieldName.match(TypeOrFieldNameRegExp); return match ? match[0] : storeFieldName; } function selectionSetMatchesResult(selectionSet, result, variables) { if (utilities.isNonNullObject(result)) { return utilities.isArray(result) ? result.every(function (item) { return selectionSetMatchesResult(selectionSet, item, variables); }) : selectionSet.selections.every(function (field) { if (utilities.isField(field) && utilities.shouldInclude(field, variables)) { var key = utilities.resultKeyNameFromField(field); return (hasOwn.call(result, key) && (!field.selectionSet || selectionSetMatchesResult(field.selectionSet, result[key], variables))); } return true; }); } return false; } function storeValueIsStoreObject(value) { return utilities.isNonNullObject(value) && !utilities.isReference(value) && !utilities.isArray(value); } function makeProcessedFieldsMerger() { return new utilities.DeepMerger(); } function extractFragmentContext(document, fragments) { var fragmentMap = utilities.createFragmentMap(utilities.getFragmentDefinitions(document)); return { fragmentMap: fragmentMap, lookupFragment: function (name) { var def = fragmentMap[name]; if (!def && fragments) { def = fragments.lookup(name); } return def || null; }, }; } var DELETE = Object.create(null); var delModifier = function () { return DELETE; }; var INVALIDATE = Object.create(null); exports.EntityStore = (function () { function EntityStore(policies, group) { var _this = this; this.policies = policies; this.group = group; this.data = Object.create(null); this.rootIds = Object.create(null); this.refs = Object.create(null); this.getFieldValue = function (objectOrReference, storeFieldName) { return utilities.maybeDeepFreeze(utilities.isReference(objectOrReference) ? _this.get(objectOrReference.__ref, storeFieldName) : objectOrReference && objectOrReference[storeFieldName]); }; this.canRead = function (objOrRef) { return utilities.isReference(objOrRef) ? _this.has(objOrRef.__ref) : typeof objOrRef === "object"; }; this.toReference = function (objOrIdOrRef, mergeIntoStore) { if (typeof objOrIdOrRef === "string") { return utilities.makeReference(objOrIdOrRef); } if (utilities.isReference(objOrIdOrRef)) { return objOrIdOrRef; } var id = _this.policies.identify(objOrIdOrRef)[0]; if (id) { var ref = utilities.makeReference(id); if (mergeIntoStore) { _this.merge(id, objOrIdOrRef); } return ref; } }; } EntityStore.prototype.toObject = function () { return tslib.__assign({}, this.data); }; EntityStore.prototype.has = function (dataId) { return this.lookup(dataId, true) !== void 0; }; EntityStore.prototype.get = function (dataId, fieldName) { this.group.depend(dataId, fieldName); if (hasOwn.call(this.data, dataId)) { var storeObject = this.data[dataId]; if (storeObject && hasOwn.call(storeObject, fieldName)) { return storeObject[fieldName]; } } if (fieldName === "__typename" && hasOwn.call(this.policies.rootTypenamesById, dataId)) { return this.policies.rootTypenamesById[dataId]; } if (this instanceof Layer) { return this.parent.get(dataId, fieldName); } }; EntityStore.prototype.lookup = function (dataId, dependOnExistence) { if (dependOnExistence) this.group.depend(dataId, "__exists"); if (hasOwn.call(this.data, dataId)) { return this.data[dataId]; } if (this instanceof Layer) { return this.parent.lookup(dataId, dependOnExistence); } if (this.policies.rootTypenamesById[dataId]) { return Object.create(null); } }; EntityStore.prototype.merge = function (older, newer) { var _this = this; var dataId; if (utilities.isReference(older)) older = older.__ref; if (utilities.isReference(newer)) newer = newer.__ref; var existing = typeof older === "string" ? this.lookup((dataId = older)) : older; var incoming = typeof newer === "string" ? this.lookup((dataId = newer)) : newer; if (!incoming) return; globals.invariant(typeof dataId === "string", 1); var merged = new utilities.DeepMerger(storeObjectReconciler).merge(existing, incoming); this.data[dataId] = merged; if (merged !== existing) { delete this.refs[dataId]; if (this.group.caching) { var fieldsToDirty_1 = Object.create(null); if (!existing) fieldsToDirty_1.__exists = 1; Object.keys(incoming).forEach(function (storeFieldName) { if (!existing || existing[storeFieldName] !== merged[storeFieldName]) { fieldsToDirty_1[storeFieldName] = 1; var fieldName = fieldNameFromStoreName(storeFieldName); if (fieldName !== storeFieldName && !_this.policies.hasKeyArgs(merged.__typename, fieldName)) { fieldsToDirty_1[fieldName] = 1; } if (merged[storeFieldName] === void 0 && !(_this instanceof Layer)) { delete merged[storeFieldName]; } } }); if (fieldsToDirty_1.__typename && !(existing && existing.__typename) && this.policies.rootTypenamesById[dataId] === merged.__typename) { delete fieldsToDirty_1.__typename; } Object.keys(fieldsToDirty_1).forEach(function (fieldName) { return _this.group.dirty(dataId, fieldName); }); } } }; EntityStore.prototype.modify = function (dataId, fields) { var _this = this; var storeObject = this.lookup(dataId); if (storeObject) { var changedFields_1 = Object.create(null); var needToMerge_1 = false; var allDeleted_1 = true; var sharedDetails_1 = { DELETE: DELETE, INVALIDATE: INVALIDATE, isReference: utilities.isReference, toReference: this.toReference, canRead: this.canRead, readField: function (fieldNameOrOptions, from) { return _this.policies.readField(typeof fieldNameOrOptions === "string" ? { fieldName: fieldNameOrOptions, from: from || utilities.makeReference(dataId), } : fieldNameOrOptions, { store: _this }); }, }; Object.keys(storeObject).forEach(function (storeFieldName) { var fieldName = fieldNameFromStoreName(storeFieldName); var fieldValue = storeObject[storeFieldName]; if (fieldValue === void 0) return; var modify = typeof fields === "function" ? fields : (fields[storeFieldName] || fields[fieldName]); if (modify) { var newValue = modify === delModifier ? DELETE : (modify(utilities.maybeDeepFreeze(fieldValue), tslib.__assign(tslib.__assign({}, sharedDetails_1), { fieldName: fieldName, storeFieldName: storeFieldName, storage: _this.getStorage(dataId, storeFieldName) }))); if (newValue === INVALIDATE) { _this.group.dirty(dataId, storeFieldName); } else { if (newValue === DELETE) newValue = void 0; if (newValue !== fieldValue) { changedFields_1[storeFieldName] = newValue; needToMerge_1 = true; fieldValue = newValue; if (globalThis.__DEV__ !== false) { var checkReference = function (ref) { if (_this.lookup(ref.__ref) === undefined) { globalThis.__DEV__ !== false && globals.invariant.warn(2, ref); return true; } }; if (utilities.isReference(newValue)) { checkReference(newValue); } else if (Array.isArray(newValue)) { var seenReference = false; var someNonReference = void 0; for (var _i = 0, newValue_1 = newValue; _i < newValue_1.length; _i++) { var value = newValue_1[_i]; if (utilities.isReference(value)) { seenReference = true; if (checkReference(value)) break; } else { if (typeof value === "object" && !!value) { var id = _this.policies.identify(value)[0]; if (id) { someNonReference = value; } } } if (seenReference && someNonReference !== undefined) { globalThis.__DEV__ !== false && globals.invariant.warn(3, someNonReference); break; } } } } } } } if (fieldValue !== void 0) { allDeleted_1 = false; } }); if (needToMerge_1) { this.merge(dataId, changedFields_1); if (allDeleted_1) { if (this instanceof Layer) { this.data[dataId] = void 0; } else { delete this.data[dataId]; } this.group.dirty(dataId, "__exists"); } return true; } } return false; }; EntityStore.prototype.delete = function (dataId, fieldName, args) { var _a; var storeObject = this.lookup(dataId); if (storeObject) { var typename = this.getFieldValue(storeObject, "__typename"); var storeFieldName = fieldName && args ? this.policies.getStoreFieldName({ typename: typename, fieldName: fieldName, args: args }) : fieldName; return this.modify(dataId, storeFieldName ? (_a = {}, _a[storeFieldName] = delModifier, _a) : delModifier); } return false; }; EntityStore.prototype.evict = function (options, limit) { var evicted = false; if (options.id) { if (hasOwn.call(this.data, options.id)) { evicted = this.delete(options.id, options.fieldName, options.args); } if (this instanceof Layer && this !== limit) { evicted = this.parent.evict(options, limit) || evicted; } if (options.fieldName || evicted) { this.group.dirty(options.id, options.fieldName || "__exists"); } } return evicted; }; EntityStore.prototype.clear = function () { this.replace(null); }; EntityStore.prototype.extract = function () { var _this = this; var obj = this.toObject(); var extraRootIds = []; this.getRootIdSet().forEach(function (id) { if (!hasOwn.call(_this.policies.rootTypenamesById, id)) { extraRootIds.push(id); } }); if (extraRootIds.length) { obj.__META = { extraRootIds: extraRootIds.sort() }; } return obj; }; EntityStore.prototype.replace = function (newData) { var _this = this; Object.keys(this.data).forEach(function (dataId) { if (!(newData && hasOwn.call(newData, dataId))) { _this.delete(dataId); } }); if (newData) { var __META = newData.__META, rest_1 = tslib.__rest(newData, ["__META"]); Object.keys(rest_1).forEach(function (dataId) { _this.merge(dataId, rest_1[dataId]); }); if (__META) { __META.extraRootIds.forEach(this.retain, this); } } }; EntityStore.prototype.retain = function (rootId) { return (this.rootIds[rootId] = (this.rootIds[rootId] || 0) + 1); }; EntityStore.prototype.release = function (rootId) { if (this.rootIds[rootId] > 0) { var count = --this.rootIds[rootId]; if (!count) delete this.rootIds[rootId]; return count; } return 0; }; EntityStore.prototype.getRootIdSet = function (ids) { if (ids === void 0) { ids = new Set(); } Object.keys(this.rootIds).forEach(ids.add, ids); if (this instanceof Layer) { this.parent.getRootIdSet(ids); } else { Object.keys(this.policies.rootTypenamesById).forEach(ids.add, ids); } return ids; }; EntityStore.prototype.gc = function () { var _this = this; var ids = this.getRootIdSet(); var snapshot = this.toObject(); ids.forEach(function (id) { if (hasOwn.call(snapshot, id)) { Object.keys(_this.findChildRefIds(id)).forEach(ids.add, ids); delete snapshot[id]; } }); var idsToRemove = Object.keys(snapshot); if (idsToRemove.length) { var root_1 = this; while (root_1 instanceof Layer) root_1 = root_1.parent; idsToRemove.forEach(function (id) { return root_1.delete(id); }); } return idsToRemove; }; EntityStore.prototype.findChildRefIds = function (dataId) { if (!hasOwn.call(this.refs, dataId)) { var found_1 = (this.refs[dataId] = Object.create(null)); var root = this.data[dataId]; if (!root) return found_1; var workSet_1 = new Set([root]); workSet_1.forEach(function (obj) { if (utilities.isReference(obj)) { found_1[obj.__ref] = true; } if (utilities.isNonNullObject(obj)) { Object.keys(obj).forEach(function (key) { var child = obj[key]; if (utilities.isNonNullObject(child)) { workSet_1.add(child); } }); } }); } return this.refs[dataId]; }; EntityStore.prototype.makeCacheKey = function () { return this.group.keyMaker.lookupArray(arguments); }; return EntityStore; }()); var CacheGroup = (function () { function CacheGroup(caching, parent) { if (parent === void 0) { parent = null; } this.caching = caching; this.parent = parent; this.d = null; this.resetCaching(); } CacheGroup.prototype.resetCaching = function () { this.d = this.caching ? optimism.dep() : null; this.keyMaker = new trie.Trie(utilities.canUseWeakMap); }; CacheGroup.prototype.depend = function (dataId, storeFieldName) { if (this.d) { this.d(makeDepKey(dataId, storeFieldName)); var fieldName = fieldNameFromStoreName(storeFieldName); if (fieldName !== storeFieldName) { this.d(makeDepKey(dataId, fieldName)); } if (this.parent) { this.parent.depend(dataId, storeFieldName); } } }; CacheGroup.prototype.dirty = function (dataId, storeFieldName) { if (this.d) { this.d.dirty(makeDepKey(dataId, storeFieldName), storeFieldName === "__exists" ? "forget" : "setDirty"); } }; return CacheGroup; }()); function makeDepKey(dataId, storeFieldName) { return storeFieldName + "#" + dataId; } function maybeDependOnExistenceOfEntity(store, entityId) { if (supportsResultCaching(store)) { store.group.depend(entityId, "__exists"); } } (function (EntityStore) { var Root = (function (_super) { tslib.__extends(Root, _super); function Root(_a) { var policies = _a.policies, _b = _a.resultCaching, resultCaching = _b === void 0 ? true : _b, seed = _a.seed; var _this = _super.call(this, policies, new CacheGroup(resultCaching)) || this; _this.stump = new Stump(_this); _this.storageTrie = new trie.Trie(utilities.canUseWeakMap); if (seed) _this.replace(seed); return _this; } Root.prototype.addLayer = function (layerId, replay) { return this.stump.addLayer(layerId, replay); }; Root.prototype.removeLayer = function () { return this; }; Root.prototype.getStorage = function () { return this.storageTrie.lookupArray(arguments); }; return Root; }(EntityStore)); EntityStore.Root = Root; })(exports.EntityStore || (exports.EntityStore = {})); var Layer = (function (_super) { tslib.__extends(Layer, _super); function Layer(id, parent, replay, group) { var _this = _super.call(this, parent.policies, group) || this; _this.id = id; _this.parent = parent; _this.replay = replay; _this.group = group; replay(_this); return _this; } Layer.prototype.addLayer = function (layerId, replay) { return new Layer(layerId, this, replay, this.group); }; Layer.prototype.removeLayer = function (layerId) { var _this = this; var parent = this.parent.removeLayer(layerId); if (layerId === this.id) { if (this.group.caching) { Object.keys(this.data).forEach(function (dataId) { var ownStoreObject = _this.data[dataId]; var parentStoreObject = parent["lookup"](dataId); if (!parentStoreObject) { _this.delete(dataId); } else if (!ownStoreObject) { _this.group.dirty(dataId, "__exists"); Object.keys(parentStoreObject).forEach(function (storeFieldName) { _this.group.dirty(dataId, storeFieldName); }); } else if (ownStoreObject !== parentStoreObject) { Object.keys(ownStoreObject).forEach(function (storeFieldName) { if (!equal.equal(ownStoreObject[storeFieldName], parentStoreObject[storeFieldName])) { _this.group.dirty(dataId, storeFieldName); } }); } }); } return parent; } if (parent === this.parent) return this; return parent.addLayer(this.id, this.replay); }; Layer.prototype.toObject = function () { return tslib.__assign(tslib.__assign({}, this.parent.toObject()), this.data); }; Layer.prototype.findChildRefIds = function (dataId) { var fromParent = this.parent.findChildRefIds(dataId); return hasOwn.call(this.data, dataId) ? tslib.__assign(tslib.__assign({}, fromParent), _super.prototype.findChildRefIds.call(this, dataId)) : fromParent; }; Layer.prototype.getStorage = function () { var p = this.parent; while (p.parent) p = p.parent; return p.getStorage.apply(p, arguments); }; return Layer; }(exports.EntityStore)); var Stump = (function (_super) { tslib.__extends(Stump, _super); function Stump(root) { return _super.call(this, "EntityStore.Stump", root, function () { }, new CacheGroup(root.group.caching, root.group)) || this; } Stump.prototype.removeLayer = function () { return this; }; Stump.prototype.merge = function (older, newer) { return this.parent.merge(older, newer); }; return Stump; }(Layer)); function storeObjectReconciler(existingObject, incomingObject, property) { var existingValue = existingObject[property]; var incomingValue = incomingObject[property]; return equal.equal(existingValue, incomingValue) ? existingValue : incomingValue; } function supportsResultCaching(store) { return !!(store instanceof exports.EntityStore && store.group.caching); } function shallowCopy(value) { if (utilities.isNonNullObject(value)) { return utilities.isArray(value) ? value.slice(0) : tslib.__assign({ __proto__: Object.getPrototypeOf(value) }, value); } return value; } var ObjectCanon = (function () { function ObjectCanon() { this.known = new (utilities.canUseWeakSet ? WeakSet : Set)(); this.pool = new trie.Trie(utilities.canUseWeakMap); this.passes = new WeakMap(); this.keysByJSON = new Map(); this.empty = this.admit({}); } ObjectCanon.prototype.isKnown = function (value) { return utilities.isNonNullObject(value) && this.known.has(value); }; ObjectCanon.prototype.pass = function (value) { if (utilities.isNonNullObject(value)) { var copy = shallowCopy(value); this.passes.set(copy, value); return copy; } return value; }; ObjectCanon.prototype.admit = function (value) { var _this = this; if (utilities.isNonNullObject(value)) { var original = this.passes.get(value); if (original) return original; var proto = Object.getPrototypeOf(value); switch (proto) { case Array.prototype: { if (this.known.has(value)) return value; var array = value.map(this.admit, this); var node = this.pool.lookupArray(array); if (!node.array) { this.known.add((node.array = array)); if (globalThis.__DEV__ !== false) { Object.freeze(array); } } return node.array; } case null: case Object.prototype: { if (this.known.has(value)) return value; var proto_1 = Object.getPrototypeOf(value); var array_1 = [proto_1]; var keys = this.sortedKeys(value); array_1.push(keys.json); var firstValueIndex_1 = array_1.length; keys.sorted.forEach(function (key) { array_1.push(_this.admit(value[key])); }); var node = this.pool.lookupArray(array_1); if (!node.object) { var obj_1 = (node.object = Object.create(proto_1)); this.known.add(obj_1); keys.sorted.forEach(function (key, i) { obj_1[key] = array_1[firstValueIndex_1 + i]; }); if (globalThis.__DEV__ !== false) { Object.freeze(obj_1); } } return node.object; } } } return value; }; ObjectCanon.prototype.sortedKeys = function (obj) { var keys = Object.keys(obj); var node = this.pool.lookupArray(keys); if (!node.keys) { keys.sort(); var json = JSON.stringify(keys); if (!(node.keys = this.keysByJSON.get(json))) { this.keysByJSON.set(json, (node.keys = { sorted: keys, json: json })); } } return node.keys; }; return ObjectCanon; }()); function execSelectionSetKeyArgs(options) { return [ options.selectionSet, options.objectOrReference, options.context, options.context.canonizeResults, ]; } var StoreReader = (function () { function StoreReader(config) { var _this = this; this.knownResults = new (utilities.canUseWeakMap ? WeakMap : Map)(); this.config = utilities.compact(config, { addTypename: config.addTypename !== false, canonizeResults: shouldCanonizeResults(config), }); this.canon = config.canon || new ObjectCanon(); this.executeSelectionSet = optimism.wrap(function (options) { var _a; var canonizeResults = options.context.canonizeResults; var peekArgs = execSelectionSetKeyArgs(options); peekArgs[3] = !canonizeResults; var other = (_a = _this.executeSelectionSet).peek.apply(_a, peekArgs); if (other) { if (canonizeResults) { return tslib.__assign(tslib.__assign({}, other), { result: _this.canon.admit(other.result) }); } return other; } maybeDependOnExistenceOfEntity(options.context.store, options.enclosingRef.__ref); return _this.execSelectionSetImpl(options); }, { max: this.config.resultCacheMaxSize || utilities.cacheSizes["inMemoryCache.executeSelectionSet"] || 50000 , keyArgs: execSelectionSetKeyArgs, makeCacheKey: function (selectionSet, parent, context, canonizeResults) { if (supportsResultCaching(context.store)) { return context.store.makeCacheKey(selectionSet, utilities.isReference(parent) ? parent.__ref : parent, context.varString, canonizeResults); } }, }); this.executeSubSelectedArray = optimism.wrap(function (options) { maybeDependOnExistenceOfEntity(options.context.store, options.enclosingRef.__ref); return _this.execSubSelectedArrayImpl(options); }, { max: this.config.resultCacheMaxSize || utilities.cacheSizes["inMemoryCache.executeSubSelectedArray"] || 10000 , makeCacheKey: function (_a) { var field = _a.field, array = _a.array, context = _a.context; if (supportsResultCaching(context.store)) { return context.store.makeCacheKey(field, array, context.varString); } }, }); } StoreReader.prototype.resetCanon = function () { this.canon = new ObjectCanon(); }; StoreReader.prototype.diffQueryAgainstStore = function (_a) { var store = _a.store, query = _a.query, _b = _a.rootId, rootId = _b === void 0 ? "ROOT_QUERY" : _b, variables = _a.variables, _c = _a.returnPartialData, returnPartialData = _c === void 0 ? true : _c, _d = _a.canonizeResults, canonizeResults = _d === void 0 ? this.config.canonizeResults : _d; var policies = this.config.cache.policies; variables = tslib.__assign(tslib.__assign({}, utilities.getDefaultValues(utilities.getQueryDefinition(query))), variables); var rootRef = utilities.makeReference(rootId); var execResult = this.executeSelectionSet({ selectionSet: utilities.getMainDefinition(query).selectionSet, objectOrReference: rootRef, enclosingRef: rootRef, context: tslib.__assign({ store: store, query: query, policies: policies, variables: variables, varString: utilities.canonicalStringify(variables), canonizeResults: canonizeResults }, extractFragmentContext(query, this.config.fragments)), }); var missing; if (execResult.missing) { missing = [ new MissingFieldError(firstMissing(execResult.missing), execResult.missing, query, variables), ]; if (!returnPartialData) { throw missing[0]; } } return { result: execResult.result, complete: !missing, missing: missing, }; }; StoreReader.prototype.isFresh = function (result, parent, selectionSet, context) { if (supportsResultCaching(context.store) && this.knownResults.get(result) === selectionSet) { var latest = this.executeSelectionSet.peek(selectionSet, parent, context, this.canon.isKnown(result)); if (latest && result === latest.result) { return true; } } return false; }; StoreReader.prototype.execSelectionSetImpl = function (_a) { var _this = this; var selectionSet = _a.selectionSet, objectOrReference = _a.objectOrReference, enclosingRef = _a.enclosingRef, context = _a.context; if (utilities.isReference(objectOrReference) && !context.policies.rootTypenamesById[objectOrReference.__ref] && !context.store.has(objectOrReference.__ref)) { return { result: this.canon.empty, missing: "Dangling reference to missing ".concat(objectOrReference.__ref, " object"), }; } var variables = context.variables, policies = context.policies, store = context.store; var typename = store.getFieldValue(objectOrReference, "__typename"); var objectsToMerge = []; var missing; var missingMerger = new utilities.DeepMerger(); if (this.config.addTypename && typeof typename === "string" && !policies.rootIdsByTypename[typename]) { objectsToMerge.push({ __typename: typename }); } function handleMissing(result, resultName) { var _a; if (result.missing) { missing = missingMerger.merge(missing, (_a = {}, _a[resultName] = result.missing, _a)); } return result.result; } var workSet = new Set(selectionSet.selections); workSet.forEach(function (selection) { var _a, _b; if (!utilities.shouldInclude(selection, variables)) return; if (utilities.isField(selection)) { var fieldValue = policies.readField({ fieldName: selection.name.value, field: selection, variables: context.variables, from: objectOrReference, }, context); var resultName = utilities.resultKeyNameFromField(selection); if (fieldValue === void 0) { if (!utilities.addTypenameToDocument.added(selection)) { missing = missingMerger.merge(missing, (_a = {}, _a[resultName] = "Can't find field '".concat(selection.name.value, "' on ").concat(utilities.isReference(objectOrReference) ? objectOrReference.__ref + " object" : "object " + JSON.stringify(objectOrReference, null, 2)), _a)); } } else if (utilities.isArray(fieldValue)) { if (fieldValue.length > 0) { fieldValue = handleMissing(_this.executeSubSelectedArray({ field: selection, array: fieldValue, enclosingRef: enclosingRef, context: context, }), resultName); } } else if (!selection.selectionSet) { if (context.canonizeResults) { fieldValue = _this.canon.pass(fieldValue); } } else if (fieldValue != null) { fieldValue = handleMissing(_this.executeSelectionSet({ selectionSet: selection.selectionSet, objectOrReference: fieldValue, enclosingRef: utilities.isReference(fieldValue) ? fieldValue : enclosingRef, context: context, }), resultName); } if (fieldValue !== void 0) { objectsToMerge.push((_b = {}, _b[resultName] = fieldValue, _b)); } } else { var fragment = utilities.getFragmentFromSelection(selection, context.lookupFragment); if (!fragment && selection.kind === graphql.Kind.FRAGMENT_SPREAD) { throw globals.newInvariantError(9, selection.name.value); } if (fragment && policies.fragmentMatches(fragment, typename)) { fragment.selectionSet.selections.forEach(workSet.add, workSet); } } }); var result = utilities.mergeDeepArray(objectsToMerge); var finalResult = { result: result, missing: missing }; var frozen = context.canonizeResults ? this.canon.admit(finalResult) : utilities.maybeDeepFreeze(finalResult); if (frozen.result) { this.knownResults.set(frozen.result, selectionSet); } return frozen; }; StoreReader.prototype.execSubSelectedArrayImpl = function (_a) { var _this = this; var field = _a.field, array = _a.array, enclosingRef = _a.enclosingRef, context = _a.context; var missing; var missingMerger = new utilities.DeepMerger(); function handleMissing(childResult, i) { var _a; if (childResult.missing) { missing = missingMerger.merge(missing, (_a = {}, _a[i] = childResult.missing, _a)); } return childResult.result; } if (field.selectionSet) { array = array.filter(context.store.canRead); } array = array.map(function (item, i) { if (item === null) { return null; } if (utilities.isArray(item)) { return handleMissing(_this.executeSubSelectedArray({ field: field, array: item, enclosingRef: enclosingRef, context: context, }), i); } if (field.selectionSet) { return handleMissing(_this.executeSelectionSet({ selectionSet: field.selectionSet, objectOrReference: item, enclosingRef: utilities.isReference(item) ? item : enclosingRef, context: context, }), i); } if (globalThis.__DEV__ !== false) { assertSelectionSetForIdValue(context.store, field, item); } return item; }); return { result: context.canonizeResults ? this.canon.admit(array) : array, missing: missing, }; }; return StoreReader; }()); function firstMissing(tree) { try { JSON.stringify(tree, function (_, value) { if (typeof value === "string") throw value; return value; }); } catch (result) { return result; } } function assertSelectionSetForIdValue(store, field, fieldValue) { if (!field.selectionSet) { var workSet_1 = new Set([fieldValue]); workSet_1.forEach(function (value) { if (utilities.isNonNullObject(value)) { globals.invariant( !utilities.isReference(value), 10, getTypenameFromStoreObject(store, value), field.name.value ); Object.values(value).forEach(workSet_1.add, workSet_1); } }); } } var cacheSlot = new optimism.Slot(); var cacheInfoMap = new WeakMap(); function getCacheInfo(cache) { var info = cacheInfoMap.get(cache); if (!info) { cacheInfoMap.set(cache, (info = { vars: new Set(), dep: optimism.dep(), })); } return info; } function forgetCache(cache) { getCacheInfo(cache).vars.forEach(function (rv) { return rv.forgetCache(cache); }); } function recallCache(cache) { getCacheInfo(cache).vars.forEach(function (rv) { return rv.attachCache(cache); }); } function makeVar(value) { var caches = new Set(); var listeners = new Set(); var rv = function (newValue) { if (arguments.length > 0) { if (value !== newValue) { value = newValue; caches.forEach(function (cache) { getCacheInfo(cache).dep.dirty(rv); broadcast(cache); }); var oldListeners = Array.from(listeners); listeners.clear(); oldListeners.forEach(function (listener) { return listener(value); }); } } else { var cache = cacheSlot.getValue(); if (cache) { attach(cache); getCacheInfo(cache).dep(rv); } } return value; }; rv.onNextChange = function (listener) { listeners.add(listener); return function () { listeners.delete(listener); }; }; var attach = (rv.attachCache = function (cache) { caches.add(cache); getCacheInfo(cache).vars.add(rv); return rv; }); rv.forgetCache = function (cache) { return caches.delete(cache); }; return rv; } function broadcast(cache) { if (cache.broadcastWatches) { cache.broadcastWatches(); } } var specifierInfoCache = Object.create(null); function lookupSpecifierInfo(spec) { var cacheKey = JSON.stringify(spec); return (specifierInfoCache[cacheKey] || (specifierInfoCache[cacheKey] = Object.create(null))); } function keyFieldsFnFromSpecifier(specifier) { var info = lookupSpecifierInfo(specifier); return (info.keyFieldsFn || (info.keyFieldsFn = function (object, context) { var extract = function (from, key) { return context.readField(key, from); }; var keyObject = (context.keyObject = collectSpecifierPaths(specifier, function (schemaKeyPath) { var extracted = extractKeyPath(context.storeObject, schemaKeyPath, extract); if (extracted === void 0 && object !== context.storeObject && hasOwn.call(object, schemaKeyPath[0])) { extracted = extractKeyPath(object, schemaKeyPath, extractKey); } globals.invariant(extracted !== void 0, 4, schemaKeyPath.join("."), object); return extracted; })); return "".concat(context.typename, ":").concat(JSON.stringify(keyObject)); })); } function keyArgsFnFromSpecifier(specifier) { var info = lookupSpecifierInfo(specifier); return (info.keyArgsFn || (info.keyArgsFn = function (args, _a) { var field = _a.field, variables = _a.variables, fieldName = _a.fieldName; var collected = collectSpecifierPaths(specifier, function (keyPath) { var firstKey = keyPath[0]; var firstChar = firstKey.charAt(0); if (firstChar === "@") { if (field && utilities.isNonEmptyArray(field.directives)) { var directiveName_1 = firstKey.slice(1); var d = field.directives.find(function (d) { return d.name.value === directiveName_1; }); var directiveArgs = d && utilities.argumentsObjectFromField(d, variables); return (directiveArgs && extractKeyPath(directiveArgs, keyPath.slice(1))); } return; } if (firstChar === "$") { var variableName = firstKey.slice(1); if (variables && hasOwn.call(variables, variableName)) { var varKeyPath = keyPath.slice(0); varKeyPath[0] = variableName; return extractKeyPath(variables, varKeyPath); } return; } if (args) { return extractKeyPath(args, keyPath); } }); var suffix = JSON.stringify(collected); if (args || suffix !== "{}") { fieldName += ":" + suffix; } return fieldName; })); } function collectSpecifierPaths(specifier, extractor) { var merger = new utilities.DeepMerger(); return getSpecifierPaths(specifier).reduce(function (collected, path) { var _a; var toMerge = extractor(path); if (toMerge !== void 0) { for (var i = path.length - 1; i >= 0; --i) { toMerge = (_a = {}, _a[path[i]] = toMerge, _a); } collected = merger.merge(collected, toMerge); } return collected; }, Object.create(null)); } function getSpecifierPaths(spec) { var info = lookupSpecifierInfo(spec); if (!info.paths) { var paths_1 = (info.paths = []); var currentPath_1 = []; spec.forEach(function (s, i) { if (utilities.isArray(s)) { getSpecifierPaths(s).forEach(function (p) { return paths_1.push(currentPath_1.concat(p)); }); currentPath_1.length = 0; } else { currentPath_1.push(s); if (!utilities.isArray(spec[i + 1])) { paths_1.push(currentPath_1.slice(0)); currentPath_1.length = 0; } } }); } return info.paths; } function extractKey(object, key) { return object[key]; } function extractKeyPath(object, path, extract) { extract = extract || extractKey; return normalize(path.reduce(function reducer(obj, key) { return utilities.isArray(obj) ? obj.map(function (child) { return reducer(child, key); }) : obj && extract(obj, key); }, object)); } function normalize(value) { if (utilities.isNonNullObject(value)) { if (utilities.isArray(value)) { return value.map(normalize); } return collectSpecifierPaths(Object.keys(value).sort(), function (path) { return extractKeyPath(value, path); }); } return value; } function argsFromFieldSpecifier(spec) { return (spec.args !== void 0 ? spec.args : spec.field ? utilities.argumentsObjectFromField(spec.field, spec.variables) : null); } var nullKeyFieldsFn = function () { return void 0; }; var simpleKeyArgsFn = function (_args, context) { return context.fieldName; }; var mergeTrueFn = function (existing, incoming, _a) { var mergeObjects = _a.mergeObjects; return mergeObjects(existing, incoming); }; var mergeFalseFn = function (_, incoming) { return incoming; }; var Policies = (function () { function Policies(config) { this.config = config; this.typePolicies = Object.create(null); this.toBeAdded = Object.create(null); this.supertypeMap = new Map(); this.fuzzySubtypes = new Map(); this.rootIdsByTypename = Object.create(null); this.rootTypenamesById = Object.create(null); this.usingPossibleTypes = false; this.config = tslib.__assign({ dataIdFromObject: defaultDataIdFromObject }, config); this.cache = this.config.cache; this.setRootTypename("Query"); this.setRootTypename("Mutation"); this.setRootTypename("Subscription"); if (config.possibleTypes) { this.addPossibleTypes(config.possibleTypes); } if (config.typePolicies) { this.addTypePolicies(config.typePolicies); } } Policies.prototype.identify = function (object, partialContext) { var _a; var policies = this; var typename = (partialContext && (partialContext.typename || ((_a = partialContext.storeObject) === null || _a === void 0 ? void 0 : _a.__typename))) || object.__typename; if (typename === this.rootTypenamesById.ROOT_QUERY) { return ["ROOT_QUERY"]; } var storeObject = (partialContext && partialContext.storeObject) || object; var context = tslib.__assign(tslib.__assign({}, partialContext), { typename: typename, storeObject: storeObject, readField: (partialContext && partialContext.readField) || function () { var options = normalizeReadFieldOptions(arguments, storeObject); return policies.readField(options, { store: policies.cache["data"], variables: options.variables, }); } }); var id; var policy = typename && this.getTypePolicy(typename); var keyFn = (policy && policy.keyFn) || this.config.dataIdFromObject; while (keyFn) { var specifierOrId = keyFn(tslib.__assign(tslib.__assign({}, object), storeObject), context); if (utilities.isArray(specifierOrId)) { keyFn = keyFieldsFnFromSpecifier(specifierOrId); } else { id = specifierOrId; break; } } id = id ? String(id) : void 0; return context.keyObject ? [id, context.keyObject] : [id]; }; Policies.prototype.addTypePolicies = function (typePolicies) { var _this = this; Object.keys(typePolicies).forEach(function (typename) { var _a = typePolicies[typename], queryType = _a.queryType, mutationType = _a.mutationType, subscriptionType = _a.subscriptionType, incoming = tslib.__rest(_a, ["queryType", "mutationType", "subscriptionType"]); if (queryType) _this.setRootTypename("Query", typename); if (mutationType) _this.setRootTypename("Mutation", typename); if (subscriptionType) _this.setRootTypename("Subscription", typename); if (hasOwn.call(_this.toBeAdded, typename)) { _this.toBeAdded[typename].push(incoming); } else { _this.toBeAdded[typename] = [incoming]; } }); }; Policies.prototype.updateTypePolicy = function (typename, incoming) { var _this = this; var existing = this.getTypePolicy(typename); var keyFields = incoming.keyFields, fields = incoming.fields; function setMerge(existing, merge) { existing.merge = typeof merge === "function" ? merge : merge === true ? mergeTrueFn : merge === false ? mergeFalseFn : existing.merge; } setMerge(existing, incoming.merge); existing.keyFn = keyFields === false ? nullKeyFieldsFn : utilities.isArray(keyFields) ? keyFieldsFnFromSpecifier(keyFields) : typeof keyFields === "function" ? keyFields : existing.keyFn; if (fields) { Object.keys(fields).forEach(function (fieldName) { var existing = _this.getFieldPolicy(typename, fieldName, true); var incoming = fields[fieldName]; if (typeof incoming === "function") { existing.read = incoming; } else { var keyArgs = incoming.keyArgs, read = incoming.read, merge = incoming.merge; existing.keyFn = keyArgs === false ? simpleKeyArgsFn : utilities.isArray(keyArgs) ? keyArgsFnFromSpecifier(keyArgs) : typeof keyArgs === "function" ? keyArgs : existing.keyFn; if (typeof read === "function") { existing.read = read; } setMerge(existing, merge); } if (existing.read && existing.merge) { existing.keyFn = existing.keyFn || simpleKeyArgsFn; } }); } }; Policies.prototype.setRootTypename = function (which, typename) { if (typename === void 0) { typename = which; } var rootId = "ROOT_" + which.toUpperCase(); var old = this.rootTypenamesById[rootId]; if (typename !== old) { globals.invariant(!old || old === which, 5, which); if (old) delete this.rootIdsByTypename[old]; this.rootIdsByTypename[typename] = rootId; this.rootTypenamesById[rootId] = typename; } }; Policies.prototype.addPossibleTypes = function (possibleTypes) { var _this = this; this.usingPossibleTypes = true; Object.keys(possibleTypes).forEach(function (supertype) { _this.getSupertypeSet(supertype, true); possibleTypes[supertype].forEach(function (subtype) { _this.getSupertypeSet(subtype, true).add(supertype); var match = subtype.match(TypeOrFieldNameRegExp); if (!match || match[0] !== subtype) { _this.fuzzySubtypes.set(subtype, new RegExp(subtype)); } }); }); }; Policies.prototype.getTypePolicy = function (typename) { var _this = this; if (!hasOwn.call(this.typePolicies, typename)) { var policy_1 = (this.typePolicies[typename] = Object.create(null)); policy_1.fields = Object.create(null); var supertypes_1 = this.supertypeMap.get(typename); if (!supertypes_1 && this.fuzzySubtypes.size) { supertypes_1 = this.getSupertypeSet(typename, true); this.fuzzySubtypes.forEach(function (regExp, fuzzy) { if (regExp.test(typename)) { var fuzzySupertypes = _this.supertypeMap.get(fuzzy); if (fuzzySupertypes) { fuzzySupertypes.forEach(function (supertype) { return supertypes_1.add(supertype); }); } } }); } if (supertypes_1 && supertypes_1.size) { supertypes_1.forEach(function (supertype) { var _a = _this.getTypePolicy(supertype), fields = _a.fields, rest = tslib.__rest(_a, ["fields"]); Object.assign(policy_1, rest); Object.assign(policy_1.fields, fields); }); } } var inbox = this.toBeAdded[typename]; if (inbox && inbox.length) { inbox.splice(0).forEach(function (policy) { _this.updateTypePolicy(typename, policy); }); } return this.typePolicies[typename]; }; Policies.prototype.getFieldPolicy = function (typename, fieldName, createIfMissing) { if (typename) { var fieldPolicies = this.getTypePolicy(typename).fields; return (fieldPolicies[fieldName] || (createIfMissing && (fieldPolicies[fieldName] = Object.create(null)))); } }; Policies.prototype.getSupertypeSet = function (subtype, createIfMissing) { var supertypeSet = this.supertypeMap.get(subtype); if (!supertypeSet && createIfMissing) { this.supertypeMap.set(subtype, (supertypeSet = new Set())); } return supertypeSet; }; Policies.prototype.fragmentMatches = function (fragment, typename, result, variables) { var _this = this; if (!fragment.typeCondition) return true; if (!typename) return false; var supertype = fragment.typeCondition.name.value; if (typename === supertype) return true; if (this.usingPossibleTypes && this.supertypeMap.has(supertype)) { var typenameSupertypeSet = this.getSupertypeSet(typename, true); var workQueue_1 = [typenameSupertypeSet]; var maybeEnqueue_1 = function (subtype) { var supertypeSet = _this.getSupertypeSet(subtype, false); if (supertypeSet && supertypeSet.size && workQueue_1.indexOf(supertypeSet) < 0) { workQueue_1.push(supertypeSet); } }; var needToCheckFuzzySubtypes = !!(result && this.fuzzySubtypes.size); var checkingFuzzySubtypes = false; for (var i = 0; i < workQueue_1.length; ++i) { var supertypeSet = workQueue_1[i]; if (supertypeSet.has(supertype)) { if (!typenameSupertypeSet.has(supertype)) { if (checkingFuzzySubtypes) { globalThis.__DEV__ !== false && globals.invariant.warn(6, typename, supertype); } typenameSupertypeSet.add(supertype); } return true; } supertypeSet.forEach(maybeEnqueue_1); if (needToCheckFuzzySubtypes && i === workQueue_1.length - 1 && selectionSetMatchesResult(fragment.selectionSet, result, variables)) { needToCheckFuzzySubtypes = false; checkingFuzzySubtypes = true; this.fuzzySubtypes.forEach(function (regExp, fuzzyString) { var match = typename.match(regExp); if (match && match[0] === typename) { maybeEnqueue_1(fuzzyString); } }); } } } return false; }; Policies.prototype.hasKeyArgs = function (typename, fieldName) { var policy = this.getFieldPolicy(typename, fieldName, false); return !!(policy && policy.keyFn); }; Policies.prototype.getStoreFieldName = function (fieldSpec) { var typename = fieldSpec.typename, fieldName = fieldSpec.fieldName; var policy = this.getFieldPolicy(typename, fieldName, false); var storeFieldName; var keyFn = policy && policy.keyFn; if (keyFn && typename) { var context = { typename: typename, fieldName: fieldName, field: fieldSpec.field || null, variables: fieldSpec.variables, }; var args = argsFromFieldSpecifier(fieldSpec); while (keyFn) { var specifierOrString = keyFn(args, context); if (utilities.isArray(specifierOrString)) { keyFn = keyArgsFnFromSpecifier(specifierOrString); } else { storeFieldName = specifierOrString || fieldName; break; } } } if (storeFieldName === void 0) { storeFieldName = fieldSpec.field ? utilities.storeKeyNameFromField(fieldSpec.field, fieldSpec.variables) : utilities.getStoreKeyName(fieldName, argsFromFieldSpecifier(fieldSpec)); } if (storeFieldName === false) { return fieldName; } return fieldName === fieldNameFromStoreName(storeFieldName) ? storeFieldName : fieldName + ":" + storeFieldName; }; Policies.prototype.readField = function (options, context) { var objectOrReference = options.from; if (!objectOrReference) return; var nameOrField = options.field || options.fieldName; if (!nameOrField) return; if (options.typename === void 0) { var typename = context.store.getFieldValue(objectOrReference, "__typename"); if (typename) options.typename = typename; } var storeFieldName = this.getStoreFieldName(options); var fieldName = fieldNameFromStoreName(storeFieldName); var existing = context.store.getFieldValue(objectOrReference, storeFieldName); var policy = this.getFieldPolicy(options.typename, fieldName, false); var read = policy && policy.read; if (read) { var readOptions = makeFieldFunctionOptions(this, objectOrReference, options, context, context.store.getStorage(utilities.isReference(objectOrReference) ? objectOrReference.__ref : objectOrReference, storeFieldName)); return cacheSlot.withValue(this.cache, read, [ existing, readOptions, ]); } return existing; }; Policies.prototype.getReadFunction = function (typename, fieldName) { var policy = this.getFieldPolicy(typename, fieldName, false); return policy && policy.read; }; Policies.prototype.getMergeFunction = function (parentTypename, fieldName, childTypename) { var policy = this.getFieldPolicy(parentTypename, fieldName, false); var merge = policy && policy.merge; if (!merge && childTypename) { policy = this.getTypePolicy(childTypename); merge = policy && policy.merge; } return merge; }; Policies.prototype.runMergeFunction = function (existing, incoming, _a, context, storage) { var field = _a.field, typename = _a.typename, merge = _a.merge; if (merge === mergeTrueFn) { return makeMergeObjectsFunction(context.store)(existing, incoming); } if (merge === mergeFalseFn) { return incoming; } if (context.overwrite) { existing = void 0; } return merge(existing, incoming, makeFieldFunctionOptions(this, void 0, { typename: typename, fieldName: field.name.value, field: field, variables: context.variables, }, context, storage || Object.create(null))); }; return Policies; }()); function makeFieldFunctionOptions(policies, objectOrReference, fieldSpec, context, storage) { var storeFieldName = policies.getStoreFieldName(fieldSpec); var fieldName = fieldNameFromStoreName(storeFieldName); var variables = fieldSpec.variables || context.variables; var _a = context.store, toReference = _a.toReference, canRead = _a.canRead; return { args: argsFromFieldSpecifier(fieldSpec), field: fieldSpec.field || null, fieldName: fieldName, storeFieldName: storeFieldName, variables: variables, isReference: utilities.isReference, toReference: toReference, storage: storage, cache: policies.cache, canRead: canRead, readField: function () { return policies.readField(normalizeReadFieldOptions(arguments, objectOrReference, variables), context); }, mergeObjects: makeMergeObjectsFunction(context.store), }; } function normalizeReadFieldOptions(readFieldArgs, objectOrReference, variables) { var fieldNameOrOptions = readFieldArgs[0], from = readFieldArgs[1], argc = readFieldArgs.length; var options; if (typeof fieldNameOrOptions === "string") { options = { fieldName: fieldNameOrOptions, from: argc > 1 ? from : objectOrReference, }; } else { options = tslib.__assign({}, fieldNameOrOptions); if (!hasOwn.call(options, "from")) { options.from = objectOrReference; } } if (globalThis.__DEV__ !== false && options.from === void 0) { globalThis.__DEV__ !== false && globals.invariant.warn(7, utilities.stringifyForDisplay(Array.from(readFieldArgs))); } if (void 0 === options.variables) { options.variables = variables; } return options; } function makeMergeObjectsFunction(store) { return function mergeObjects(existing, incoming) { if (utilities.isArray(existing) || utilities.isArray(incoming)) { throw globals.newInvariantError(8); } if (utilities.isNonNullObject(existing) && utilities.isNonNullObject(incoming)) { var eType = store.getFieldValue(existing, "__typename"); var iType = store.getFieldValue(incoming, "__typename"); var typesDiffer = eType && iType && eType !== iType; if (typesDiffer) { return incoming; } if (utilities.isReference(existing) && storeValueIsStoreObject(incoming)) { store.merge(existing.__ref, incoming); return existing; } if (storeValueIsStoreObject(existing) && utilities.isReference(incoming)) { store.merge(existing, incoming.__ref); return incoming; } if (storeValueIsStoreObject(existing) && storeValueIsStoreObject(incoming)) { return tslib.__assign(tslib.__assign({}, existing), incoming); } } return incoming; }; } function getContextFlavor(context, clientOnly, deferred) { var key = "".concat(clientOnly).concat(deferred); var flavored = context.flavors.get(key); if (!flavored) { context.flavors.set(key, (flavored = context.clientOnly === clientOnly && context.deferred === deferred ? context : tslib.__assign(tslib.__assign({}, context), { clientOnly: clientOnly, deferred: deferred }))); } return flavored; } var StoreWriter = (function () { function StoreWriter(cache, reader, fragments) { this.cache = cache; this.reader = reader; this.fragments = fragments; } StoreWriter.prototype.writeToStore = function (store, _a) { var _this = this; var query = _a.query, result = _a.result, dataId = _a.dataId, variables = _a.variables, overwrite = _a.overwrite; var operationDefinition = utilities.getOperationDefinition(query); var merger = makeProcessedFieldsMerger(); variables = tslib.__assign(tslib.__assign({}, utilities.getDefaultValues(operationDefinition)), variables); var context = tslib.__assign(tslib.__assign({ store: store, written: Object.create(null), merge: function (existing, incoming) { return merger.merge(existing, incoming); }, variables: variables, varString: utilities.canonicalStringify(variables) }, extractFragmentContext(query, this.fragments)), { overwrite: !!overwrite, incomingById: new Map(), clientOnly: false, deferred: false, flavors: new Map() }); var ref = this.processSelectionSet({ result: result || Object.create(null), dataId: dataId, selectionSet: operationDefinition.selectionSet, mergeTree: { map: new Map() }, context: context, }); if (!utilities.isReference(ref)) { throw globals.newInvariantError(11, result); } context.incomingById.forEach(function (_a, dataId) { var storeObject = _a.storeObject, mergeTree = _a.mergeTree, fieldNodeSet = _a.fieldNodeSet; var entityRef = utilities.makeReference(dataId); if (mergeTree && mergeTree.map.size) { var applied = _this.applyMerges(mergeTree, entityRef, storeObject, context); if (utilities.isReference(applied)) { return; } storeObject = applied; } if (globalThis.__DEV__ !== false && !context.overwrite) { var fieldsWithSelectionSets_1 = Object.create(null); fieldNodeSet.forEach(function (field) { if (field.selectionSet) { fieldsWithSelectionSets_1[field.name.value] = true; } }); var hasSelectionSet_1 = function (storeFieldName) { return fieldsWithSelectionSets_1[fieldNameFromStoreName(storeFieldName)] === true; }; var hasMergeFunction_1 = function (storeFieldName) { var childTree = mergeTree && mergeTree.map.get(storeFieldName); return Boolean(childTree && childTree.info && childTree.info.merge); }; Object.keys(storeObject).forEach(function (storeFieldName) { if (hasSelectionSet_1(storeFieldName) && !hasMergeFunction_1(storeFieldName)) { warnAboutDataLoss(entityRef, storeObject, storeFieldName, context.store); } }); } store.merge(dataId, storeObject); }); store.retain(ref.__ref); return ref; }; StoreWriter.prototype.processSelectionSet = function (_a) { var _this = this; var dataId = _a.dataId, result = _a.result, selectionSet = _a.selectionSet, context = _a.context, mergeTree = _a.mergeTree; var policies = this.cache.policies; var incoming = Object.create(null); var typename = (dataId && policies.rootTypenamesById[dataId]) || utilities.getTypenameFromResult(result, selectionSet, context.fragmentMap) || (dataId && context.store.get(dataId, "__typename")); if ("string" === typeof typename) { incoming.__typename = typename; } var readField = function () { var options = normalizeReadFieldOptions(arguments, incoming, context.variables); if (utilities.isReference(options.from)) { var info = context.incomingById.get(options.from.__ref); if (info) { var result_1 = policies.readField(tslib.__assign(tslib.__assign({}, options), { from: info.storeObject }), context); if (result_1 !== void 0) { return result_1; } } } return policies.readField(options, context); }; var fieldNodeSet = new Set(); this.flattenFields(selectionSet, result, context, typename).forEach(function (context, field) { var _a; var resultFieldKey = utilities.resultKeyNameFromField(field); var value = result[resultFieldKey]; fieldNodeSet.add(field); if (value !== void 0) { var storeFieldName = policies.getStoreFieldName({ typename: typename, fieldName: field.name.value, field: field, variables: context.variables, }); var childTree = getChildMergeTree(mergeTree, storeFieldName); var incomingValue = _this.processFieldValue(value, field, field.selectionSet ? getContextFlavor(context, false, false) : context, childTree); var childTypename = void 0; if (field.selectionSet && (utilities.isReference(incomingValue) || storeValueIsStoreObject(incomingValue))) { childTypename = readField("__typename", incomingValue); } var merge = policies.getMergeFunction(typename, field.name.value, childTypename); if (merge) { childTree.info = { field: field, typename: typename, merge: merge, }; } else { maybeRecycleChildMergeTree(mergeTree, storeFieldName); } incoming = context.merge(incoming, (_a = {}, _a[storeFieldName] = incomingValue, _a)); } else if (globalThis.__DEV__ !== false && !context.clientOnly && !context.deferred && !utilities.addTypenameToDocument.added(field) && !policies.getReadFunction(typename, field.name.value)) { globalThis.__DEV__ !== false && globals.invariant.error(12, utilities.resultKeyNameFromField(field), result); } }); try { var _b = policies.identify(result, { typename: typename, selectionSet: selectionSet, fragmentMap: context.fragmentMap, storeObject: incoming, readField: readField, }), id = _b[0], keyObject = _b[1]; dataId = dataId || id; if (keyObject) { incoming = context.merge(incoming, keyObject); } } catch (e) { if (!dataId) throw e; } if ("string" === typeof dataId) { var dataRef = utilities.makeReference(dataId); var sets = context.written[dataId] || (context.written[dataId] = []); if (sets.indexOf(selectionSet) >= 0) return dataRef; sets.push(selectionSet); if (this.reader && this.reader.isFresh(result, dataRef, selectionSet, context)) { return dataRef; } var previous_1 = context.incomingById.get(dataId); if (previous_1) { previous_1.storeObject = context.merge(previous_1.storeObject, incoming); previous_1.mergeTree = mergeMergeTrees(previous_1.mergeTree, mergeTree); fieldNodeSet.forEach(function (field) { return previous_1.fieldNodeSet.add(field); }); } else { context.incomingById.set(dataId, { storeObject: incoming, mergeTree: mergeTreeIsEmpty(mergeTree) ? void 0 : mergeTree, fieldNodeSet: fieldNodeSet, }); } return dataRef; } return incoming; }; StoreWriter.prototype.processFieldValue = function (value, field, context, mergeTree) { var _this = this; if (!field.selectionSet || value === null) { return globalThis.__DEV__ !== false ? utilities.cloneDeep(value) : value; } if (utilities.isArray(value)) { return value.map(function (item, i) { var value = _this.processFieldValue(item, field, context, getChildMergeTree(mergeTree, i)); maybeRecycleChildMergeTree(mergeTree, i); return value; }); } return this.processSelectionSet({ result: value, selectionSet: field.selectionSet, context: context, mergeTree: mergeTree, }); }; StoreWriter.prototype.flattenFields = function (selectionSet, result, context, typename) { if (typename === void 0) { typename = utilities.getTypenameFromResult(result, selectionSet, context.fragmentMap); } var fieldMap = new Map(); var policies = this.cache.policies; var limitingTrie = new trie.Trie(false); (function flatten(selectionSet, inheritedContext) { var visitedNode = limitingTrie.lookup(selectionSet, inheritedContext.clientOnly, inheritedContext.deferred); if (visitedNode.visited) return; visitedNode.visited = true; selectionSet.selections.forEach(function (selection) { if (!utilities.shouldInclude(selection, context.variables)) return; var clientOnly = inheritedContext.clientOnly, deferred = inheritedContext.deferred; if ( !(clientOnly && deferred) && utilities.isNonEmptyArray(selection.directives)) { selection.directives.forEach(function (dir) { var name = dir.name.value; if (name === "client") clientOnly = true; if (name === "defer") { var args = utilities.argumentsObjectFromField(dir, context.variables); if (!args || args.if !== false) { deferred = true; } } }); } if (utilities.isField(selection)) { var existing = fieldMap.get(selection); if (existing) { clientOnly = clientOnly && existing.clientOnly; deferred = deferred && existing.deferred; } fieldMap.set(selection, getContextFlavor(context, clientOnly, deferred)); } else { var fragment = utilities.getFragmentFromSelection(selection, context.lookupFragment); if (!fragment && selection.kind === graphql.Kind.FRAGMENT_SPREAD) { throw globals.newInvariantError(13, selection.name.value); } if (fragment && policies.fragmentMatches(fragment, typename, result, context.variables)) { flatten(fragment.selectionSet, getContextFlavor(context, clientOnly, deferred)); } } }); })(selectionSet, context); return fieldMap; }; StoreWriter.prototype.applyMerges = function (mergeTree, existing, incoming, context, getStorageArgs) { var _a; var _this = this; if (mergeTree.map.size && !utilities.isReference(incoming)) { var e_1 = (!utilities.isArray(incoming) && (utilities.isReference(existing) || storeValueIsStoreObject(existing))) ? existing : void 0; var i_1 = incoming; if (e_1 && !getStorageArgs) { getStorageArgs = [utilities.isReference(e_1) ? e_1.__ref : e_1]; } var changedFields_1; var getValue_1 = function (from, name) { return (utilities.isArray(from) ? typeof name === "number" ? from[name] : void 0 : context.store.getFieldValue(from, String(name))); }; mergeTree.map.forEach(function (childTree, storeFieldName) { var eVal = getValue_1(e_1, storeFieldName); var iVal = getValue_1(i_1, storeFieldName); if (void 0 === iVal) return; if (getStorageArgs) { getStorageArgs.push(storeFieldName); } var aVal = _this.applyMerges(childTree, eVal, iVal, context, getStorageArgs); if (aVal !== iVal) { changedFields_1 = changedFields_1 || new Map(); changedFields_1.set(storeFieldName, aVal); } if (getStorageArgs) { globals.invariant(getStorageArgs.pop() === storeFieldName); } }); if (changedFields_1) { incoming = (utilities.isArray(i_1) ? i_1.slice(0) : tslib.__assign({}, i_1)); changedFields_1.forEach(function (value, name) { incoming[name] = value; }); } } if (mergeTree.info) { return this.cache.policies.runMergeFunction(existing, incoming, mergeTree.info, context, getStorageArgs && (_a = context.store).getStorage.apply(_a, getStorageArgs)); } return incoming; }; return StoreWriter; }()); var emptyMergeTreePool = []; function getChildMergeTree(_a, name) { var map = _a.map; if (!map.has(name)) { map.set(name, emptyMergeTreePool.pop() || { map: new Map() }); } return map.get(name); } function mergeMergeTrees(left, right) { if (left === right || !right || mergeTreeIsEmpty(right)) return left; if (!left || mergeTreeIsEmpty(left)) return right; var info = left.info && right.info ? tslib.__assign(tslib.__assign({}, left.info), right.info) : left.info || right.info; var needToMergeMaps = left.map.size && right.map.size; var map = needToMergeMaps ? new Map() : left.map.size ? left.map : right.map; var merged = { info: info, map: map }; if (needToMergeMaps) { var remainingRightKeys_1 = new Set(right.map.keys()); left.map.forEach(function (leftTree, key) { merged.map.set(key, mergeMergeTrees(leftTree, right.map.get(key))); remainingRightKeys_1.delete(key); }); remainingRightKeys_1.forEach(function (key) { merged.map.set(key, mergeMergeTrees(right.map.get(key), left.map.get(key))); }); } return merged; } function mergeTreeIsEmpty(tree) { return !tree || !(tree.info || tree.map.size); } function maybeRecycleChildMergeTree(_a, name) { var map = _a.map; var childTree = map.get(name); if (childTree && mergeTreeIsEmpty(childTree)) { emptyMergeTreePool.push(childTree); map.delete(name); } } var warnings = new Set(); function warnAboutDataLoss(existingRef, incomingObj, storeFieldName, store) { var getChild = function (objOrRef) { var child = store.getFieldValue(objOrRef, storeFieldName); return typeof child === "object" && child; }; var existing = getChild(existingRef); if (!existing) return; var incoming = getChild(incomingObj); if (!incoming) return; if (utilities.isReference(existing)) return; if (equal.equal(existing, incoming)) return; if (Object.keys(existing).every(function (key) { return store.getFieldValue(incoming, key) !== void 0; })) { return; } var parentType = store.getFieldValue(existingRef, "__typename") || store.getFieldValue(incomingObj, "__typename"); var fieldName = fieldNameFromStoreName(storeFieldName); var typeDotName = "".concat(parentType, ".").concat(fieldName); if (warnings.has(typeDotName)) return; warnings.add(typeDotName); var childTypenames = []; if (!utilities.isArray(existing) && !utilities.isArray(incoming)) { [existing, incoming].forEach(function (child) { var typename = store.getFieldValue(child, "__typename"); if (typeof typename === "string" && !childTypenames.includes(typename)) { childTypenames.push(typename); } }); } globalThis.__DEV__ !== false && globals.invariant.warn(14, fieldName, parentType, childTypenames.length ? "either ensure all objects of type " + childTypenames.join(" and ") + " have an ID or a custom merge function, or " : "", typeDotName, tslib.__assign({}, existing), tslib.__assign({}, incoming)); } var InMemoryCache = (function (_super) { tslib.__extends(InMemoryCache, _super); function InMemoryCache(config) { if (config === void 0) { config = {}; } var _this = _super.call(this) || this; _this.watches = new Set(); _this.addTypenameTransform = new utilities.DocumentTransform(utilities.addTypenameToDocument); _this.assumeImmutableResults = true; _this.makeVar = makeVar; _this.txCount = 0; _this.config = normalizeConfig(config); _this.addTypename = !!_this.config.addTypename; _this.policies = new Policies({ cache: _this, dataIdFromObject: _this.config.dataIdFromObject, possibleTypes: _this.config.possibleTypes, typePolicies: _this.config.typePolicies, }); _this.init(); return _this; } InMemoryCache.prototype.init = function () { var rootStore = (this.data = new exports.EntityStore.Root({ policies: this.policies, resultCaching: this.config.resultCaching, })); this.optimisticData = rootStore.stump; this.resetResultCache(); }; InMemoryCache.prototype.resetResultCache = function (resetResultIdentities) { var _this = this; var previousReader = this.storeReader; var fragments = this.config.fragments; this.storeWriter = new StoreWriter(this, (this.storeReader = new StoreReader({ cache: this, addTypename: this.addTypename, resultCacheMaxSize: this.config.resultCacheMaxSize, canonizeResults: shouldCanonizeResults(this.config), canon: resetResultIdentities ? void 0 : (previousReader && previousReader.canon), fragments: fragments, })), fragments); this.maybeBroadcastWatch = optimism.wrap(function (c, options) { return _this.broadcastWatch(c, options); }, { max: this.config.resultCacheMaxSize || utilities.cacheSizes["inMemoryCache.maybeBroadcastWatch"] || 5000 , makeCacheKey: function (c) { var store = c.optimistic ? _this.optimisticData : _this.data; if (supportsResultCaching(store)) { var optimistic = c.optimistic, id = c.id, variables = c.variables; return store.makeCacheKey(c.query, c.callback, utilities.canonicalStringify({ optimistic: optimistic, id: id, variables: variables })); } }, }); new Set([this.data.group, this.optimisticData.group]).forEach(function (group) { return group.resetCaching(); }); }; InMemoryCache.prototype.restore = function (data) { this.init(); if (data) this.data.replace(data); return this; }; InMemoryCache.prototype.extract = function (optimistic) { if (optimistic === void 0) { optimistic = false; } return (optimistic ? this.optimisticData : this.data).extract(); }; InMemoryCache.prototype.read = function (options) { var _a = options.returnPartialData, returnPartialData = _a === void 0 ? false : _a; try { return (this.storeReader.diffQueryAgainstStore(tslib.__assign(tslib.__assign({}, options), { store: options.optimistic ? this.optimisticData : this.data, config: this.config, returnPartialData: returnPartialData })).result || null); } catch (e) { if (e instanceof MissingFieldError) { return null; } throw e; } }; InMemoryCache.prototype.write = function (options) { try { ++this.txCount; return this.storeWriter.writeToStore(this.data, options); } finally { if (!--this.txCount && options.broadcast !== false) { this.broadcastWatches(); } } }; InMemoryCache.prototype.modify = function (options) { if (hasOwn.call(options, "id") && !options.id) { return false; } var store = ((options.optimistic) ) ? this.optimisticData : this.data; try { ++this.txCount; return store.modify(options.id || "ROOT_QUERY", options.fields); } finally { if (!--this.txCount && options.broadcast !== false) { this.broadcastWatches(); } } }; InMemoryCache.prototype.diff = function (options) { return this.storeReader.diffQueryAgainstStore(tslib.__assign(tslib.__assign({}, options), { store: options.optimistic ? this.optimisticData : this.data, rootId: options.id || "ROOT_QUERY", config: this.config })); }; InMemoryCache.prototype.watch = function (watch) { var _this = this; if (!this.watches.size) { recallCache(this); } this.watches.add(watch); if (watch.immediate) { this.maybeBroadcastWatch(watch); } return function () { if (_this.watches.delete(watch) && !_this.watches.size) { forgetCache(_this); } _this.maybeBroadcastWatch.forget(watch); }; }; InMemoryCache.prototype.gc = function (options) { var _a; utilities.canonicalStringify.reset(); utilities.print.reset(); this.addTypenameTransform.resetCache(); (_a = this.config.fragments) === null || _a === void 0 ? void 0 : _a.resetCaches(); var ids = this.optimisticData.gc(); if (options && !this.txCount) { if (options.resetResultCache) { this.resetResultCache(options.resetResultIdentities); } else if (options.resetResultIdentities) { this.storeReader.resetCanon(); } } return ids; }; InMemoryCache.prototype.retain = function (rootId, optimistic) { return (optimistic ? this.optimisticData : this.data).retain(rootId); }; InMemoryCache.prototype.release = function (rootId, optimistic) { return (optimistic ? this.optimisticData : this.data).release(rootId); }; InMemoryCache.prototype.identify = function (object) { if (utilities.isReference(object)) return object.__ref; try { return this.policies.identify(object)[0]; } catch (e) { globalThis.__DEV__ !== false && globals.invariant.warn(e); } }; InMemoryCache.prototype.evict = function (options) { if (!options.id) { if (hasOwn.call(options, "id")) { return false; } options = tslib.__assign(tslib.__assign({}, options), { id: "ROOT_QUERY" }); } try { ++this.txCount; return this.optimisticData.evict(options, this.data); } finally { if (!--this.txCount && options.broadcast !== false) { this.broadcastWatches(); } } }; InMemoryCache.prototype.reset = function (options) { var _this = this; this.init(); utilities.canonicalStringify.reset(); if (options && options.discardWatches) { this.watches.forEach(function (watch) { return _this.maybeBroadcastWatch.forget(watch); }); this.watches.clear(); forgetCache(this); } else { this.broadcastWatches(); } return Promise.resolve(); }; InMemoryCache.prototype.removeOptimistic = function (idToRemove) { var newOptimisticData = this.optimisticData.removeLayer(idToRemove); if (newOptimisticData !== this.optimisticData) { this.optimisticData = newOptimisticData; this.broadcastWatches(); } }; InMemoryCache.prototype.batch = function (options) { var _this = this; var update = options.update, _a = options.optimistic, optimistic = _a === void 0 ? true : _a, removeOptimistic = options.removeOptimistic, onWatchUpdated = options.onWatchUpdated; var updateResult; var perform = function (layer) { var _a = _this, data = _a.data, optimisticData = _a.optimisticData; ++_this.txCount; if (layer) { _this.data = _this.optimisticData = layer; } try { return (updateResult = update(_this)); } finally { --_this.txCount; _this.data = data; _this.optimisticData = optimisticData; } }; var alreadyDirty = new Set(); if (onWatchUpdated && !this.txCount) { this.broadcastWatches(tslib.__assign(tslib.__assign({}, options), { onWatchUpdated: function (watch) { alreadyDirty.add(watch); return false; } })); } if (typeof optimistic === "string") { this.optimisticData = this.optimisticData.addLayer(optimistic, perform); } else if (optimistic === false) { perform(this.data); } else { perform(); } if (typeof removeOptimistic === "string") { this.optimisticData = this.optimisticData.removeLayer(removeOptimistic); } if (onWatchUpdated && alreadyDirty.size) { this.broadcastWatches(tslib.__assign(tslib.__assign({}, options), { onWatchUpdated: function (watch, diff) { var result = onWatchUpdated.call(this, watch, diff); if (result !== false) { alreadyDirty.delete(watch); } return result; } })); if (alreadyDirty.size) { alreadyDirty.forEach(function (watch) { return _this.maybeBroadcastWatch.dirty(watch); }); } } else { this.broadcastWatches(options); } return updateResult; }; InMemoryCache.prototype.performTransaction = function (update, optimisticId) { return this.batch({ update: update, optimistic: optimisticId || optimisticId !== null, }); }; InMemoryCache.prototype.transformDocument = function (document) { return this.addTypenameToDocument(this.addFragmentsToDocument(document)); }; InMemoryCache.prototype.broadcastWatches = function (options) { var _this = this; if (!this.txCount) { this.watches.forEach(function (c) { return _this.maybeBroadcastWatch(c, options); }); } }; InMemoryCache.prototype.addFragmentsToDocument = function (document) { var fragments = this.config.fragments; return fragments ? fragments.transform(document) : document; }; InMemoryCache.prototype.addTypenameToDocument = function (document) { if (this.addTypename) { return this.addTypenameTransform.transformDocument(document); } return document; }; InMemoryCache.prototype.broadcastWatch = function (c, options) { var lastDiff = c.lastDiff; var diff = this.diff(c); if (options) { if (c.optimistic && typeof options.optimistic === "string") { diff.fromOptimisticTransaction = true; } if (options.onWatchUpdated && options.onWatchUpdated.call(this, c, diff, lastDiff) === false) { return; } } if (!lastDiff || !equal.equal(lastDiff.result, diff.result)) { c.callback((c.lastDiff = diff), lastDiff); } }; return InMemoryCache; }(ApolloCache)); if (globalThis.__DEV__ !== false) { InMemoryCache.prototype.getMemoryInternals = getInMemoryCacheMemoryInternals; } function createFragmentRegistry() { var fragments = []; for (var _i = 0; _i < arguments.length; _i++) { fragments[_i] = arguments[_i]; } return new (FragmentRegistry.bind.apply(FragmentRegistry, tslib.__spreadArray([void 0], fragments, false)))(); } var FragmentRegistry = (function () { function FragmentRegistry() { var fragments = []; for (var _i = 0; _i < arguments.length; _i++) { fragments[_i] = arguments[_i]; } this.registry = Object.create(null); this.resetCaches(); if (fragments.length) { this.register.apply(this, fragments); } } FragmentRegistry.prototype.register = function () { var _this = this; var fragments = []; for (var _i = 0; _i < arguments.length; _i++) { fragments[_i] = arguments[_i]; } var definitions = new Map(); fragments.forEach(function (doc) { utilities.getFragmentDefinitions(doc).forEach(function (node) { definitions.set(node.name.value, node); }); }); definitions.forEach(function (node, name) { if (node !== _this.registry[name]) { _this.registry[name] = node; _this.invalidate(name); } }); return this; }; FragmentRegistry.prototype.invalidate = function (name) { }; FragmentRegistry.prototype.resetCaches = function () { var proto = FragmentRegistry.prototype; this.invalidate = (this.lookup = optimism.wrap(proto.lookup.bind(this), { makeCacheKey: function (arg) { return arg; }, max: utilities.cacheSizes["fragmentRegistry.lookup"] || 1000 , })).dirty; this.transform = optimism.wrap(proto.transform.bind(this), { cache: caches.WeakCache, max: utilities.cacheSizes["fragmentRegistry.transform"] || 2000 , }); this.findFragmentSpreads = optimism.wrap(proto.findFragmentSpreads.bind(this), { cache: caches.WeakCache, max: utilities.cacheSizes["fragmentRegistry.findFragmentSpreads"] || 4000 , }); }; FragmentRegistry.prototype.lookup = function (fragmentName) { return this.registry[fragmentName] || null; }; FragmentRegistry.prototype.transform = function (document) { var _this = this; var defined = new Map(); utilities.getFragmentDefinitions(document).forEach(function (def) { defined.set(def.name.value, def); }); var unbound = new Set(); var enqueue = function (spreadName) { if (!defined.has(spreadName)) { unbound.add(spreadName); } }; var enqueueChildSpreads = function (node) { return Object.keys(_this.findFragmentSpreads(node)).forEach(enqueue); }; enqueueChildSpreads(document); var missing = []; var map = Object.create(null); unbound.forEach(function (fragmentName) { var knownFragmentDef = defined.get(fragmentName); if (knownFragmentDef) { enqueueChildSpreads((map[fragmentName] = knownFragmentDef)); } else { missing.push(fragmentName); var def = _this.lookup(fragmentName); if (def) { enqueueChildSpreads((map[fragmentName] = def)); } } }); if (missing.length) { var defsToAppend_1 = []; missing.forEach(function (name) { var def = map[name]; if (def) { defsToAppend_1.push(def); } }); if (defsToAppend_1.length) { document = tslib.__assign(tslib.__assign({}, document), { definitions: document.definitions.concat(defsToAppend_1) }); } } return document; }; FragmentRegistry.prototype.findFragmentSpreads = function (root) { var spreads = Object.create(null); graphql.visit(root, { FragmentSpread: function (node) { spreads[node.name.value] = node; }, }); return spreads; }; return FragmentRegistry; }()); exports.canonicalStringify = utilities.canonicalStringify; exports.isReference = utilities.isReference; exports.makeReference = utilities.makeReference; exports.ApolloCache = ApolloCache; exports.InMemoryCache = InMemoryCache; exports.MissingFieldError = MissingFieldError; exports.Policies = Policies; exports.cacheSlot = cacheSlot; exports.createFragmentRegistry = createFragmentRegistry; exports.defaultDataIdFromObject = defaultDataIdFromObject; exports.fieldNameFromStoreName = fieldNameFromStoreName; exports.makeVar = makeVar; //# sourceMappingURL=cache.cjs.map