UNPKG

graphql-norm-stale

Version:

Staleness tracking for normalized GraphQL responses

221 lines 8.34 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Makes an entity stale in the cache */ function invalidateEntity(id, recursive) { return { type: "InvalidateEntity", id: id, recursive: recursive }; } exports.invalidateEntity = invalidateEntity; /** * Makes a field stale in the cache */ function invalidateField(id, fieldName, recursive, fieldArguments) { return { type: "InvalidateField", id: id, fieldName: fieldName, recursive: recursive, fieldArguments: fieldArguments }; } exports.invalidateField = invalidateField; function applyPatches(patches, cache, staleMap) { var e_1, _a; if (patches.length === 0) { return staleMap; } // Make a shallow copy of the stale entities var staleEntitiesCopy = __assign({}, staleMap); try { for (var patches_1 = __values(patches), patches_1_1 = patches_1.next(); !patches_1_1.done; patches_1_1 = patches_1.next()) { var patch = patches_1_1.value; switch (patch.type) { case "InvalidateEntity": { applyInvalidateEntity(patch, cache, staleEntitiesCopy); break; } case "InvalidateField": { applyInvalidateField(patch, cache, staleEntitiesCopy); break; } default: { var exhaustiveCheck = function (x) { return x; }; exhaustiveCheck(patch); } } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (patches_1_1 && !patches_1_1.done && (_a = patches_1.return)) _a.call(patches_1); } finally { if (e_1) throw e_1.error; } } return staleEntitiesCopy; } exports.applyPatches = applyPatches; function applyInvalidateEntity(patch, cache, staleEntities) { var e_2, _a; var entity = cache[patch.id]; if (entity !== undefined) { var newStaleEntity = new Set(staleEntities[patch.id]); try { for (var _b = __values(Object.keys(entity)), _c = _b.next(); !_c.done; _c = _b.next()) { var entityKey = _c.value; newStaleEntity.add(entityKey); if (patch.recursive) { invalidateRecursive(cache, staleEntities, entity[entityKey]); } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } staleEntities[patch.id] = newStaleEntity; } } function applyInvalidateField(patch, cache, staleEntities) { var e_3, _a; if (cache[patch.id] !== undefined) { // We want to invalidate all fields that start with the specified // field name in order to invlidate fields with arguments // For example the fields "products" and "products(ids: [1, 2])" should // both be invalidated if the field name "products" is specified var entityFieldKeys = Object.keys(cache[patch.id]).filter(function (k) { return k.indexOf(withArgs(patch.fieldName, patch.fieldArguments)) !== -1; }); if (entityFieldKeys.length === 0) { return; } try { for (var entityFieldKeys_1 = __values(entityFieldKeys), entityFieldKeys_1_1 = entityFieldKeys_1.next(); !entityFieldKeys_1_1.done; entityFieldKeys_1_1 = entityFieldKeys_1.next()) { var fieldKey = entityFieldKeys_1_1.value; var existingFields = staleEntities[patch.id] || []; staleEntities[patch.id] = new Set(__spread(existingFields, [fieldKey])); if (patch.recursive) { // Shallow mutation of stale entities OK as we have a shallow copy invalidateRecursive(cache, staleEntities, cache[patch.id][fieldKey]); } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (entityFieldKeys_1_1 && !entityFieldKeys_1_1.done && (_a = entityFieldKeys_1.return)) _a.call(entityFieldKeys_1); } finally { if (e_3) throw e_3.error; } } } } function invalidateRecursive(cache, staleEntities, startingEntity) { var e_4, _a; if (typeof startingEntity === "number" || typeof startingEntity === "boolean" || startingEntity === "undefined" || startingEntity === "null") { return; } var stack = []; if (typeof startingEntity === "string" && cache[startingEntity]) { stack.push(startingEntity); } else if (isArrayOfEntityIds(cache, startingEntity)) { stack.push.apply(stack, __spread(startingEntity)); } while (stack.length > 0) { var entityId = stack.shift(); var entity = cache[entityId]; if (entity === undefined) { continue; } var entityFieldKeys = Object.keys(entity); var newStaleEntity = new Set(staleEntities[entityId]); try { for (var entityFieldKeys_2 = (e_4 = void 0, __values(entityFieldKeys)), entityFieldKeys_2_1 = entityFieldKeys_2.next(); !entityFieldKeys_2_1.done; entityFieldKeys_2_1 = entityFieldKeys_2.next()) { var entityFieldKey = entityFieldKeys_2_1.value; var entityField = entity[entityFieldKey]; if (isArrayOfEntityIds(cache, entityField)) { stack.push.apply(stack, __spread(entityField.filter(function (id) { return !staleEntities[id]; }))); } else if (typeof entityField === "string" && cache[entityField] && !staleEntities[entityField]) { stack.push(entityField); } newStaleEntity.add(entityFieldKey); } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (entityFieldKeys_2_1 && !entityFieldKeys_2_1.done && (_a = entityFieldKeys_2.return)) _a.call(entityFieldKeys_2); } finally { if (e_4) throw e_4.error; } } staleEntities[entityId] = newStaleEntity; } } function withArgs(fieldName, fieldArguments) { if (fieldArguments === undefined) { return fieldName; } var hashedArgs = JSON.stringify(fieldArguments); return fieldName + "(" + hashedArgs + ")"; } function isArrayOfEntityIds(cache, field) { if (Array.isArray(field) && field.some(function (x) { return !!cache[x]; })) { return true; } return false; } //# sourceMappingURL=patch.js.map