@nerdwallet/apollo-cache-policies
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -0,1 +1,5 @@ | ||
2.3.0 (Dan Reynolds) | ||
* Update TSConfig | ||
2.2.0 (Dan Reynolds) | ||
@@ -2,0 +6,0 @@ |
@@ -1,38 +0,34 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const filter_1 = __importDefault(require("lodash/filter")); | ||
class AuditLog { | ||
constructor() { | ||
import _filter from 'lodash/filter'; | ||
var AuditLog = /** @class */ (function () { | ||
function AuditLog() { | ||
this._log = []; | ||
} | ||
log(event, type, group, meta) { | ||
const auditLogEntry = { | ||
AuditLog.prototype.log = function (event, type, group, meta) { | ||
var auditLogEntry = { | ||
time: Date.now(), | ||
type, | ||
event, | ||
meta, | ||
group, | ||
type: type, | ||
event: event, | ||
meta: meta, | ||
group: group, | ||
}; | ||
this._log.push(auditLogEntry); | ||
} | ||
getLog(filter) { | ||
return filter_1.default(this._log, filter); | ||
} | ||
printLog(filter) { | ||
}; | ||
AuditLog.prototype.getLog = function (filter) { | ||
return _filter(this._log, filter); | ||
}; | ||
AuditLog.prototype.printLog = function (filter) { | ||
this.getLog(filter).forEach(this.printLogEntry); | ||
} | ||
printLogEntry(entry) { | ||
}; | ||
AuditLog.prototype.printLogEntry = function (entry) { | ||
var _a; | ||
console.log(`%c event: ${entry.event}`, "color: green"); | ||
console.log("%c event: " + entry.event, "color: green"); | ||
console.group(); | ||
console.log(`type: ${entry.type}`); | ||
console.log(`time: ${entry.time}`); | ||
console.log(`meta: ${JSON.stringify((_a = entry.meta) !== null && _a !== void 0 ? _a : {})}`); | ||
console.log("type: " + entry.type); | ||
console.log("time: " + entry.time); | ||
console.log("meta: " + JSON.stringify((_a = entry.meta) !== null && _a !== void 0 ? _a : {})); | ||
console.groupEnd(); | ||
} | ||
} | ||
exports.default = AuditLog; | ||
}; | ||
return AuditLog; | ||
}()); | ||
export default AuditLog; | ||
//# sourceMappingURL=AuditLog.js.map |
@@ -1,7 +0,17 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const EntityTypeMap_1 = __importDefault(require("../entity-store/EntityTypeMap")); | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
import EntityTypeMap from "../entity-store/EntityTypeMap"; | ||
var EntityTypeMapAuditorEvent; | ||
@@ -12,25 +22,28 @@ (function (EntityTypeMapAuditorEvent) { | ||
})(EntityTypeMapAuditorEvent || (EntityTypeMapAuditorEvent = {})); | ||
class EntityTypeMapAudtior extends EntityTypeMap_1.default { | ||
constructor(config) { | ||
super(); | ||
this.auditLog = config.auditLog; | ||
var EntityTypeMapAudtior = /** @class */ (function (_super) { | ||
__extends(EntityTypeMapAudtior, _super); | ||
function EntityTypeMapAudtior(config) { | ||
var _this = _super.call(this) || this; | ||
_this.auditLog = config.auditLog; | ||
return _this; | ||
} | ||
write(typename, dataId, storeFieldName, variables) { | ||
EntityTypeMapAudtior.prototype.write = function (typename, dataId, storeFieldName, variables) { | ||
this.auditLog.log("Writing to type map", EntityTypeMapAuditorEvent.Write, "EntityTypeMap", { | ||
typename, | ||
dataId, | ||
storeFieldName, | ||
variables, | ||
typename: typename, | ||
dataId: dataId, | ||
storeFieldName: storeFieldName, | ||
variables: variables, | ||
}); | ||
return super.write(typename, dataId, storeFieldName, variables); | ||
} | ||
evict(dataId, storeFieldName) { | ||
return _super.prototype.write.call(this, typename, dataId, storeFieldName, variables); | ||
}; | ||
EntityTypeMapAudtior.prototype.evict = function (dataId, storeFieldName) { | ||
this.auditLog.log("Evicting from type map", EntityTypeMapAuditorEvent.Evict, "EntityTypeMap", { | ||
dataId, | ||
storeFieldName, | ||
dataId: dataId, | ||
storeFieldName: storeFieldName, | ||
}); | ||
return super.evict(dataId, storeFieldName); | ||
} | ||
} | ||
exports.default = EntityTypeMapAudtior; | ||
return _super.prototype.evict.call(this, dataId, storeFieldName); | ||
}; | ||
return EntityTypeMapAudtior; | ||
}(EntityTypeMap)); | ||
export default EntityTypeMapAudtior; | ||
//# sourceMappingURL=EntityTypeMapAuditor.js.map |
@@ -1,9 +0,2 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InvalidationPolicyCacheAuditor = void 0; | ||
var InvalidationPolicyCacheAuditor_1 = require("./InvalidationPolicyCacheAuditor"); | ||
Object.defineProperty(exports, "InvalidationPolicyCacheAuditor", { enumerable: true, get: function () { return __importDefault(InvalidationPolicyCacheAuditor_1).default; } }); | ||
export { default as InvalidationPolicyCacheAuditor } from "./InvalidationPolicyCacheAuditor"; | ||
//# sourceMappingURL=index.js.map |
@@ -1,42 +0,55 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const InvalidationPolicyCache_1 = __importDefault(require("../cache/InvalidationPolicyCache")); | ||
const CacheResultProcessor_1 = require("../cache/CacheResultProcessor"); | ||
const InvalidationPolicyManagerAuditor_1 = __importDefault(require("./InvalidationPolicyManagerAuditor")); | ||
const EntityTypeMapAuditor_1 = __importDefault(require("./EntityTypeMapAuditor")); | ||
const AuditLog_1 = __importDefault(require("./AuditLog")); | ||
const entity_store_1 = require("../entity-store"); | ||
class InvalidationPolicyCacheAuditor extends InvalidationPolicyCache_1.default { | ||
constructor(config) { | ||
super(config); | ||
this.auditLog = new AuditLog_1.default(); | ||
const { invalidationPolicies = {} } = config; | ||
this.entityTypeMap = new EntityTypeMapAuditor_1.default({ auditLog: this.auditLog }); | ||
new entity_store_1.EntityStoreWatcher({ | ||
entityStore: this.entityStoreRoot, | ||
entityTypeMap: this.entityTypeMap, | ||
policies: this.policies, | ||
updateCollectionField: this.updateCollectionField.bind(this), | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
import InvalidationPolicyCache from "../cache/InvalidationPolicyCache"; | ||
import { CacheResultProcessor } from "../cache/CacheResultProcessor"; | ||
import InvalidationPolicyManagerAuditor from "./InvalidationPolicyManagerAuditor"; | ||
import EntityTypeMapAuditor from "./EntityTypeMapAuditor"; | ||
import AuditLog from "./AuditLog"; | ||
import { EntityStoreWatcher } from "../entity-store"; | ||
var InvalidationPolicyCacheAuditor = /** @class */ (function (_super) { | ||
__extends(InvalidationPolicyCacheAuditor, _super); | ||
function InvalidationPolicyCacheAuditor(config) { | ||
var _this = _super.call(this, config) || this; | ||
_this.auditLog = new AuditLog(); | ||
var _a = config.invalidationPolicies, invalidationPolicies = _a === void 0 ? {} : _a; | ||
_this.entityTypeMap = new EntityTypeMapAuditor({ auditLog: _this.auditLog }); | ||
new EntityStoreWatcher({ | ||
entityStore: _this.entityStoreRoot, | ||
entityTypeMap: _this.entityTypeMap, | ||
policies: _this.policies, | ||
updateCollectionField: _this.updateCollectionField.bind(_this), | ||
}); | ||
this.invalidationPolicyManager = new InvalidationPolicyManagerAuditor_1.default({ | ||
auditLog: this.auditLog, | ||
_this.invalidationPolicyManager = new InvalidationPolicyManagerAuditor({ | ||
auditLog: _this.auditLog, | ||
policies: invalidationPolicies, | ||
entityTypeMap: this.entityTypeMap, | ||
entityTypeMap: _this.entityTypeMap, | ||
cacheOperations: { | ||
evict: this.evict.bind(this), | ||
modify: this.modify.bind(this), | ||
readField: this.readField.bind(this), | ||
evict: _this.evict.bind(_this), | ||
modify: _this.modify.bind(_this), | ||
readField: _this.readField.bind(_this), | ||
}, | ||
}); | ||
this.cacheResultProcessor = new CacheResultProcessor_1.CacheResultProcessor({ | ||
invalidationPolicyManager: this.invalidationPolicyManager, | ||
entityTypeMap: this.entityTypeMap, | ||
cache: this, | ||
_this.cacheResultProcessor = new CacheResultProcessor({ | ||
invalidationPolicyManager: _this.invalidationPolicyManager, | ||
entityTypeMap: _this.entityTypeMap, | ||
cache: _this, | ||
}); | ||
return _this; | ||
} | ||
} | ||
exports.default = InvalidationPolicyCacheAuditor; | ||
return InvalidationPolicyCacheAuditor; | ||
}(InvalidationPolicyCache)); | ||
export default InvalidationPolicyCacheAuditor; | ||
//# sourceMappingURL=InvalidationPolicyCacheAuditor.js.map |
@@ -1,9 +0,29 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
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); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AuditType = void 0; | ||
const InvalidationPolicyManager_1 = __importDefault(require("../policies/InvalidationPolicyManager")); | ||
var AuditType; | ||
import InvalidationPolicyManager from "../policies/InvalidationPolicyManager"; | ||
export var AuditType; | ||
(function (AuditType) { | ||
@@ -13,33 +33,37 @@ AuditType["Read"] = "Read"; | ||
AuditType["Evict"] = "Evict"; | ||
})(AuditType = exports.AuditType || (exports.AuditType = {})); | ||
class InvalidationPolicyManagerAuditor extends InvalidationPolicyManager_1.default { | ||
constructor(config) { | ||
super(config); | ||
this.auditLog = config.auditLog; | ||
})(AuditType || (AuditType = {})); | ||
var InvalidationPolicyManagerAuditor = /** @class */ (function (_super) { | ||
__extends(InvalidationPolicyManagerAuditor, _super); | ||
function InvalidationPolicyManagerAuditor(config) { | ||
var _this = _super.call(this, config) || this; | ||
_this.auditLog = config.auditLog; | ||
return _this; | ||
} | ||
runReadPolicy({ typename, dataId, fieldName, storeFieldName, reportOnly = false, }) { | ||
InvalidationPolicyManagerAuditor.prototype.runReadPolicy = function (_a) { | ||
var typename = _a.typename, dataId = _a.dataId, fieldName = _a.fieldName, storeFieldName = _a.storeFieldName, _b = _a.reportOnly, reportOnly = _b === void 0 ? false : _b; | ||
this.auditLog.log("Running read policy", AuditType.Read, "InvalidationPolicyManager", { | ||
storeFieldName, | ||
typename, | ||
dataId, | ||
fieldName, | ||
storeFieldName: storeFieldName, | ||
typename: typename, | ||
dataId: dataId, | ||
fieldName: fieldName, | ||
}); | ||
return super.runReadPolicy({ | ||
typename, | ||
dataId, | ||
fieldName, | ||
storeFieldName, | ||
reportOnly, | ||
return _super.prototype.runReadPolicy.call(this, { | ||
typename: typename, | ||
dataId: dataId, | ||
fieldName: fieldName, | ||
storeFieldName: storeFieldName, | ||
reportOnly: reportOnly, | ||
}); | ||
} | ||
runWritePolicy(typeName, policyMeta) { | ||
this.auditLog.log("Running write policy", AuditType.Write, "InvalidationPolicyManager", Object.assign({ typeName }, policyMeta)); | ||
return super.runWritePolicy(typeName, policyMeta); | ||
} | ||
runEvictPolicy(typeName, policyMeta) { | ||
this.auditLog.log("Running evict policy", AuditType.Write, "InvalidationPolicyManager", Object.assign({ typeName }, policyMeta)); | ||
return super.runEvictPolicy(typeName, policyMeta); | ||
} | ||
} | ||
exports.default = InvalidationPolicyManagerAuditor; | ||
}; | ||
InvalidationPolicyManagerAuditor.prototype.runWritePolicy = function (typeName, policyMeta) { | ||
this.auditLog.log("Running write policy", AuditType.Write, "InvalidationPolicyManager", __assign({ typeName: typeName }, policyMeta)); | ||
return _super.prototype.runWritePolicy.call(this, typeName, policyMeta); | ||
}; | ||
InvalidationPolicyManagerAuditor.prototype.runEvictPolicy = function (typeName, policyMeta) { | ||
this.auditLog.log("Running evict policy", AuditType.Write, "InvalidationPolicyManager", __assign({ typeName: typeName }, policyMeta)); | ||
return _super.prototype.runEvictPolicy.call(this, typeName, policyMeta); | ||
}; | ||
return InvalidationPolicyManagerAuditor; | ||
}(InvalidationPolicyManager)); | ||
export default InvalidationPolicyManagerAuditor; | ||
//# sourceMappingURL=InvalidationPolicyManagerAuditor.js.map |
@@ -1,14 +0,8 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CacheResultProcessor = exports.ReadResultStatus = void 0; | ||
const core_1 = require("@apollo/client/core"); | ||
const isPlainObject_1 = __importDefault(require("lodash/isPlainObject")); | ||
const isArray_1 = __importDefault(require("lodash/isArray")); | ||
const utilities_1 = require("@apollo/client/utilities"); | ||
const helpers_1 = require("../helpers"); | ||
const types_1 = require("../policies/types"); | ||
var ReadResultStatus; | ||
import { makeReference } from "@apollo/client/core"; | ||
import isPlainObject from "lodash/isPlainObject"; | ||
import isArray from "lodash/isArray"; | ||
import { argumentsObjectFromField, createFragmentMap, getFragmentDefinitions, getFragmentFromSelection, getOperationDefinition, isField, maybeDeepFreeze, resultKeyNameFromField, } from "@apollo/client/utilities"; | ||
import { makeEntityId, isQuery } from "../helpers"; | ||
import { RenewalPolicy } from "../policies/types"; | ||
export var ReadResultStatus; | ||
(function (ReadResultStatus) { | ||
@@ -18,44 +12,47 @@ ReadResultStatus[ReadResultStatus["Evicted"] = 0] = "Evicted"; | ||
ReadResultStatus[ReadResultStatus["Complete"] = 2] = "Complete"; | ||
})(ReadResultStatus = exports.ReadResultStatus || (exports.ReadResultStatus = {})); | ||
})(ReadResultStatus || (ReadResultStatus = {})); | ||
/** | ||
* Processes the result of a cache read/write to run cache policies on the deeply nested objects. | ||
*/ | ||
class CacheResultProcessor { | ||
constructor(config) { | ||
var CacheResultProcessor = /** @class */ (function () { | ||
function CacheResultProcessor(config) { | ||
this.config = config; | ||
} | ||
getFieldsForQuery(options) { | ||
const operationDefinition = utilities_1.getOperationDefinition(options.query); | ||
const fragmentMap = utilities_1.createFragmentMap(utilities_1.getFragmentDefinitions(options.query)); | ||
return operationDefinition.selectionSet.selections.reduce((acc, selection) => { | ||
CacheResultProcessor.prototype.getFieldsForQuery = function (options) { | ||
var operationDefinition = getOperationDefinition(options.query); | ||
var fragmentMap = createFragmentMap(getFragmentDefinitions(options.query)); | ||
return operationDefinition.selectionSet.selections.reduce(function (acc, selection) { | ||
var _a, _b; | ||
if (utilities_1.isField(selection)) { | ||
if (isField(selection)) { | ||
acc.push(selection); | ||
return acc; | ||
} | ||
const selections = (_b = (_a = utilities_1.getFragmentFromSelection(selection, fragmentMap)) === null || _a === void 0 ? void 0 : _a.selectionSet) === null || _b === void 0 ? void 0 : _b.selections; | ||
var selections = (_b = (_a = getFragmentFromSelection(selection, fragmentMap)) === null || _a === void 0 ? void 0 : _a.selectionSet) === null || _b === void 0 ? void 0 : _b.selections; | ||
if (selections) { | ||
acc.push(...selections); | ||
acc.push.apply(acc, selections); | ||
} | ||
return acc; | ||
}, []); | ||
} | ||
processReadSubResult(parentResult, fieldNameOrIndex) { | ||
const { cache, invalidationPolicyManager, entityTypeMap } = this.config; | ||
const result = fieldNameOrIndex == undefined | ||
}; | ||
CacheResultProcessor.prototype.processReadSubResult = function (parentResult, fieldNameOrIndex) { | ||
var _this = this; | ||
var _a = this.config, cache = _a.cache, invalidationPolicyManager = _a.invalidationPolicyManager, entityTypeMap = _a.entityTypeMap; | ||
var result = fieldNameOrIndex == undefined | ||
? parentResult | ||
: parentResult[fieldNameOrIndex]; | ||
if (isPlainObject_1.default(result)) { | ||
const { __typename } = result; | ||
const aggregateResultComplete = Object.keys(result).reduce((_acc, fieldName) => this.processReadSubResult(result, fieldName) === | ||
ReadResultStatus.Complete, true); | ||
if (isPlainObject(result)) { | ||
var __typename = result.__typename; | ||
var aggregateResultComplete = Object.keys(result).reduce(function (_acc, fieldName) { | ||
return _this.processReadSubResult(result, fieldName) === | ||
ReadResultStatus.Complete; | ||
}, true); | ||
if (__typename) { | ||
const id = cache.identify(result); | ||
var id = cache.identify(result); | ||
if (id) { | ||
const renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(__typename); | ||
if (renewalPolicy === types_1.RenewalPolicy.AccessAndWrite || | ||
renewalPolicy === types_1.RenewalPolicy.AccessOnly) { | ||
var renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(__typename); | ||
if (renewalPolicy === RenewalPolicy.AccessAndWrite || | ||
renewalPolicy === RenewalPolicy.AccessOnly) { | ||
entityTypeMap.renewEntity(id); | ||
} | ||
const evicted = invalidationPolicyManager.runReadPolicy({ | ||
var evicted = invalidationPolicyManager.runReadPolicy({ | ||
typename: __typename, | ||
@@ -65,3 +62,3 @@ dataId: id | ||
if (evicted) { | ||
if (isPlainObject_1.default(parentResult) && fieldNameOrIndex) { | ||
if (isPlainObject(parentResult) && fieldNameOrIndex) { | ||
delete parentResult[fieldNameOrIndex]; | ||
@@ -77,16 +74,18 @@ } | ||
} | ||
else if (isArray_1.default(result)) { | ||
let aggregateSubResultStatus = ReadResultStatus.Complete; | ||
const subResultStatuses = result.map((_subResult, index) => { | ||
const subResultStatus = this.processReadSubResult(result, index); | ||
if (subResultStatus < aggregateSubResultStatus) { | ||
aggregateSubResultStatus = subResultStatus; | ||
else if (isArray(result)) { | ||
var aggregateSubResultStatus_1 = ReadResultStatus.Complete; | ||
var subResultStatuses_1 = result.map(function (_subResult, index) { | ||
var subResultStatus = _this.processReadSubResult(result, index); | ||
if (subResultStatus < aggregateSubResultStatus_1) { | ||
aggregateSubResultStatus_1 = subResultStatus; | ||
} | ||
return subResultStatus; | ||
}); | ||
if (aggregateSubResultStatus === ReadResultStatus.Evicted && | ||
if (aggregateSubResultStatus_1 === ReadResultStatus.Evicted && | ||
fieldNameOrIndex) { | ||
parentResult[fieldNameOrIndex] = result.filter((_subResult, index) => subResultStatuses[index] !== ReadResultStatus.Evicted); | ||
parentResult[fieldNameOrIndex] = result.filter(function (_subResult, index) { | ||
return subResultStatuses_1[index] !== ReadResultStatus.Evicted; | ||
}); | ||
} | ||
return aggregateSubResultStatus === ReadResultStatus.Complete | ||
return aggregateSubResultStatus_1 === ReadResultStatus.Complete | ||
? ReadResultStatus.Complete | ||
@@ -96,48 +95,49 @@ : ReadResultStatus.Incomplete; | ||
return ReadResultStatus.Complete; | ||
} | ||
processReadResult(result, options) { | ||
const { cache, entityTypeMap, invalidationPolicyManager } = this.config; | ||
const { rootId: dataId = "ROOT_QUERY" } = options; | ||
if (isPlainObject_1.default(result)) { | ||
if (helpers_1.isQuery(dataId)) { | ||
const { variables } = options; | ||
const aggregateResultComplete = this.getFieldsForQuery(options).reduce((acc, field) => { | ||
}; | ||
CacheResultProcessor.prototype.processReadResult = function (result, options) { | ||
var _this = this; | ||
var _a = this.config, cache = _a.cache, entityTypeMap = _a.entityTypeMap, invalidationPolicyManager = _a.invalidationPolicyManager; | ||
var _b = options.rootId, dataId = _b === void 0 ? "ROOT_QUERY" : _b; | ||
if (isPlainObject(result)) { | ||
if (isQuery(dataId)) { | ||
var variables_1 = options.variables; | ||
var aggregateResultComplete = this.getFieldsForQuery(options).reduce(function (acc, field) { | ||
var _a; | ||
const fieldName = field.name.value; | ||
var fieldName = field.name.value; | ||
// While the field name is used as the key in the cache, the result object | ||
// will have it keyed by an alias name if provided so we keep track of the | ||
// result key name in case it needs to be removed from the response due to an evicted TTL | ||
const resultKeyName = utilities_1.resultKeyNameFromField(field); | ||
const subResultStatus = this.processReadSubResult(result, fieldName); | ||
const typename = (_a = entityTypeMap.readEntityById(helpers_1.makeEntityId(dataId, fieldName))) === null || _a === void 0 ? void 0 : _a.typename; | ||
var resultKeyName = resultKeyNameFromField(field); | ||
var subResultStatus = _this.processReadSubResult(result, fieldName); | ||
var typename = (_a = entityTypeMap.readEntityById(makeEntityId(dataId, fieldName))) === null || _a === void 0 ? void 0 : _a.typename; | ||
if (typename) { | ||
const storeFieldNameForEntity = cache.policies.getStoreFieldName({ | ||
typename, | ||
fieldName, | ||
field, | ||
variables, | ||
var storeFieldNameForEntity = cache.policies.getStoreFieldName({ | ||
typename: typename, | ||
fieldName: fieldName, | ||
field: field, | ||
variables: variables_1, | ||
}); | ||
const queryTypename = cache.policies.rootTypenamesById[dataId]; | ||
const storeFieldNameForQuery = cache.policies.getStoreFieldName({ | ||
var queryTypename = cache.policies.rootTypenamesById[dataId]; | ||
var storeFieldNameForQuery = cache.policies.getStoreFieldName({ | ||
typename: queryTypename, | ||
fieldName, | ||
field, | ||
variables, | ||
fieldName: fieldName, | ||
field: field, | ||
variables: variables_1, | ||
}); | ||
const renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(typename); | ||
if (renewalPolicy === types_1.RenewalPolicy.AccessAndWrite || | ||
renewalPolicy === types_1.RenewalPolicy.AccessOnly) { | ||
var renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(typename); | ||
if (renewalPolicy === RenewalPolicy.AccessAndWrite || | ||
renewalPolicy === RenewalPolicy.AccessOnly) { | ||
entityTypeMap.renewEntity(dataId, storeFieldNameForEntity); | ||
entityTypeMap.renewEntity(dataId, storeFieldNameForQuery); | ||
} | ||
const evictedByStoreFieldNameForEntity = invalidationPolicyManager.runReadPolicy({ | ||
typename, | ||
dataId, | ||
fieldName, | ||
var evictedByStoreFieldNameForEntity = invalidationPolicyManager.runReadPolicy({ | ||
typename: typename, | ||
dataId: dataId, | ||
fieldName: fieldName, | ||
storeFieldName: storeFieldNameForEntity, | ||
}); | ||
const evictedByStoreFieldNameForQuery = invalidationPolicyManager.runReadPolicy({ | ||
typename, | ||
dataId, | ||
fieldName, | ||
var evictedByStoreFieldNameForQuery = invalidationPolicyManager.runReadPolicy({ | ||
typename: typename, | ||
dataId: dataId, | ||
fieldName: fieldName, | ||
storeFieldName: storeFieldNameForQuery, | ||
@@ -152,3 +152,3 @@ }); | ||
}, true); | ||
utilities_1.maybeDeepFreeze(result); | ||
maybeDeepFreeze(result); | ||
return aggregateResultComplete | ||
@@ -158,18 +158,21 @@ ? ReadResultStatus.Complete | ||
} | ||
utilities_1.maybeDeepFreeze(result); | ||
maybeDeepFreeze(result); | ||
return this.processReadSubResult(result); | ||
} | ||
return ReadResultStatus.Complete; | ||
} | ||
processWriteSubResult(result) { | ||
const { cache, invalidationPolicyManager, entityTypeMap } = this.config; | ||
if (isPlainObject_1.default(result)) { | ||
const { __typename } = result; | ||
Object.keys(result).forEach((resultField) => this.processWriteSubResult(result[resultField])); | ||
}; | ||
CacheResultProcessor.prototype.processWriteSubResult = function (result) { | ||
var _this = this; | ||
var _a = this.config, cache = _a.cache, invalidationPolicyManager = _a.invalidationPolicyManager, entityTypeMap = _a.entityTypeMap; | ||
if (isPlainObject(result)) { | ||
var __typename = result.__typename; | ||
Object.keys(result).forEach(function (resultField) { | ||
return _this.processWriteSubResult(result[resultField]); | ||
}); | ||
if (__typename) { | ||
const id = cache.identify(result); | ||
var id = cache.identify(result); | ||
if (id) { | ||
const renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(__typename); | ||
if (renewalPolicy === types_1.RenewalPolicy.WriteOnly || | ||
renewalPolicy === types_1.RenewalPolicy.AccessAndWrite) { | ||
var renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(__typename); | ||
if (renewalPolicy === RenewalPolicy.WriteOnly || | ||
renewalPolicy === RenewalPolicy.AccessAndWrite) { | ||
entityTypeMap.renewEntity(id); | ||
@@ -179,4 +182,4 @@ } | ||
parent: { | ||
id, | ||
ref: core_1.makeReference(id), | ||
id: id, | ||
ref: makeReference(id), | ||
}, | ||
@@ -187,32 +190,34 @@ }); | ||
} | ||
else if (isArray_1.default(result)) { | ||
result.forEach((resultListItem) => this.processWriteSubResult(resultListItem)); | ||
else if (isArray(result)) { | ||
result.forEach(function (resultListItem) { | ||
return _this.processWriteSubResult(resultListItem); | ||
}); | ||
} | ||
} | ||
processWriteResult(options) { | ||
}; | ||
CacheResultProcessor.prototype.processWriteResult = function (options) { | ||
var _a; | ||
const { dataId, variables, result } = options; | ||
const { entityTypeMap, cache, invalidationPolicyManager } = this.config; | ||
if (isPlainObject_1.default(result)) { | ||
var dataId = options.dataId, variables = options.variables, result = options.result; | ||
var _b = this.config, entityTypeMap = _b.entityTypeMap, cache = _b.cache, invalidationPolicyManager = _b.invalidationPolicyManager; | ||
if (isPlainObject(result)) { | ||
this.processWriteSubResult(result); | ||
} | ||
if (dataId && helpers_1.isQuery(dataId) && isPlainObject_1.default(result)) { | ||
this.getFieldsForQuery(options).forEach((field) => { | ||
if (dataId && isQuery(dataId) && isPlainObject(result)) { | ||
this.getFieldsForQuery(options).forEach(function (field) { | ||
var _a; | ||
const fieldName = field.name.value; | ||
const typename = (_a = entityTypeMap.readEntityById(helpers_1.makeEntityId(dataId, fieldName))) === null || _a === void 0 ? void 0 : _a.typename; | ||
var fieldName = field.name.value; | ||
var typename = (_a = entityTypeMap.readEntityById(makeEntityId(dataId, fieldName))) === null || _a === void 0 ? void 0 : _a.typename; | ||
if (typename) { | ||
const storeFieldName = cache.policies.getStoreFieldName({ | ||
typename, | ||
field, | ||
fieldName, | ||
variables, | ||
var storeFieldName = cache.policies.getStoreFieldName({ | ||
typename: typename, | ||
field: field, | ||
fieldName: fieldName, | ||
variables: variables, | ||
}); | ||
const fieldArgs = utilities_1.argumentsObjectFromField(field, variables); | ||
const fieldVariables = variables !== null && variables !== void 0 ? variables : (fieldArgs !== null ? {} : undefined); | ||
var fieldArgs = argumentsObjectFromField(field, variables); | ||
var fieldVariables = variables !== null && variables !== void 0 ? variables : (fieldArgs !== null ? {} : undefined); | ||
// Write a query to the entity type map at `write` in addition to `merge` time so that we can keep track of its variables. | ||
entityTypeMap.write(typename, dataId, storeFieldName, fieldVariables, fieldArgs); | ||
const renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(typename); | ||
if (renewalPolicy === types_1.RenewalPolicy.WriteOnly || | ||
renewalPolicy === types_1.RenewalPolicy.AccessAndWrite) { | ||
var renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(typename); | ||
if (renewalPolicy === RenewalPolicy.WriteOnly || | ||
renewalPolicy === RenewalPolicy.AccessAndWrite) { | ||
entityTypeMap.renewEntity(dataId, storeFieldName); | ||
@@ -223,5 +228,5 @@ } | ||
id: dataId, | ||
fieldName, | ||
storeFieldName, | ||
ref: core_1.makeReference(dataId), | ||
fieldName: fieldName, | ||
storeFieldName: storeFieldName, | ||
ref: makeReference(dataId), | ||
variables: fieldVariables, | ||
@@ -235,7 +240,7 @@ args: fieldArgs, | ||
else if (dataId) { | ||
const typename = (_a = entityTypeMap.readEntityById(helpers_1.makeEntityId(dataId))) === null || _a === void 0 ? void 0 : _a.typename; | ||
var typename = (_a = entityTypeMap.readEntityById(makeEntityId(dataId))) === null || _a === void 0 ? void 0 : _a.typename; | ||
if (typename) { | ||
const renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(typename); | ||
if (renewalPolicy === types_1.RenewalPolicy.WriteOnly || | ||
renewalPolicy === types_1.RenewalPolicy.AccessAndWrite) { | ||
var renewalPolicy = invalidationPolicyManager.getRenewalPolicyForType(typename); | ||
if (renewalPolicy === RenewalPolicy.WriteOnly || | ||
renewalPolicy === RenewalPolicy.AccessAndWrite) { | ||
entityTypeMap.renewEntity(dataId); | ||
@@ -246,4 +251,4 @@ } | ||
id: dataId, | ||
ref: core_1.makeReference(dataId), | ||
variables, | ||
ref: makeReference(dataId), | ||
variables: variables, | ||
}, | ||
@@ -253,5 +258,6 @@ }); | ||
} | ||
} | ||
} | ||
exports.CacheResultProcessor = CacheResultProcessor; | ||
}; | ||
return CacheResultProcessor; | ||
}()); | ||
export { CacheResultProcessor }; | ||
//# sourceMappingURL=CacheResultProcessor.js.map |
@@ -1,9 +0,2 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InvalidationPolicyCache = void 0; | ||
var InvalidationPolicyCache_1 = require("./InvalidationPolicyCache"); | ||
Object.defineProperty(exports, "InvalidationPolicyCache", { enumerable: true, get: function () { return __importDefault(InvalidationPolicyCache_1).default; } }); | ||
export { default as InvalidationPolicyCache } from "./InvalidationPolicyCache"; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,31 @@ | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { | ||
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } | ||
return cooked; | ||
}; | ||
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 __rest = (this && this.__rest) || function (s, e) { | ||
@@ -13,17 +42,18 @@ var t = {}; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from) { | ||
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) | ||
to[j] = from[i]; | ||
return to; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const core_1 = require("@apollo/client/core"); | ||
const compact_1 = __importDefault(require("lodash/compact")); | ||
const every_1 = __importDefault(require("lodash/every")); | ||
const pick_1 = __importDefault(require("lodash/pick")); | ||
const isFunction_1 = __importDefault(require("lodash/isFunction")); | ||
const InvalidationPolicyManager_1 = __importDefault(require("../policies/InvalidationPolicyManager")); | ||
const entity_store_1 = require("../entity-store"); | ||
const helpers_1 = require("../helpers"); | ||
const CacheResultProcessor_1 = require("./CacheResultProcessor"); | ||
const types_1 = require("../policies/types"); | ||
const utils_1 = require("./utils"); | ||
import { gql, InMemoryCache, makeReference, } from "@apollo/client/core"; | ||
import compact from "lodash/compact"; | ||
import every from "lodash/every"; | ||
import pick from "lodash/pick"; | ||
import isFunction from "lodash/isFunction"; | ||
import InvalidationPolicyManager from "../policies/InvalidationPolicyManager"; | ||
import { EntityStoreWatcher, EntityTypeMap } from "../entity-store"; | ||
import { makeEntityId, isQuery, maybeDeepClone, fieldNameFromStoreName } from "../helpers"; | ||
import { CacheResultProcessor, ReadResultStatus } from "./CacheResultProcessor"; | ||
import { InvalidationPolicyEvent } from "../policies/types"; | ||
import { cacheExtensionsCollectionTypename, collectionEntityIdForType } from './utils'; | ||
/** | ||
@@ -33,29 +63,51 @@ * Extension of Apollo in-memory cache which adds support for cache policies. | ||
// @ts-ignore: Private API overloads | ||
class InvalidationPolicyCache extends core_1.InMemoryCache { | ||
constructor(config = {}) { | ||
const { invalidationPolicies = {}, enableCollections = false } = config, inMemoryCacheConfig = __rest(config, ["invalidationPolicies", "enableCollections"]); | ||
super(inMemoryCacheConfig); | ||
this.enableCollections = enableCollections; | ||
this.isBroadcasting = false; | ||
const { entityTypeMap } = this; | ||
this.invalidationPolicyManager = new InvalidationPolicyManager_1.default({ | ||
var InvalidationPolicyCache = /** @class */ (function (_super) { | ||
__extends(InvalidationPolicyCache, _super); | ||
function InvalidationPolicyCache(config) { | ||
if (config === void 0) { config = {}; } | ||
var _this = this; | ||
var _a = config.invalidationPolicies, invalidationPolicies = _a === void 0 ? {} : _a, _b = config.enableCollections, enableCollections = _b === void 0 ? false : _b, inMemoryCacheConfig = __rest(config, ["invalidationPolicies", "enableCollections"]); | ||
_this = _super.call(this, inMemoryCacheConfig) || this; | ||
_this.enableCollections = enableCollections; | ||
_this.isBroadcasting = false; | ||
var entityTypeMap = _this.entityTypeMap; | ||
_this.invalidationPolicyManager = new InvalidationPolicyManager({ | ||
policies: invalidationPolicies, | ||
entityTypeMap: entityTypeMap, | ||
cacheOperations: { | ||
evict: (...args) => this.evict(...args), | ||
modify: (...args) => this.modify(...args), | ||
readField: (...args) => this.readField(...args), | ||
evict: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return _this.evict.apply(_this, args); | ||
}, | ||
modify: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return _this.modify.apply(_this, args); | ||
}, | ||
readField: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return _this.readField.apply(_this, args); | ||
}, | ||
}, | ||
}); | ||
this.cacheResultProcessor = new CacheResultProcessor_1.CacheResultProcessor({ | ||
invalidationPolicyManager: this.invalidationPolicyManager, | ||
_this.cacheResultProcessor = new CacheResultProcessor({ | ||
invalidationPolicyManager: _this.invalidationPolicyManager, | ||
// @ts-ignore This field is assigned in the parent constructor | ||
entityTypeMap: this.entityTypeMap, | ||
cache: this, | ||
entityTypeMap: _this.entityTypeMap, | ||
cache: _this, | ||
}); | ||
return _this; | ||
} | ||
// @ts-ignore private API | ||
init() { | ||
InvalidationPolicyCache.prototype.init = function () { | ||
// @ts-ignore private API | ||
super.init(); | ||
_super.prototype.init.call(this); | ||
// After init is called, the entity store has been reset so we must also reset | ||
@@ -66,4 +118,4 @@ // the cache policies library's corresponding entity type map, watcher and | ||
this.entityStoreRoot = this.data; | ||
this.entityTypeMap = new entity_store_1.EntityTypeMap(); | ||
this.entityStoreWatcher = new entity_store_1.EntityStoreWatcher({ | ||
this.entityTypeMap = new EntityTypeMap(); | ||
this.entityStoreWatcher = new EntityStoreWatcher({ | ||
entityStore: this.entityStoreRoot, | ||
@@ -74,3 +126,3 @@ entityTypeMap: this.entityTypeMap, | ||
}); | ||
this.cacheResultProcessor = new CacheResultProcessor_1.CacheResultProcessor({ | ||
this.cacheResultProcessor = new CacheResultProcessor({ | ||
invalidationPolicyManager: this.invalidationPolicyManager, | ||
@@ -81,11 +133,11 @@ // @ts-ignore This field is assigned in the parent constructor | ||
}); | ||
} | ||
readField(fieldNameOrOptions, from) { | ||
}; | ||
InvalidationPolicyCache.prototype.readField = function (fieldNameOrOptions, from) { | ||
if (!fieldNameOrOptions) { | ||
return; | ||
} | ||
const options = typeof fieldNameOrOptions === "string" | ||
var options = typeof fieldNameOrOptions === "string" | ||
? { | ||
fieldName: fieldNameOrOptions, | ||
from, | ||
from: from, | ||
} | ||
@@ -99,36 +151,37 @@ : fieldNameOrOptions; | ||
}); | ||
} | ||
broadcastWatches() { | ||
}; | ||
InvalidationPolicyCache.prototype.broadcastWatches = function () { | ||
this.isBroadcasting = true; | ||
super.broadcastWatches(); | ||
_super.prototype.broadcastWatches.call(this); | ||
this.isBroadcasting = false; | ||
} | ||
}; | ||
// Determines whether the cache's data reference is set to the root store. If not, then there is an ongoing optimistic transaction | ||
// being applied to a new layer. | ||
isOperatingOnRootData() { | ||
InvalidationPolicyCache.prototype.isOperatingOnRootData = function () { | ||
// @ts-ignore | ||
return this.data === this.entityStoreRoot; | ||
} | ||
modify(options) { | ||
}; | ||
InvalidationPolicyCache.prototype.modify = function (options) { | ||
var _this = this; | ||
var _a; | ||
const modifyResult = super.modify(options); | ||
if (!this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Write) || | ||
var modifyResult = _super.prototype.modify.call(this, options); | ||
if (!this.invalidationPolicyManager.isPolicyEventActive(InvalidationPolicyEvent.Write) || | ||
!modifyResult) { | ||
return modifyResult; | ||
} | ||
const { id = "ROOT_QUERY", fields } = options; | ||
if (helpers_1.isQuery(id)) { | ||
Object.keys(fields).forEach((storeFieldName) => { | ||
var _b = options.id, id = _b === void 0 ? "ROOT_QUERY" : _b, fields = options.fields; | ||
if (isQuery(id)) { | ||
Object.keys(fields).forEach(function (storeFieldName) { | ||
var _a; | ||
const fieldName = helpers_1.fieldNameFromStoreName(storeFieldName); | ||
const typename = (_a = this.entityTypeMap.readEntityById(helpers_1.makeEntityId(id, fieldName))) === null || _a === void 0 ? void 0 : _a.typename; | ||
var fieldName = fieldNameFromStoreName(storeFieldName); | ||
var typename = (_a = _this.entityTypeMap.readEntityById(makeEntityId(id, fieldName))) === null || _a === void 0 ? void 0 : _a.typename; | ||
if (!typename) { | ||
return; | ||
} | ||
this.invalidationPolicyManager.runWritePolicy(typename, { | ||
_this.invalidationPolicyManager.runWritePolicy(typename, { | ||
parent: { | ||
id, | ||
fieldName, | ||
storeFieldName, | ||
ref: core_1.makeReference(id), | ||
id: id, | ||
fieldName: fieldName, | ||
storeFieldName: storeFieldName, | ||
ref: makeReference(id), | ||
}, | ||
@@ -139,3 +192,3 @@ }); | ||
else { | ||
const typename = (_a = this.entityTypeMap.readEntityById(id)) === null || _a === void 0 ? void 0 : _a.typename; | ||
var typename = (_a = this.entityTypeMap.readEntityById(id)) === null || _a === void 0 ? void 0 : _a.typename; | ||
if (!typename) { | ||
@@ -146,4 +199,4 @@ return modifyResult; | ||
parent: { | ||
id, | ||
ref: core_1.makeReference(id), | ||
id: id, | ||
ref: makeReference(id), | ||
}, | ||
@@ -156,9 +209,9 @@ }); | ||
return modifyResult; | ||
} | ||
write(options) { | ||
const writeResult = super.write(options); | ||
}; | ||
InvalidationPolicyCache.prototype.write = function (options) { | ||
var writeResult = _super.prototype.write.call(this, options); | ||
// Do not trigger a write policy if the current write is being applied to an optimistic data layer since | ||
// the policy will later be applied when the server data response is received. | ||
if ((!this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Write) && | ||
!this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Read)) || | ||
if ((!this.invalidationPolicyManager.isPolicyEventActive(InvalidationPolicyEvent.Write) && | ||
!this.invalidationPolicyManager.isPolicyEventActive(InvalidationPolicyEvent.Read)) || | ||
!this.isOperatingOnRootData()) { | ||
@@ -172,19 +225,20 @@ return writeResult; | ||
return writeResult; | ||
} | ||
}; | ||
// Evicts all entities of the given type matching the filter criteria. Returns a list of evicted entities | ||
// by reference. | ||
evictWhere(filters) { | ||
const { __typename, filter } = filters; | ||
const references = this.readReferenceWhere({ | ||
__typename, | ||
filter, | ||
InvalidationPolicyCache.prototype.evictWhere = function (filters) { | ||
var _this = this; | ||
var __typename = filters.__typename, filter = filters.filter; | ||
var references = this.readReferenceWhere({ | ||
__typename: __typename, | ||
filter: filter, | ||
}); | ||
references.forEach((ref) => this.evict({ id: ref.__ref, broadcast: false })); | ||
references.forEach(function (ref) { return _this.evict({ id: ref.__ref, broadcast: false }); }); | ||
this.broadcastWatches(); | ||
return references; | ||
} | ||
evict(options) { | ||
}; | ||
InvalidationPolicyCache.prototype.evict = function (options) { | ||
var _a; | ||
const { fieldName, args } = options; | ||
let { id } = options; | ||
var fieldName = options.fieldName, args = options.args; | ||
var id = options.id; | ||
if (!id) { | ||
@@ -196,10 +250,10 @@ if (Object.prototype.hasOwnProperty.call(options, "id")) { | ||
} | ||
if (this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Evict)) { | ||
const { typename } = (_a = this.entityTypeMap.readEntityById(helpers_1.makeEntityId(id, fieldName))) !== null && _a !== void 0 ? _a : {}; | ||
if (this.invalidationPolicyManager.isPolicyEventActive(InvalidationPolicyEvent.Evict)) { | ||
var typename = ((_a = this.entityTypeMap.readEntityById(makeEntityId(id, fieldName))) !== null && _a !== void 0 ? _a : {}).typename; | ||
if (typename) { | ||
const storeFieldName = helpers_1.isQuery(id) && fieldName | ||
var storeFieldName = isQuery(id) && fieldName | ||
? this.policies.getStoreFieldName({ | ||
typename, | ||
fieldName, | ||
args, | ||
typename: typename, | ||
fieldName: fieldName, | ||
args: args, | ||
}) | ||
@@ -209,7 +263,7 @@ : undefined; | ||
parent: { | ||
id, | ||
fieldName, | ||
storeFieldName, | ||
id: id, | ||
fieldName: fieldName, | ||
storeFieldName: storeFieldName, | ||
variables: args, | ||
ref: core_1.makeReference(id), | ||
ref: makeReference(id), | ||
}, | ||
@@ -219,5 +273,5 @@ }); | ||
} | ||
return super.evict(options); | ||
} | ||
updateCollectionField(typename, dataId) { | ||
return _super.prototype.evict.call(this, options); | ||
}; | ||
InvalidationPolicyCache.prototype.updateCollectionField = function (typename, dataId) { | ||
// Since colletion support is still experimental, only record entities in collections if enabled | ||
@@ -227,4 +281,4 @@ if (!this.enableCollections) { | ||
} | ||
const collectionEntityId = utils_1.collectionEntityIdForType(typename); | ||
const collectionFieldExists = !!this.readField('id', core_1.makeReference(collectionEntityId)); | ||
var collectionEntityId = collectionEntityIdForType(typename); | ||
var collectionFieldExists = !!this.readField('id', makeReference(collectionEntityId)); | ||
// If the collection field for the type does not exist in the cache, then initialize it as | ||
@@ -235,10 +289,5 @@ // an empty array. | ||
id: collectionEntityId, | ||
fragment: core_1.gql ` | ||
fragment InitializeCollectionEntity on CacheExtensionsCollectionEntity { | ||
data | ||
id | ||
} | ||
`, | ||
fragment: gql(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n fragment InitializeCollectionEntity on CacheExtensionsCollectionEntity {\n data\n id\n }\n "], ["\n fragment InitializeCollectionEntity on CacheExtensionsCollectionEntity {\n data\n id\n }\n "]))), | ||
data: { | ||
__typename: utils_1.cacheExtensionsCollectionTypename, | ||
__typename: cacheExtensionsCollectionTypename, | ||
id: typename, | ||
@@ -255,7 +304,7 @@ data: [], | ||
fields: { | ||
data: (existing, { canRead }) => { | ||
return [ | ||
...existing.filter((ref) => canRead(ref)), | ||
core_1.makeReference(dataId), | ||
]; | ||
data: function (existing, _a) { | ||
var canRead = _a.canRead; | ||
return __spreadArray(__spreadArray([], existing.filter(function (ref) { return canRead(ref); })), [ | ||
makeReference(dataId), | ||
]); | ||
} | ||
@@ -265,23 +314,25 @@ } | ||
} | ||
} | ||
}; | ||
// Returns all expired entities whose cache time exceeds their type's timeToLive or as a fallback | ||
// the global timeToLive if specified. Evicts the expired entities by default, with an option to only report | ||
// them. | ||
_expire(reportOnly = false) { | ||
const { entitiesById } = this.entityTypeMap.extract(); | ||
const expiredEntityIds = []; | ||
Object.keys(entitiesById).forEach((entityId) => { | ||
const entity = entitiesById[entityId]; | ||
const { storeFieldNames, dataId, fieldName, typename } = entity; | ||
if (helpers_1.isQuery(dataId) && storeFieldNames) { | ||
Object.keys(storeFieldNames.entries).forEach((storeFieldName) => { | ||
const isExpired = this.invalidationPolicyManager.runReadPolicy({ | ||
typename, | ||
dataId, | ||
fieldName, | ||
storeFieldName, | ||
reportOnly, | ||
InvalidationPolicyCache.prototype._expire = function (reportOnly) { | ||
var _this = this; | ||
if (reportOnly === void 0) { reportOnly = false; } | ||
var entitiesById = this.entityTypeMap.extract().entitiesById; | ||
var expiredEntityIds = []; | ||
Object.keys(entitiesById).forEach(function (entityId) { | ||
var entity = entitiesById[entityId]; | ||
var storeFieldNames = entity.storeFieldNames, dataId = entity.dataId, fieldName = entity.fieldName, typename = entity.typename; | ||
if (isQuery(dataId) && storeFieldNames) { | ||
Object.keys(storeFieldNames.entries).forEach(function (storeFieldName) { | ||
var isExpired = _this.invalidationPolicyManager.runReadPolicy({ | ||
typename: typename, | ||
dataId: dataId, | ||
fieldName: fieldName, | ||
storeFieldName: storeFieldName, | ||
reportOnly: reportOnly, | ||
}); | ||
if (isExpired) { | ||
expiredEntityIds.push(helpers_1.makeEntityId(dataId, storeFieldName)); | ||
expiredEntityIds.push(makeEntityId(dataId, storeFieldName)); | ||
} | ||
@@ -291,10 +342,10 @@ }); | ||
else { | ||
const isExpired = this.invalidationPolicyManager.runReadPolicy({ | ||
typename, | ||
dataId, | ||
fieldName, | ||
reportOnly, | ||
var isExpired = _this.invalidationPolicyManager.runReadPolicy({ | ||
typename: typename, | ||
dataId: dataId, | ||
fieldName: fieldName, | ||
reportOnly: reportOnly, | ||
}); | ||
if (isExpired) { | ||
expiredEntityIds.push(helpers_1.makeEntityId(dataId)); | ||
expiredEntityIds.push(makeEntityId(dataId)); | ||
} | ||
@@ -307,55 +358,66 @@ } | ||
return expiredEntityIds; | ||
} | ||
}; | ||
// Expires all entities still present in the cache that have exceeded their timeToLive. By default entities are evicted | ||
// lazily on read if their entity is expired. Use this expire API to eagerly remove expired entities. | ||
expire() { | ||
InvalidationPolicyCache.prototype.expire = function () { | ||
return this._expire(false); | ||
} | ||
}; | ||
// Returns all expired entities still present in the cache. | ||
expiredEntities() { | ||
InvalidationPolicyCache.prototype.expiredEntities = function () { | ||
return this._expire(true); | ||
} | ||
}; | ||
// Activates the provided policy events (on read, on write, on evict) or by default all policy events. | ||
activatePolicyEvents(...policyEvents) { | ||
InvalidationPolicyCache.prototype.activatePolicyEvents = function () { | ||
var _a; | ||
var policyEvents = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
policyEvents[_i] = arguments[_i]; | ||
} | ||
if (policyEvents.length > 0) { | ||
this.invalidationPolicyManager.activatePolicies(...policyEvents); | ||
(_a = this.invalidationPolicyManager).activatePolicies.apply(_a, policyEvents); | ||
} | ||
else { | ||
this.invalidationPolicyManager.activatePolicies(types_1.InvalidationPolicyEvent.Read, types_1.InvalidationPolicyEvent.Write, types_1.InvalidationPolicyEvent.Evict); | ||
this.invalidationPolicyManager.activatePolicies(InvalidationPolicyEvent.Read, InvalidationPolicyEvent.Write, InvalidationPolicyEvent.Evict); | ||
} | ||
} | ||
}; | ||
// Deactivates the provided policy events (on read, on write, on evict) or by default all policy events. | ||
deactivatePolicyEvents(...policyEvents) { | ||
InvalidationPolicyCache.prototype.deactivatePolicyEvents = function () { | ||
var _a; | ||
var policyEvents = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
policyEvents[_i] = arguments[_i]; | ||
} | ||
if (policyEvents.length > 0) { | ||
this.invalidationPolicyManager.deactivatePolicies(...policyEvents); | ||
(_a = this.invalidationPolicyManager).deactivatePolicies.apply(_a, policyEvents); | ||
} | ||
else { | ||
this.invalidationPolicyManager.deactivatePolicies(types_1.InvalidationPolicyEvent.Read, types_1.InvalidationPolicyEvent.Write, types_1.InvalidationPolicyEvent.Evict); | ||
this.invalidationPolicyManager.deactivatePolicies(InvalidationPolicyEvent.Read, InvalidationPolicyEvent.Write, InvalidationPolicyEvent.Evict); | ||
} | ||
} | ||
}; | ||
// Returns the policy events that are currently active. | ||
activePolicyEvents() { | ||
InvalidationPolicyCache.prototype.activePolicyEvents = function () { | ||
var _this = this; | ||
return [ | ||
types_1.InvalidationPolicyEvent.Read, | ||
types_1.InvalidationPolicyEvent.Write, | ||
types_1.InvalidationPolicyEvent.Evict | ||
].filter(policyEvent => this.invalidationPolicyManager.isPolicyEventActive(policyEvent)); | ||
} | ||
read(options) { | ||
const result = super.read(options); | ||
if (!this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Read)) { | ||
InvalidationPolicyEvent.Read, | ||
InvalidationPolicyEvent.Write, | ||
InvalidationPolicyEvent.Evict | ||
].filter(function (policyEvent) { return _this.invalidationPolicyManager.isPolicyEventActive(policyEvent); }); | ||
}; | ||
InvalidationPolicyCache.prototype.read = function (options) { | ||
var result = _super.prototype.read.call(this, options); | ||
if (!this.invalidationPolicyManager.isPolicyEventActive(InvalidationPolicyEvent.Read)) { | ||
return result; | ||
} | ||
const processedResult = helpers_1.maybeDeepClone(result); | ||
const processedResultStatus = this.cacheResultProcessor.processReadResult(processedResult, options); | ||
if (processedResultStatus === CacheResultProcessor_1.ReadResultStatus.Complete) { | ||
var processedResult = maybeDeepClone(result); | ||
var processedResultStatus = this.cacheResultProcessor.processReadResult(processedResult, options); | ||
if (processedResultStatus === ReadResultStatus.Complete) { | ||
return result; | ||
} | ||
this.broadcastWatches(); | ||
return processedResultStatus === CacheResultProcessor_1.ReadResultStatus.Evicted | ||
return processedResultStatus === ReadResultStatus.Evicted | ||
? null | ||
: processedResult; | ||
} | ||
diff(options) { | ||
const cacheDiff = super.diff(options); | ||
}; | ||
InvalidationPolicyCache.prototype.diff = function (options) { | ||
var cacheDiff = _super.prototype.diff.call(this, options); | ||
// Diff calls made by `broadcastWatches` should not trigger the read policy | ||
@@ -365,10 +427,10 @@ // as these are internal reads not reflective of client action and can lead to recursive recomputation of cached data which is an error. | ||
// the scope of broadcasts. | ||
if (!this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Read) || | ||
if (!this.invalidationPolicyManager.isPolicyEventActive(InvalidationPolicyEvent.Read) || | ||
this.isBroadcasting) { | ||
return cacheDiff; | ||
} | ||
const { result } = cacheDiff; | ||
const processedResult = helpers_1.maybeDeepClone(result); | ||
const processedResultStatus = this.cacheResultProcessor.processReadResult(processedResult, options); | ||
if (processedResultStatus === CacheResultProcessor_1.ReadResultStatus.Complete) { | ||
var result = cacheDiff.result; | ||
var processedResult = maybeDeepClone(result); | ||
var processedResultStatus = this.cacheResultProcessor.processReadResult(processedResult, options); | ||
if (processedResultStatus === ReadResultStatus.Complete) { | ||
return cacheDiff; | ||
@@ -379,35 +441,39 @@ } | ||
cacheDiff.result = | ||
processedResultStatus === CacheResultProcessor_1.ReadResultStatus.Evicted | ||
processedResultStatus === ReadResultStatus.Evicted | ||
? undefined | ||
: processedResult; | ||
return cacheDiff; | ||
} | ||
extract(optimistic = false, withInvalidation = true) { | ||
const extractedCache = super.extract(optimistic); | ||
}; | ||
InvalidationPolicyCache.prototype.extract = function (optimistic, withInvalidation) { | ||
if (optimistic === void 0) { optimistic = false; } | ||
if (withInvalidation === void 0) { withInvalidation = true; } | ||
var extractedCache = _super.prototype.extract.call(this, optimistic); | ||
if (withInvalidation) { | ||
// The entitiesById are sufficient alone for reconstructing the type map, so to | ||
// minimize payload size only inject the entitiesById object into the extracted cache | ||
extractedCache.invalidation = pick_1.default(this.entityTypeMap.extract(), "entitiesById"); | ||
extractedCache.invalidation = pick(this.entityTypeMap.extract(), "entitiesById"); | ||
} | ||
return extractedCache; | ||
} | ||
}; | ||
// Supports reading a collection of entities by type from the cache and filtering them by the given fields. Returns | ||
// a list of the dereferenced matching entities from the cache based on the given fragment. | ||
readFragmentWhere(options) { | ||
const { fragment, filter } = options, restOptions = __rest(options, ["fragment", "filter"]); | ||
const fragmentDefinition = fragment.definitions[0]; | ||
const __typename = fragmentDefinition.typeCondition.name.value; | ||
const matchingRefs = this.readReferenceWhere({ | ||
__typename, | ||
filter | ||
InvalidationPolicyCache.prototype.readFragmentWhere = function (options) { | ||
var _this = this; | ||
var fragment = options.fragment, filter = options.filter, restOptions = __rest(options, ["fragment", "filter"]); | ||
var fragmentDefinition = fragment.definitions[0]; | ||
var __typename = fragmentDefinition.typeCondition.name.value; | ||
var matchingRefs = this.readReferenceWhere({ | ||
__typename: __typename, | ||
filter: filter | ||
}); | ||
const matchingFragments = matchingRefs.map(ref => this.readFragment(Object.assign(Object.assign({}, restOptions), { fragment, id: ref.__ref }))); | ||
return compact_1.default(matchingFragments); | ||
} | ||
var matchingFragments = matchingRefs.map(function (ref) { return _this.readFragment(__assign(__assign({}, restOptions), { fragment: fragment, id: ref.__ref })); }); | ||
return compact(matchingFragments); | ||
}; | ||
// Supports reading a collection of references by type from the cache and filtering them by the given fields. Returns a | ||
// list of the matching references. | ||
readReferenceWhere(options) { | ||
const { __typename, filter } = options; | ||
const collectionEntityName = utils_1.collectionEntityIdForType(__typename); | ||
const entityReferences = this.readField('data', core_1.makeReference(collectionEntityName)); | ||
InvalidationPolicyCache.prototype.readReferenceWhere = function (options) { | ||
var _this = this; | ||
var __typename = options.__typename, filter = options.filter; | ||
var collectionEntityName = collectionEntityIdForType(__typename); | ||
var entityReferences = this.readField('data', makeReference(collectionEntityName)); | ||
if (!entityReferences) { | ||
@@ -419,17 +485,19 @@ return []; | ||
} | ||
return entityReferences.filter(ref => { | ||
if (isFunction_1.default(filter)) { | ||
return filter(ref, this.readField.bind(this)); | ||
return entityReferences.filter(function (ref) { | ||
if (isFunction(filter)) { | ||
return filter(ref, _this.readField.bind(_this)); | ||
} | ||
const entityFilterResults = Object.keys(filter).map(filterField => { | ||
var entityFilterResults = Object.keys(filter).map(function (filterField) { | ||
// @ts-ignore | ||
const filterValue = filter[filterField]; | ||
const entityValueForFilter = this.readField(filterField, ref); | ||
var filterValue = filter[filterField]; | ||
var entityValueForFilter = _this.readField(filterField, ref); | ||
return filterValue === entityValueForFilter; | ||
}); | ||
return every_1.default(entityFilterResults, Boolean); | ||
return every(entityFilterResults, Boolean); | ||
}); | ||
} | ||
} | ||
exports.default = InvalidationPolicyCache; | ||
}; | ||
return InvalidationPolicyCache; | ||
}(InMemoryCache)); | ||
export default InvalidationPolicyCache; | ||
var templateObject_1; | ||
//# sourceMappingURL=InvalidationPolicyCache.js.map |
@@ -1,3 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
export {}; | ||
//# sourceMappingURL=types.js.map |
@@ -1,9 +0,5 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.collectionEntityIdForType = exports.cacheExtensionsCollectionTypename = void 0; | ||
exports.cacheExtensionsCollectionTypename = 'CacheExtensionsCollectionEntity'; | ||
function collectionEntityIdForType(typename) { | ||
return `${exports.cacheExtensionsCollectionTypename}:${typename}`; | ||
export var cacheExtensionsCollectionTypename = 'CacheExtensionsCollectionEntity'; | ||
export function collectionEntityIdForType(typename) { | ||
return cacheExtensionsCollectionTypename + ":" + typename; | ||
} | ||
exports.collectionEntityIdForType = collectionEntityIdForType; | ||
//# sourceMappingURL=utils.js.map |
@@ -1,36 +0,67 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const core_1 = require("@apollo/client/core"); | ||
const utils_1 = require("./utils"); | ||
const helpers_1 = require("../helpers"); | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
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); | ||
}; | ||
import { ApolloClient, } from '@apollo/client/core'; | ||
import { buildWatchFragmentQuery, buildWatchFragmentWhereQuery } from './utils'; | ||
import { generateFragmentFieldName } from '../helpers'; | ||
// An extension of the Apollo client that add support for watching updates to entities | ||
// and collections of entities based on the provided filters. | ||
class ApolloExtensionsClient extends core_1.ApolloClient { | ||
constructor(config) { | ||
super(config); | ||
var ApolloExtensionsClient = /** @class */ (function (_super) { | ||
__extends(ApolloExtensionsClient, _super); | ||
function ApolloExtensionsClient(config) { | ||
var _this = _super.call(this, config) || this; | ||
// @ts-ignore | ||
this.policies = this.cache.policies; | ||
_this.policies = _this.cache.policies; | ||
return _this; | ||
} | ||
// Watches the data in the cache similarly to watchQuery and additionally extracts the given fieldName from the watch query | ||
// subscription and returns a subscription that emits that field data. | ||
watchQueryForField(query, fieldName) { | ||
const obsQuery = this.watchQuery({ | ||
ApolloExtensionsClient.prototype.watchQueryForField = function (query, fieldName) { | ||
var _this = this; | ||
var obsQuery = this.watchQuery({ | ||
fetchPolicy: 'cache-only', | ||
query: query, | ||
}); | ||
const subscribe = obsQuery.subscribe.bind(obsQuery); | ||
obsQuery.subscribe = (observer) => { | ||
var subscribe = obsQuery.subscribe.bind(obsQuery); | ||
obsQuery.subscribe = function (observer) { | ||
var rest = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
rest[_i - 1] = arguments[_i]; | ||
} | ||
// This check is modeled after the Zen Observable observer check: | ||
// https://github.com/zenparsing/zen-observable/blob/master/src/Observable.js#L211 | ||
if (typeof observer != 'object') { | ||
if (typeof observer !== 'object') { | ||
observer = { | ||
next: observer, | ||
error: arguments[1], | ||
complete: arguments[2], | ||
error: rest[0], | ||
complete: rest[1], | ||
}; | ||
} | ||
const observerNext = observer.next; | ||
var observerNext = observer.next; | ||
// The observer maps the value emitted from the observable to the data at the | ||
// given field name. | ||
observer.next = (value) => { | ||
observer.next = function (value) { | ||
var _a; | ||
@@ -41,8 +72,8 @@ if (observerNext) { | ||
}; | ||
const subscription = subscribe(observer); | ||
const unsubscribe = subscription.unsubscribe.bind(subscription); | ||
subscription.unsubscribe = () => { | ||
var subscription = subscribe(observer); | ||
var unsubscribe = subscription.unsubscribe.bind(subscription); | ||
subscription.unsubscribe = function () { | ||
// @ts-ignore typePolicies is private. Delete the field name from the type policies | ||
// after the subscription has been cleaned up. | ||
delete this.cache.policies.typePolicies.Query.fields[fieldName]; | ||
delete _this.cache.policies.typePolicies.Query.fields[fieldName]; | ||
unsubscribe(); | ||
@@ -53,18 +84,19 @@ }; | ||
return obsQuery; | ||
} | ||
}; | ||
// Watches the data in the cache similarly to watchQuery for a given fragment. | ||
watchFragment(options) { | ||
const fieldName = helpers_1.generateFragmentFieldName(); | ||
const query = utils_1.buildWatchFragmentQuery(Object.assign(Object.assign({}, options), { fieldName, policies: this.policies })); | ||
ApolloExtensionsClient.prototype.watchFragment = function (options) { | ||
var fieldName = generateFragmentFieldName(); | ||
var query = buildWatchFragmentQuery(__assign(__assign({}, options), { fieldName: fieldName, policies: this.policies })); | ||
return this.watchQueryForField(query, fieldName); | ||
} | ||
}; | ||
// Watches the data in the cache similarly to watchQuery for all entities int he cache | ||
// matching the given filter. | ||
watchFragmentWhere(options) { | ||
const fieldName = helpers_1.generateFragmentFieldName(); | ||
const query = utils_1.buildWatchFragmentWhereQuery(Object.assign(Object.assign({}, options), { fieldName, cache: this.cache, policies: this.policies })); | ||
ApolloExtensionsClient.prototype.watchFragmentWhere = function (options) { | ||
var fieldName = generateFragmentFieldName(); | ||
var query = buildWatchFragmentWhereQuery(__assign(__assign({}, options), { fieldName: fieldName, cache: this.cache, policies: this.policies })); | ||
return this.watchQueryForField(query, fieldName); | ||
} | ||
} | ||
exports.default = ApolloExtensionsClient; | ||
}; | ||
return ApolloExtensionsClient; | ||
}(ApolloClient)); | ||
export default ApolloExtensionsClient; | ||
//# sourceMappingURL=ApolloExtensionsClient.js.map |
@@ -1,9 +0,2 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ApolloExtensionsClient = void 0; | ||
var ApolloExtensionsClient_1 = require("./ApolloExtensionsClient"); | ||
Object.defineProperty(exports, "ApolloExtensionsClient", { enumerable: true, get: function () { return __importDefault(ApolloExtensionsClient_1).default; } }); | ||
export { default as ApolloExtensionsClient } from './ApolloExtensionsClient'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
export {}; | ||
//# sourceMappingURL=types.js.map |
@@ -1,6 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.buildWatchFragmentWhereQuery = exports.buildWatchFragmentQuery = void 0; | ||
const core_1 = require("@apollo/client/core"); | ||
function _generateQueryFromFragment({ fieldName, fragmentDefinition, }) { | ||
import { makeReference } from '@apollo/client/core'; | ||
function _generateQueryFromFragment(_a) { | ||
var fieldName = _a.fieldName, fragmentDefinition = _a.fragmentDefinition; | ||
return { | ||
@@ -38,8 +36,9 @@ kind: 'Document', | ||
// and dynamically adding a type policy that returns the entity. | ||
function buildWatchFragmentQuery(options) { | ||
const { fragment, id, policies, fieldName } = options; | ||
const fragmentDefinition = fragment.definitions[0]; | ||
const query = _generateQueryFromFragment({ | ||
export function buildWatchFragmentQuery(options) { | ||
var _a; | ||
var fragment = options.fragment, id = options.id, policies = options.policies, fieldName = options.fieldName; | ||
var fragmentDefinition = fragment.definitions[0]; | ||
var query = _generateQueryFromFragment({ | ||
fragmentDefinition: fragmentDefinition, | ||
fieldName, | ||
fieldName: fieldName, | ||
}); | ||
@@ -52,9 +51,9 @@ // @ts-ignore The getFieldPolicy is private but we need it here to determine | ||
Query: { | ||
fields: { | ||
[fieldName]: { | ||
read(_existing) { | ||
return core_1.makeReference(id); | ||
fields: (_a = {}, | ||
_a[fieldName] = { | ||
read: function (_existing) { | ||
return makeReference(id); | ||
} | ||
} | ||
} | ||
}, | ||
_a) | ||
} | ||
@@ -65,12 +64,12 @@ }); | ||
} | ||
exports.buildWatchFragmentQuery = buildWatchFragmentQuery; | ||
// Returns a query that can be used to watch a filtered list of normalized cache entities by converting the fragment to a query | ||
// and dynamically adding a type policy that returns the list of matching entities. | ||
function buildWatchFragmentWhereQuery(options) { | ||
const { fragment, filter, policies, cache, fieldName, } = options; | ||
const fragmentDefinition = fragment.definitions[0]; | ||
const __typename = fragmentDefinition.typeCondition.name.value; | ||
const query = _generateQueryFromFragment({ | ||
fragmentDefinition, | ||
fieldName, | ||
export function buildWatchFragmentWhereQuery(options) { | ||
var _a; | ||
var fragment = options.fragment, filter = options.filter, policies = options.policies, cache = options.cache, fieldName = options.fieldName; | ||
var fragmentDefinition = fragment.definitions[0]; | ||
var __typename = fragmentDefinition.typeCondition.name.value; | ||
var query = _generateQueryFromFragment({ | ||
fragmentDefinition: fragmentDefinition, | ||
fieldName: fieldName, | ||
}); | ||
@@ -83,12 +82,12 @@ // @ts-ignore The getFieldPolicy is private but we need it here to determine | ||
Query: { | ||
fields: { | ||
[fieldName]: { | ||
read(_existing) { | ||
fields: (_a = {}, | ||
_a[fieldName] = { | ||
read: function (_existing) { | ||
return cache.readReferenceWhere({ | ||
__typename, | ||
filter, | ||
__typename: __typename, | ||
filter: filter, | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
_a) | ||
} | ||
@@ -99,3 +98,2 @@ }); | ||
} | ||
exports.buildWatchFragmentWhereQuery = buildWatchFragmentWhereQuery; | ||
//# sourceMappingURL=utils.js.map |
@@ -1,20 +0,19 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const helpers_1 = require("../helpers"); | ||
const utils_1 = require("../cache/utils"); | ||
import { makeEntityId, isQuery } from "../helpers"; | ||
import { cacheExtensionsCollectionTypename } from '../cache/utils'; | ||
/** | ||
* Watches the EntityStore for changes and performs side-effects to keep the EntityTypeMap synchronized with the data in the EntityStore. | ||
*/ | ||
class EntityStoreWatcher { | ||
constructor(config) { | ||
var EntityStoreWatcher = /** @class */ (function () { | ||
function EntityStoreWatcher(config) { | ||
var _this = this; | ||
this.config = config; | ||
this.delete = (dataId, fieldName, args) => { | ||
const { entityStore, entityTypeMap, policies } = this.config; | ||
const result = this.storeFunctions.delete.call(entityStore, dataId, fieldName, args); | ||
const entity = entityTypeMap.readEntityById(helpers_1.makeEntityId(dataId, fieldName)); | ||
const storeFieldName = fieldName && args | ||
this.delete = function (dataId, fieldName, args) { | ||
var _a = _this.config, entityStore = _a.entityStore, entityTypeMap = _a.entityTypeMap, policies = _a.policies; | ||
var result = _this.storeFunctions.delete.call(entityStore, dataId, fieldName, args); | ||
var entity = entityTypeMap.readEntityById(makeEntityId(dataId, fieldName)); | ||
var storeFieldName = fieldName && args | ||
? policies.getStoreFieldName({ | ||
typename: entity ? entity.typename : undefined, | ||
fieldName, | ||
args, | ||
fieldName: fieldName, | ||
args: args, | ||
}) | ||
@@ -25,7 +24,7 @@ : undefined; | ||
}; | ||
this.merge = (dataId, incomingStoreObject) => { | ||
const { entityStore, entityTypeMap } = this.config; | ||
if (helpers_1.isQuery(dataId)) { | ||
this.merge = function (dataId, incomingStoreObject) { | ||
var _a = _this.config, entityStore = _a.entityStore, entityTypeMap = _a.entityTypeMap; | ||
if (isQuery(dataId)) { | ||
Object.keys(incomingStoreObject) | ||
.filter((storeFieldName) => { | ||
.filter(function (storeFieldName) { | ||
var _a; | ||
@@ -38,4 +37,4 @@ // If there is a valid response, it will contain the type Query and then the nested response types for each requested field. We want | ||
}) | ||
.forEach((storeFieldName) => { | ||
const entityStoreObject = incomingStoreObject[storeFieldName]; | ||
.forEach(function (storeFieldName) { | ||
var entityStoreObject = incomingStoreObject[storeFieldName]; | ||
entityTypeMap.write(entityStoreObject.__typename, dataId, storeFieldName); | ||
@@ -45,19 +44,19 @@ }); | ||
else { | ||
const typename = incomingStoreObject.__typename; | ||
var typename = incomingStoreObject.__typename; | ||
// If the incoming data is empty, the dataId entry in the cache is being deleted so do nothing | ||
if (dataId && typename && typename !== utils_1.cacheExtensionsCollectionTypename) { | ||
this.config.updateCollectionField(typename, dataId); | ||
if (dataId && typename && typename !== cacheExtensionsCollectionTypename) { | ||
_this.config.updateCollectionField(typename, dataId); | ||
entityTypeMap.write(typename, dataId); | ||
} | ||
} | ||
return this.storeFunctions.merge.call(entityStore, dataId, incomingStoreObject); | ||
return _this.storeFunctions.merge.call(entityStore, dataId, incomingStoreObject); | ||
}; | ||
this.clear = () => { | ||
const { config: { entityStore, entityTypeMap }, storeFunctions, } = this; | ||
this.clear = function () { | ||
var _a = _this, _b = _a.config, entityStore = _b.entityStore, entityTypeMap = _b.entityTypeMap, storeFunctions = _a.storeFunctions; | ||
entityTypeMap.clear(); | ||
storeFunctions.clear.call(entityStore); | ||
}; | ||
this.replace = (data) => { | ||
const { config: { entityStore, entityTypeMap }, storeFunctions: { replace }, } = this; | ||
const invalidation = data === null || data === void 0 ? void 0 : data.invalidation; | ||
this.replace = function (data) { | ||
var _a = _this, _b = _a.config, entityStore = _b.entityStore, entityTypeMap = _b.entityTypeMap, replace = _a.storeFunctions.replace; | ||
var invalidation = data === null || data === void 0 ? void 0 : data.invalidation; | ||
if (!data || !invalidation) { | ||
@@ -73,17 +72,17 @@ replace.call(entityStore, data); | ||
// so instead we pause the store watcher until the entity store data has been replaced. | ||
this.pause(); | ||
_this.pause(); | ||
replace.call(entityStore, data); | ||
this.watch(); | ||
_this.watch(); | ||
}; | ||
const { entityStore: { clear, delete: deleteKey, merge, replace }, } = this.config; | ||
var _a = this.config.entityStore, clear = _a.clear, deleteKey = _a.delete, merge = _a.merge, replace = _a.replace; | ||
this.storeFunctions = { | ||
clear, | ||
clear: clear, | ||
delete: deleteKey, | ||
merge, | ||
replace, | ||
merge: merge, | ||
replace: replace, | ||
}; | ||
this.watch(); | ||
} | ||
watch() { | ||
const { entityStore } = this.config; | ||
EntityStoreWatcher.prototype.watch = function () { | ||
var entityStore = this.config.entityStore; | ||
entityStore.clear = this.clear; | ||
@@ -93,6 +92,6 @@ entityStore.delete = this.delete; | ||
entityStore.replace = this.replace; | ||
} | ||
pause() { | ||
const { entityStore } = this.config; | ||
const { clear, delete: deleteFunction, merge, replace, } = this.storeFunctions; | ||
}; | ||
EntityStoreWatcher.prototype.pause = function () { | ||
var entityStore = this.config.entityStore; | ||
var _a = this.storeFunctions, clear = _a.clear, deleteFunction = _a.delete, merge = _a.merge, replace = _a.replace; | ||
entityStore.clear = clear; | ||
@@ -102,5 +101,6 @@ entityStore.delete = deleteFunction; | ||
entityStore.replace = replace; | ||
} | ||
} | ||
exports.default = EntityStoreWatcher; | ||
}; | ||
return EntityStoreWatcher; | ||
}()); | ||
export default EntityStoreWatcher; | ||
//# sourceMappingURL=EntityStoreWatcher.js.map |
@@ -1,8 +0,3 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const set_1 = __importDefault(require("lodash/set")); | ||
const helpers_1 = require("../helpers"); | ||
import set from "lodash/set"; | ||
import { makeEntityId, isQuery, fieldNameFromStoreName } from "../helpers"; | ||
/** | ||
@@ -97,16 +92,17 @@ * Map which stores a relationship between entities in the cache and their type | ||
*/ | ||
class EntityTypeMap { | ||
constructor() { | ||
var EntityTypeMap = /** @class */ (function () { | ||
function EntityTypeMap() { | ||
this.entitiesByType = {}; | ||
this.entitiesById = {}; | ||
} | ||
write(typename, dataId, storeFieldName, variables, args) { | ||
const fieldName = storeFieldName | ||
? helpers_1.fieldNameFromStoreName(storeFieldName) | ||
EntityTypeMap.prototype.write = function (typename, dataId, storeFieldName, variables, args) { | ||
var _a; | ||
var fieldName = storeFieldName | ||
? fieldNameFromStoreName(storeFieldName) | ||
: undefined; | ||
const entityId = helpers_1.makeEntityId(dataId, fieldName); | ||
const existingTypeMapEntity = this.readEntityById(entityId); | ||
var entityId = makeEntityId(dataId, fieldName); | ||
var existingTypeMapEntity = this.readEntityById(entityId); | ||
if (existingTypeMapEntity) { | ||
if (helpers_1.isQuery(dataId) && storeFieldName) { | ||
const storeFieldNameEntry = existingTypeMapEntity.storeFieldNames | ||
if (isQuery(dataId) && storeFieldName) { | ||
var storeFieldNameEntry = existingTypeMapEntity.storeFieldNames | ||
.entries[storeFieldName]; | ||
@@ -119,4 +115,4 @@ if (storeFieldNameEntry) { | ||
existingTypeMapEntity.storeFieldNames.entries[storeFieldName] = { | ||
variables, | ||
args, | ||
variables: variables, | ||
args: args, | ||
}; | ||
@@ -128,14 +124,14 @@ existingTypeMapEntity.storeFieldNames.__size++; | ||
else { | ||
let newEntity; | ||
const cacheTime = Date.now(); | ||
if (helpers_1.isQuery(dataId) && storeFieldName) { | ||
var newEntity = void 0; | ||
var cacheTime = Date.now(); | ||
if (isQuery(dataId) && storeFieldName) { | ||
newEntity = { | ||
dataId, | ||
typename, | ||
fieldName, | ||
dataId: dataId, | ||
typename: typename, | ||
fieldName: fieldName, | ||
storeFieldNames: { | ||
__size: 1, | ||
entries: { | ||
[storeFieldName]: { variables, args, cacheTime }, | ||
}, | ||
entries: (_a = {}, | ||
_a[storeFieldName] = { variables: variables, args: args, cacheTime: cacheTime }, | ||
_a), | ||
}, | ||
@@ -146,18 +142,18 @@ }; | ||
newEntity = { | ||
dataId, | ||
typename, | ||
cacheTime, | ||
dataId: dataId, | ||
typename: typename, | ||
cacheTime: cacheTime, | ||
}; | ||
} | ||
set_1.default(this.entitiesByType, [typename, entityId], newEntity); | ||
set(this.entitiesByType, [typename, entityId], newEntity); | ||
this.entitiesById[entityId] = newEntity; | ||
} | ||
} | ||
evict(dataId, storeFieldName) { | ||
}; | ||
EntityTypeMap.prototype.evict = function (dataId, storeFieldName) { | ||
var _a; | ||
const fieldName = storeFieldName | ||
? helpers_1.fieldNameFromStoreName(storeFieldName) | ||
var fieldName = storeFieldName | ||
? fieldNameFromStoreName(storeFieldName) | ||
: null; | ||
const entityId = helpers_1.makeEntityId(dataId, fieldName); | ||
const entity = this.readEntityById(entityId); | ||
var entityId = makeEntityId(dataId, fieldName); | ||
var entity = this.readEntityById(entityId); | ||
if (!entity) { | ||
@@ -169,3 +165,3 @@ return; | ||
if (storeFieldName && fieldName !== storeFieldName) { | ||
const storeFieldNameEntries = (_a = this.entitiesByType[entity.typename][entityId]) === null || _a === void 0 ? void 0 : _a.storeFieldNames; | ||
var storeFieldNameEntries = (_a = this.entitiesByType[entity.typename][entityId]) === null || _a === void 0 ? void 0 : _a.storeFieldNames; | ||
if (storeFieldNameEntries) { | ||
@@ -186,18 +182,18 @@ if (storeFieldNameEntries.__size === 1) { | ||
} | ||
} | ||
readEntitiesByType(typeName) { | ||
}; | ||
EntityTypeMap.prototype.readEntitiesByType = function (typeName) { | ||
return this.entitiesByType[typeName] || null; | ||
} | ||
readEntityById(entityId) { | ||
}; | ||
EntityTypeMap.prototype.readEntityById = function (entityId) { | ||
return this.entitiesById[entityId] || null; | ||
} | ||
renewEntity(dataId, storeFieldName) { | ||
const fieldName = storeFieldName | ||
? helpers_1.fieldNameFromStoreName(storeFieldName) | ||
}; | ||
EntityTypeMap.prototype.renewEntity = function (dataId, storeFieldName) { | ||
var fieldName = storeFieldName | ||
? fieldNameFromStoreName(storeFieldName) | ||
: undefined; | ||
const entity = this.entitiesById[helpers_1.makeEntityId(dataId, fieldName)]; | ||
var entity = this.entitiesById[makeEntityId(dataId, fieldName)]; | ||
if (entity) { | ||
const cacheTime = Date.now(); | ||
if (helpers_1.isQuery(dataId) && storeFieldName) { | ||
const storeFieldNameEntry = entity.storeFieldNames.entries[storeFieldName]; | ||
var cacheTime = Date.now(); | ||
if (isQuery(dataId) && storeFieldName) { | ||
var storeFieldNameEntry = entity.storeFieldNames.entries[storeFieldName]; | ||
if (storeFieldNameEntry) { | ||
@@ -211,26 +207,28 @@ storeFieldNameEntry.cacheTime = cacheTime; | ||
} | ||
} | ||
restore(entitiesById) { | ||
}; | ||
EntityTypeMap.prototype.restore = function (entitiesById) { | ||
var _this = this; | ||
this.entitiesById = entitiesById; | ||
Object.keys(entitiesById).forEach((entityId) => { | ||
const entity = entitiesById[entityId]; | ||
if (!this.entitiesByType[entity.typename]) { | ||
this.entitiesByType[entity.typename] = {}; | ||
Object.keys(entitiesById).forEach(function (entityId) { | ||
var entity = entitiesById[entityId]; | ||
if (!_this.entitiesByType[entity.typename]) { | ||
_this.entitiesByType[entity.typename] = {}; | ||
} | ||
this.entitiesByType[entity.typename][entityId] = entity; | ||
_this.entitiesByType[entity.typename][entityId] = entity; | ||
}); | ||
} | ||
extract() { | ||
const { entitiesById, entitiesByType } = this; | ||
}; | ||
EntityTypeMap.prototype.extract = function () { | ||
var _a = this, entitiesById = _a.entitiesById, entitiesByType = _a.entitiesByType; | ||
return { | ||
entitiesById, | ||
entitiesByType, | ||
entitiesById: entitiesById, | ||
entitiesByType: entitiesByType, | ||
}; | ||
} | ||
clear() { | ||
}; | ||
EntityTypeMap.prototype.clear = function () { | ||
this.entitiesById = {}; | ||
this.entitiesByType = {}; | ||
} | ||
} | ||
exports.default = EntityTypeMap; | ||
}; | ||
return EntityTypeMap; | ||
}()); | ||
export default EntityTypeMap; | ||
//# sourceMappingURL=EntityTypeMap.js.map |
@@ -1,11 +0,3 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EntityTypeMap = exports.EntityStoreWatcher = void 0; | ||
var EntityStoreWatcher_1 = require("./EntityStoreWatcher"); | ||
Object.defineProperty(exports, "EntityStoreWatcher", { enumerable: true, get: function () { return __importDefault(EntityStoreWatcher_1).default; } }); | ||
var EntityTypeMap_1 = require("./EntityTypeMap"); | ||
Object.defineProperty(exports, "EntityTypeMap", { enumerable: true, get: function () { return __importDefault(EntityTypeMap_1).default; } }); | ||
export { default as EntityStoreWatcher } from "./EntityStoreWatcher"; | ||
export { default as EntityTypeMap } from "./EntityTypeMap"; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
export {}; | ||
//# sourceMappingURL=types.js.map |
@@ -1,15 +0,8 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.generateFragmentFieldName = exports.fieldNameFromStoreName = exports.TypeOrFieldNameRegExp = exports.maybeDeepClone = exports.makeEntityId = exports.isQuery = void 0; | ||
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep")); | ||
const compact_1 = __importDefault(require("lodash/compact")); | ||
const isPlainObject_1 = __importDefault(require("lodash/isPlainObject")); | ||
const uuid_1 = require("uuid"); | ||
function isQuery(dataId) { | ||
import cloneDeep from "lodash/cloneDeep"; | ||
import compact from "lodash/compact"; | ||
import isPlainObject from "lodash/isPlainObject"; | ||
import { v4 } from "uuid"; | ||
export function isQuery(dataId) { | ||
return dataId === "ROOT_QUERY" || dataId === "ROOT_MUTATION"; | ||
} | ||
exports.isQuery = isQuery; | ||
/** | ||
@@ -22,20 +15,19 @@ * Returns a store entity ID matching the path at which the entity is found in the entity store. | ||
*/ | ||
function makeEntityId(dataId, fieldName) { | ||
export function makeEntityId(dataId, fieldName) { | ||
if (isQuery(dataId)) { | ||
return compact_1.default([dataId, fieldName]).join("."); | ||
return compact([dataId, fieldName]).join("."); | ||
} | ||
return dataId; | ||
} | ||
exports.makeEntityId = makeEntityId; | ||
// In development, results are frozen and updating them as part of executing the read policy must be done | ||
// on a cloned object. This has no impact in production since objects are not frozen and will not be cloned: | ||
// https://github.com/apollographql/apollo-client/blob/master/src/utilities/common/maybeDeepFreeze.ts#L20:L20 | ||
const maybeDeepClone = (obj) => isPlainObject_1.default(obj) && Object.isFrozen(obj) ? cloneDeep_1.default(obj) : obj; | ||
exports.maybeDeepClone = maybeDeepClone; | ||
exports.TypeOrFieldNameRegExp = /^[_a-z][_0-9a-z]*/i; | ||
function fieldNameFromStoreName(storeFieldName) { | ||
var match = storeFieldName.match(exports.TypeOrFieldNameRegExp); | ||
export var maybeDeepClone = function (obj) { | ||
return isPlainObject(obj) && Object.isFrozen(obj) ? cloneDeep(obj) : obj; | ||
}; | ||
export var TypeOrFieldNameRegExp = /^[_a-z][_0-9a-z]*/i; | ||
export function fieldNameFromStoreName(storeFieldName) { | ||
var match = storeFieldName.match(TypeOrFieldNameRegExp); | ||
return match ? match[0] : storeFieldName; | ||
} | ||
exports.fieldNameFromStoreName = fieldNameFromStoreName; | ||
// When generating a dynamic field name for a fragment type policy, we use a uuid with | ||
@@ -45,6 +37,5 @@ // a leading prefix in order to distinguish it as a fragment policy and | ||
// https://github.com/apollographql/apollo-client/blob/d9a1039d36801d450a79cb56870f0a351044254b/src/cache/inmemory/helpers.ts#L80 | ||
function generateFragmentFieldName() { | ||
return `-fragment-${uuid_1.v4()}`; | ||
export function generateFragmentFieldName() { | ||
return "-fragment-" + v4(); | ||
} | ||
exports.generateFragmentFieldName = generateFragmentFieldName; | ||
//# sourceMappingURL=helpers.js.map |
@@ -1,11 +0,3 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useFragmentWhere = exports.useFragment = void 0; | ||
var useFragment_1 = require("./useFragment"); | ||
Object.defineProperty(exports, "useFragment", { enumerable: true, get: function () { return __importDefault(useFragment_1).default; } }); | ||
var useFragmentWhere_1 = require("./useFragmentWhere"); | ||
Object.defineProperty(exports, "useFragmentWhere", { enumerable: true, get: function () { return __importDefault(useFragmentWhere_1).default; } }); | ||
export { default as useFragment } from './useFragment'; | ||
export { default as useFragmentWhere } from './useFragmentWhere'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,24 +0,21 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const client_1 = require("@apollo/client"); | ||
const react_1 = require("react"); | ||
const utils_1 = require("./utils"); | ||
const utils_2 = require("../client/utils"); | ||
const useFragmentTypePolicyFieldName_1 = require("./useFragmentTypePolicyFieldName"); | ||
const useGetQueryDataByFieldName_1 = require("./useGetQueryDataByFieldName"); | ||
import { getApolloContext } from '@apollo/client'; | ||
import { useContext } from 'react'; | ||
import { useOnce } from './utils'; | ||
import { buildWatchFragmentQuery } from '../client/utils'; | ||
import { useFragmentTypePolicyFieldName } from './useFragmentTypePolicyFieldName'; | ||
import { useGetQueryDataByFieldName } from './useGetQueryDataByFieldName'; | ||
// A hook for subscribing to a fragment in the Apollo cache from a React component. | ||
function useFragment(fragment, options) { | ||
const context = react_1.useContext(client_1.getApolloContext()); | ||
const client = context.client; | ||
const cache = client === null || client === void 0 ? void 0 : client.cache; | ||
const fieldName = useFragmentTypePolicyFieldName_1.useFragmentTypePolicyFieldName(); | ||
const queryForFragment = utils_1.useOnce(() => utils_2.buildWatchFragmentQuery({ | ||
fragment, | ||
fieldName, | ||
export default function useFragment(fragment, options) { | ||
var context = useContext(getApolloContext()); | ||
var client = context.client; | ||
var cache = client === null || client === void 0 ? void 0 : client.cache; | ||
var fieldName = useFragmentTypePolicyFieldName(); | ||
var queryForFragment = useOnce(function () { return buildWatchFragmentQuery({ | ||
fragment: fragment, | ||
fieldName: fieldName, | ||
id: options.id, | ||
policies: cache.policies, | ||
})); | ||
return useGetQueryDataByFieldName_1.useGetQueryDataByFieldName(queryForFragment, fieldName); | ||
}); }); | ||
return useGetQueryDataByFieldName(queryForFragment, fieldName); | ||
} | ||
exports.default = useFragment; | ||
//# sourceMappingURL=useFragment.js.map |
@@ -1,18 +0,15 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useFragmentTypePolicyFieldName = void 0; | ||
const client_1 = require("@apollo/client"); | ||
const react_1 = require("react"); | ||
const helpers_1 = require("../helpers"); | ||
import { useApolloClient } from "@apollo/client"; | ||
import { useEffect, useRef } from "react"; | ||
import { generateFragmentFieldName } from "../helpers"; | ||
// Creates a field name to be used for a dynamically added field policy. | ||
const useFragmentTypePolicyFieldName = () => { | ||
const { current: fieldName } = react_1.useRef(helpers_1.generateFragmentFieldName()); | ||
const client = client_1.useApolloClient(); | ||
react_1.useEffect(() => | ||
// @ts-ignore After the component using the hook is torn down, remove the dynamically added type policy | ||
// for this hook from the type policies list. | ||
() => delete client.cache.policies.typePolicies.Query.fields[fieldName], []); | ||
export var useFragmentTypePolicyFieldName = function () { | ||
var fieldName = useRef(generateFragmentFieldName()).current; | ||
var client = useApolloClient(); | ||
useEffect(function () { | ||
// @ts-ignore After the component using the hook is torn down, remove the dynamically added type policy | ||
// for this hook from the type policies list. | ||
return function () { return delete client.cache.policies.typePolicies.Query.fields[fieldName]; }; | ||
}, []); | ||
return fieldName; | ||
}; | ||
exports.useFragmentTypePolicyFieldName = useFragmentTypePolicyFieldName; | ||
//# sourceMappingURL=useFragmentTypePolicyFieldName.js.map |
@@ -1,25 +0,22 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const client_1 = require("@apollo/client"); | ||
const react_1 = require("react"); | ||
const utils_1 = require("../client/utils"); | ||
const utils_2 = require("./utils"); | ||
const useFragmentTypePolicyFieldName_1 = require("./useFragmentTypePolicyFieldName"); | ||
const useGetQueryDataByFieldName_1 = require("./useGetQueryDataByFieldName"); | ||
import { getApolloContext } from '@apollo/client'; | ||
import { useContext } from 'react'; | ||
import { buildWatchFragmentWhereQuery } from '../client/utils'; | ||
import { useOnce } from './utils'; | ||
import { useFragmentTypePolicyFieldName } from './useFragmentTypePolicyFieldName'; | ||
import { useGetQueryDataByFieldName } from './useGetQueryDataByFieldName'; | ||
// A hook for subscribing to a fragment for entities in the Apollo cache matching a given filter from a React component. | ||
function useFragmentWhere(fragment, filter) { | ||
const context = react_1.useContext(client_1.getApolloContext()); | ||
const client = context.client; | ||
const cache = client === null || client === void 0 ? void 0 : client.cache; | ||
const fieldName = useFragmentTypePolicyFieldName_1.useFragmentTypePolicyFieldName(); | ||
const query = utils_2.useOnce(() => utils_1.buildWatchFragmentWhereQuery({ | ||
filter, | ||
fragment, | ||
fieldName, | ||
cache, | ||
export default function useFragmentWhere(fragment, filter) { | ||
var context = useContext(getApolloContext()); | ||
var client = context.client; | ||
var cache = client === null || client === void 0 ? void 0 : client.cache; | ||
var fieldName = useFragmentTypePolicyFieldName(); | ||
var query = useOnce(function () { return buildWatchFragmentWhereQuery({ | ||
filter: filter, | ||
fragment: fragment, | ||
fieldName: fieldName, | ||
cache: cache, | ||
policies: cache.policies, | ||
})); | ||
return useGetQueryDataByFieldName_1.useGetQueryDataByFieldName(query, fieldName); | ||
}); }); | ||
return useGetQueryDataByFieldName(query, fieldName); | ||
} | ||
exports.default = useFragmentWhere; | ||
//# sourceMappingURL=useFragmentWhere.js.map |
@@ -1,16 +0,23 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useGetQueryDataByFieldName = void 0; | ||
const client_1 = require("@apollo/client"); | ||
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); | ||
}; | ||
import { useQuery } from "@apollo/client"; | ||
// A hook that subscribes to a query with useQuery and gets the data under a particular field name | ||
// of the raw useQuery response as the new data response. | ||
const useGetQueryDataByFieldName = (query, fieldName) => { | ||
export var useGetQueryDataByFieldName = function (query, fieldName) { | ||
var _a; | ||
const result = client_1.useQuery(query, { | ||
var result = useQuery(query, { | ||
fetchPolicy: 'cache-only', | ||
}); | ||
const requiredDataResult = Object.assign(Object.assign({}, result), { data: (_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a[fieldName] }); | ||
var requiredDataResult = __assign(__assign({}, result), { data: (_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a[fieldName] }); | ||
return requiredDataResult; | ||
}; | ||
exports.useGetQueryDataByFieldName = useGetQueryDataByFieldName; | ||
//# sourceMappingURL=useGetQueryDataByFieldName.js.map |
@@ -1,14 +0,8 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useOnce = void 0; | ||
const react_1 = require("react"); | ||
const isFunction_1 = __importDefault(require("lodash/isFunction")); | ||
function useOnce(value) { | ||
const valueRef = react_1.useRef(); | ||
const hasCachedValueRef = react_1.useRef(false); | ||
import { useRef } from 'react'; | ||
import isFunction from "lodash/isFunction"; | ||
export function useOnce(value) { | ||
var valueRef = useRef(); | ||
var hasCachedValueRef = useRef(false); | ||
if (!hasCachedValueRef.current) { | ||
if (isFunction_1.default(value)) { | ||
if (isFunction(value)) { | ||
valueRef.current = value(); | ||
@@ -23,3 +17,2 @@ } | ||
} | ||
exports.useOnce = useOnce; | ||
//# sourceMappingURL=utils.js.map |
@@ -1,16 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RenewalPolicy = exports.InvalidationPolicyEvent = exports.InvalidationPolicyCacheAuditor = exports.useFragmentWhere = exports.useFragment = exports.ApolloExtensionsClient = exports.InvalidationPolicyCache = void 0; | ||
var cache_1 = require("./cache"); | ||
Object.defineProperty(exports, "InvalidationPolicyCache", { enumerable: true, get: function () { return cache_1.InvalidationPolicyCache; } }); | ||
var client_1 = require("./client"); | ||
Object.defineProperty(exports, "ApolloExtensionsClient", { enumerable: true, get: function () { return client_1.ApolloExtensionsClient; } }); | ||
var hooks_1 = require("./hooks"); | ||
Object.defineProperty(exports, "useFragment", { enumerable: true, get: function () { return hooks_1.useFragment; } }); | ||
Object.defineProperty(exports, "useFragmentWhere", { enumerable: true, get: function () { return hooks_1.useFragmentWhere; } }); | ||
var audit_1 = require("./audit"); | ||
Object.defineProperty(exports, "InvalidationPolicyCacheAuditor", { enumerable: true, get: function () { return audit_1.InvalidationPolicyCacheAuditor; } }); | ||
var types_1 = require("./policies/types"); | ||
Object.defineProperty(exports, "InvalidationPolicyEvent", { enumerable: true, get: function () { return types_1.InvalidationPolicyEvent; } }); | ||
Object.defineProperty(exports, "RenewalPolicy", { enumerable: true, get: function () { return types_1.RenewalPolicy; } }); | ||
export { InvalidationPolicyCache } from "./cache"; | ||
export { ApolloExtensionsClient } from "./client"; | ||
export { useFragment, useFragmentWhere } from './hooks'; | ||
export { InvalidationPolicyCacheAuditor } from "./audit"; | ||
export { InvalidationPolicyEvent, RenewalPolicy, } from "./policies/types"; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,12 @@ | ||
"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 __rest = (this && this.__rest) || function (s, e) { | ||
@@ -13,54 +23,51 @@ var t = {}; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const isNumber_1 = __importDefault(require("lodash/isNumber")); | ||
const types_1 = require("./types"); | ||
const helpers_1 = require("../helpers"); | ||
const core_1 = require("@apollo/client/core"); | ||
const types_2 = require("./types"); | ||
import isNumber from "lodash/isNumber"; | ||
import { InvalidationPolicyEvent, InvalidationPolicyLifecycleEvent, } from "./types"; | ||
import { makeEntityId } from "../helpers"; | ||
import { makeReference } from "@apollo/client/core"; | ||
import { RenewalPolicy } from "./types"; | ||
/** | ||
* Executes cache policies for types when they are modified, evicted or read from the cache. | ||
*/ | ||
class InvalidationPolicyManager { | ||
constructor(config) { | ||
var InvalidationPolicyManager = /** @class */ (function () { | ||
function InvalidationPolicyManager(config) { | ||
this.config = config; | ||
this.policyActionStorage = {}; | ||
const { cacheOperations: { readField, evict, modify }, } = this.config; | ||
var _a = this.config.cacheOperations, readField = _a.readField, evict = _a.evict, modify = _a.modify; | ||
// Watch broadcasts by evict and modify operations called by policy actions | ||
// are suppressed until after all policy actions have run. | ||
this.mutedCacheOperations = { | ||
readField, | ||
evict: (options) => evict(Object.assign(Object.assign({}, options), { broadcast: false })), | ||
modify: (options) => modify(Object.assign(Object.assign({}, options), { broadcast: false })), | ||
readField: readField, | ||
evict: function (options) { return evict(__assign(__assign({}, options), { broadcast: false })); }, | ||
modify: function (options) { return modify(__assign(__assign({}, options), { broadcast: false })); }, | ||
}; | ||
this.activePolicyEvents = this.activateInitialPolicyEvents(); | ||
} | ||
activateInitialPolicyEvents() { | ||
const { policies } = this.config; | ||
const { types: policyTypes = {}, timeToLive: defaultTimeToLive } = policies; | ||
return Object.keys(policyTypes).reduce((acc, type) => { | ||
const policy = policyTypes[type]; | ||
acc[types_1.InvalidationPolicyEvent.Read] = | ||
acc[types_1.InvalidationPolicyEvent.Read] || !!policy.timeToLive; | ||
acc[types_1.InvalidationPolicyEvent.Write] = | ||
acc[types_1.InvalidationPolicyEvent.Write] || | ||
!!policy[types_1.InvalidationPolicyLifecycleEvent.Write]; | ||
acc[types_1.InvalidationPolicyEvent.Evict] = | ||
acc[types_1.InvalidationPolicyEvent.Evict] || | ||
!!policy[types_1.InvalidationPolicyLifecycleEvent.Evict]; | ||
InvalidationPolicyManager.prototype.activateInitialPolicyEvents = function () { | ||
var _a; | ||
var policies = this.config.policies; | ||
var _b = policies.types, policyTypes = _b === void 0 ? {} : _b, defaultTimeToLive = policies.timeToLive; | ||
return Object.keys(policyTypes).reduce(function (acc, type) { | ||
var policy = policyTypes[type]; | ||
acc[InvalidationPolicyEvent.Read] = | ||
acc[InvalidationPolicyEvent.Read] || !!policy.timeToLive; | ||
acc[InvalidationPolicyEvent.Write] = | ||
acc[InvalidationPolicyEvent.Write] || | ||
!!policy[InvalidationPolicyLifecycleEvent.Write]; | ||
acc[InvalidationPolicyEvent.Evict] = | ||
acc[InvalidationPolicyEvent.Evict] || | ||
!!policy[InvalidationPolicyLifecycleEvent.Evict]; | ||
return acc; | ||
}, { | ||
[types_1.InvalidationPolicyEvent.Read]: !!defaultTimeToLive, | ||
[types_1.InvalidationPolicyEvent.Write]: false, | ||
[types_1.InvalidationPolicyEvent.Evict]: false, | ||
}); | ||
} | ||
getPolicy(typeName) { | ||
}, (_a = {}, | ||
_a[InvalidationPolicyEvent.Read] = !!defaultTimeToLive, | ||
_a[InvalidationPolicyEvent.Write] = false, | ||
_a[InvalidationPolicyEvent.Evict] = false, | ||
_a)); | ||
}; | ||
InvalidationPolicyManager.prototype.getPolicy = function (typeName) { | ||
var _a, _b; | ||
return ((_b = (_a = this.config.policies) === null || _a === void 0 ? void 0 : _a.types) === null || _b === void 0 ? void 0 : _b[typeName]) || null; | ||
} | ||
getPolicyActionStorage(identifier) { | ||
const existingStorage = this.policyActionStorage[identifier]; | ||
}; | ||
InvalidationPolicyManager.prototype.getPolicyActionStorage = function (identifier) { | ||
var existingStorage = this.policyActionStorage[identifier]; | ||
if (!existingStorage) { | ||
@@ -70,21 +77,22 @@ this.policyActionStorage[identifier] = {}; | ||
return this.policyActionStorage[identifier]; | ||
} | ||
getTypePolicyForEvent(typeName, policyEvent) { | ||
const policyForType = this.getPolicy(typeName); | ||
}; | ||
InvalidationPolicyManager.prototype.getTypePolicyForEvent = function (typeName, policyEvent) { | ||
var policyForType = this.getPolicy(typeName); | ||
if (!policyForType) { | ||
return null; | ||
} | ||
return policyForType[types_1.InvalidationPolicyLifecycleEvent[policyEvent]]; | ||
} | ||
runPolicyEvent(typeName, policyEvent, policyMeta) { | ||
const { entityTypeMap } = this.config; | ||
const { mutedCacheOperations } = this; | ||
const typePolicyForEvent = this.getTypePolicyForEvent(typeName, policyEvent); | ||
return policyForType[InvalidationPolicyLifecycleEvent[policyEvent]]; | ||
}; | ||
InvalidationPolicyManager.prototype.runPolicyEvent = function (typeName, policyEvent, policyMeta) { | ||
var _this = this; | ||
var entityTypeMap = this.config.entityTypeMap; | ||
var mutedCacheOperations = this.mutedCacheOperations; | ||
var typePolicyForEvent = this.getTypePolicyForEvent(typeName, policyEvent); | ||
if (!typePolicyForEvent) { | ||
return; | ||
} | ||
let defaultPolicyAction; | ||
let restTypePolicyTypeNames = {}; | ||
var defaultPolicyAction; | ||
var restTypePolicyTypeNames = {}; | ||
if (typeof typePolicyForEvent === 'object') { | ||
let { __default: _defaultPolicyAction } = typePolicyForEvent, _restTypePolicyTypeNames = __rest(typePolicyForEvent, ["__default"]); | ||
var _defaultPolicyAction = typePolicyForEvent.__default, _restTypePolicyTypeNames = __rest(typePolicyForEvent, ["__default"]); | ||
defaultPolicyAction = _defaultPolicyAction; | ||
@@ -97,43 +105,44 @@ restTypePolicyTypeNames = _restTypePolicyTypeNames; | ||
if (defaultPolicyAction) { | ||
defaultPolicyAction(mutedCacheOperations, Object.assign({ storage: this.getPolicyActionStorage(`${typeName}__default`) }, policyMeta)); | ||
defaultPolicyAction(mutedCacheOperations, __assign({ storage: this.getPolicyActionStorage(typeName + "__default") }, policyMeta)); | ||
} | ||
Object.keys(restTypePolicyTypeNames).forEach((typePolicyTypeName) => { | ||
Object.keys(restTypePolicyTypeNames).forEach(function (typePolicyTypeName) { | ||
var _a; | ||
const typeMapEntities = (_a = entityTypeMap.readEntitiesByType(typePolicyTypeName)) !== null && _a !== void 0 ? _a : {}; | ||
const policyAction = typePolicyForEvent[typePolicyTypeName]; | ||
Object.values(typeMapEntities).forEach((typeMapEntity) => { | ||
const { dataId, fieldName, storeFieldNames } = typeMapEntity; | ||
var typeMapEntities = (_a = entityTypeMap.readEntitiesByType(typePolicyTypeName)) !== null && _a !== void 0 ? _a : {}; | ||
var policyAction = typePolicyForEvent[typePolicyTypeName]; | ||
Object.values(typeMapEntities).forEach(function (typeMapEntity) { | ||
var dataId = typeMapEntity.dataId, fieldName = typeMapEntity.fieldName, storeFieldNames = typeMapEntity.storeFieldNames; | ||
if (storeFieldNames) { | ||
Object.keys(storeFieldNames.entries).forEach((storeFieldName) => { | ||
policyAction(mutedCacheOperations, Object.assign({ id: dataId, fieldName, | ||
storeFieldName, variables: storeFieldNames.entries[storeFieldName].variables, args: storeFieldNames.entries[storeFieldName].args, ref: core_1.makeReference(dataId), storage: this.getPolicyActionStorage(storeFieldName) }, policyMeta)); | ||
Object.keys(storeFieldNames.entries).forEach(function (storeFieldName) { | ||
policyAction(mutedCacheOperations, __assign({ id: dataId, fieldName: fieldName, | ||
storeFieldName: storeFieldName, variables: storeFieldNames.entries[storeFieldName].variables, args: storeFieldNames.entries[storeFieldName].args, ref: makeReference(dataId), storage: _this.getPolicyActionStorage(storeFieldName) }, policyMeta)); | ||
}); | ||
} | ||
else { | ||
policyAction(mutedCacheOperations, Object.assign({ id: dataId, storage: this.getPolicyActionStorage(dataId), ref: core_1.makeReference(dataId) }, policyMeta)); | ||
policyAction(mutedCacheOperations, __assign({ id: dataId, storage: _this.getPolicyActionStorage(dataId), ref: makeReference(dataId) }, policyMeta)); | ||
} | ||
}); | ||
}); | ||
} | ||
getRenewalPolicyForType(typename) { | ||
}; | ||
InvalidationPolicyManager.prototype.getRenewalPolicyForType = function (typename) { | ||
var _a, _b, _c, _d; | ||
const { policies } = this.config; | ||
return ((_d = (_c = (_b = (_a = policies.types) === null || _a === void 0 ? void 0 : _a[typename]) === null || _b === void 0 ? void 0 : _b.renewalPolicy) !== null && _c !== void 0 ? _c : policies.renewalPolicy) !== null && _d !== void 0 ? _d : types_2.RenewalPolicy.WriteOnly); | ||
} | ||
runWritePolicy(typeName, policyMeta) { | ||
return this.runPolicyEvent(typeName, types_1.InvalidationPolicyEvent.Write, policyMeta); | ||
} | ||
runEvictPolicy(typeName, policyMeta) { | ||
return this.runPolicyEvent(typeName, types_1.InvalidationPolicyEvent.Evict, policyMeta); | ||
} | ||
var policies = this.config.policies; | ||
return ((_d = (_c = (_b = (_a = policies.types) === null || _a === void 0 ? void 0 : _a[typename]) === null || _b === void 0 ? void 0 : _b.renewalPolicy) !== null && _c !== void 0 ? _c : policies.renewalPolicy) !== null && _d !== void 0 ? _d : RenewalPolicy.WriteOnly); | ||
}; | ||
InvalidationPolicyManager.prototype.runWritePolicy = function (typeName, policyMeta) { | ||
return this.runPolicyEvent(typeName, InvalidationPolicyEvent.Write, policyMeta); | ||
}; | ||
InvalidationPolicyManager.prototype.runEvictPolicy = function (typeName, policyMeta) { | ||
return this.runPolicyEvent(typeName, InvalidationPolicyEvent.Evict, policyMeta); | ||
}; | ||
// Runs the read poliy on the entity, returning whether its TTL was expired. | ||
runReadPolicy({ typename, dataId, fieldName, storeFieldName, reportOnly = false, }) { | ||
var _a; | ||
const { cacheOperations, entityTypeMap, policies } = this.config; | ||
const entityId = helpers_1.makeEntityId(dataId, fieldName); | ||
const typeMapEntity = entityTypeMap.readEntityById(entityId); | ||
InvalidationPolicyManager.prototype.runReadPolicy = function (_a) { | ||
var _b; | ||
var typename = _a.typename, dataId = _a.dataId, fieldName = _a.fieldName, storeFieldName = _a.storeFieldName, _c = _a.reportOnly, reportOnly = _c === void 0 ? false : _c; | ||
var _d = this.config, cacheOperations = _d.cacheOperations, entityTypeMap = _d.entityTypeMap, policies = _d.policies; | ||
var entityId = makeEntityId(dataId, fieldName); | ||
var typeMapEntity = entityTypeMap.readEntityById(entityId); | ||
if (!typeMapEntity) { | ||
return false; | ||
} | ||
let entityCacheTime; | ||
var entityCacheTime; | ||
// If a read is done against an entity before it has ever been written, it would not be present in the cache yet and should not attempt | ||
@@ -144,3 +153,3 @@ // to have read policy eviction run on it. This can occur in the case of fetching a query field over the network for example, where first | ||
if (storeFieldName && !!typeMapEntity.storeFieldNames) { | ||
const entityForStoreFieldName = typeMapEntity.storeFieldNames.entries[storeFieldName]; | ||
var entityForStoreFieldName = typeMapEntity.storeFieldNames.entries[storeFieldName]; | ||
if (!entityForStoreFieldName) { | ||
@@ -154,4 +163,4 @@ return false; | ||
} | ||
const timeToLive = ((_a = this.getPolicy(typename)) === null || _a === void 0 ? void 0 : _a.timeToLive) || policies.timeToLive; | ||
if (isNumber_1.default(entityCacheTime) && | ||
var timeToLive = ((_b = this.getPolicy(typename)) === null || _b === void 0 ? void 0 : _b.timeToLive) || policies.timeToLive; | ||
if (isNumber(entityCacheTime) && | ||
timeToLive && | ||
@@ -169,14 +178,25 @@ Date.now() > entityCacheTime + timeToLive) { | ||
return false; | ||
} | ||
activatePolicies(...policyEvents) { | ||
policyEvents.forEach(policyEvent => this.activePolicyEvents[policyEvent] = true); | ||
} | ||
deactivatePolicies(...policyEvents) { | ||
policyEvents.forEach(policyEvent => this.activePolicyEvents[policyEvent] = false); | ||
} | ||
isPolicyEventActive(policyEvent) { | ||
}; | ||
InvalidationPolicyManager.prototype.activatePolicies = function () { | ||
var _this = this; | ||
var policyEvents = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
policyEvents[_i] = arguments[_i]; | ||
} | ||
policyEvents.forEach(function (policyEvent) { return _this.activePolicyEvents[policyEvent] = true; }); | ||
}; | ||
InvalidationPolicyManager.prototype.deactivatePolicies = function () { | ||
var _this = this; | ||
var policyEvents = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
policyEvents[_i] = arguments[_i]; | ||
} | ||
policyEvents.forEach(function (policyEvent) { return _this.activePolicyEvents[policyEvent] = false; }); | ||
}; | ||
InvalidationPolicyManager.prototype.isPolicyEventActive = function (policyEvent) { | ||
return this.activePolicyEvents[policyEvent]; | ||
} | ||
} | ||
exports.default = InvalidationPolicyManager; | ||
}; | ||
return InvalidationPolicyManager; | ||
}()); | ||
export default InvalidationPolicyManager; | ||
//# sourceMappingURL=InvalidationPolicyManager.js.map |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RenewalPolicy = exports.InvalidationPolicyLifecycleEvent = exports.InvalidationPolicyEvent = void 0; | ||
var InvalidationPolicyEvent; | ||
export var InvalidationPolicyEvent; | ||
(function (InvalidationPolicyEvent) { | ||
@@ -9,9 +6,9 @@ InvalidationPolicyEvent["Write"] = "Write"; | ||
InvalidationPolicyEvent["Read"] = "Read"; | ||
})(InvalidationPolicyEvent = exports.InvalidationPolicyEvent || (exports.InvalidationPolicyEvent = {})); | ||
var InvalidationPolicyLifecycleEvent; | ||
})(InvalidationPolicyEvent || (InvalidationPolicyEvent = {})); | ||
export var InvalidationPolicyLifecycleEvent; | ||
(function (InvalidationPolicyLifecycleEvent) { | ||
InvalidationPolicyLifecycleEvent["Write"] = "onWrite"; | ||
InvalidationPolicyLifecycleEvent["Evict"] = "onEvict"; | ||
})(InvalidationPolicyLifecycleEvent = exports.InvalidationPolicyLifecycleEvent || (exports.InvalidationPolicyLifecycleEvent = {})); | ||
var RenewalPolicy; | ||
})(InvalidationPolicyLifecycleEvent || (InvalidationPolicyLifecycleEvent = {})); | ||
export var RenewalPolicy; | ||
(function (RenewalPolicy) { | ||
@@ -22,3 +19,3 @@ RenewalPolicy["AccessOnly"] = "access-only"; | ||
RenewalPolicy["None"] = "none"; | ||
})(RenewalPolicy = exports.RenewalPolicy || (exports.RenewalPolicy = {})); | ||
})(RenewalPolicy || (RenewalPolicy = {})); | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "@nerdwallet/apollo-cache-policies", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "An extension to the InMemoryCache from Apollo that adds additional cache policies.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
316432
2369