apollo-invalidation-policies
Advanced tools
Comparing version
@@ -0,1 +1,6 @@ | ||
1.0.0-beta12 (Dan Reynolds) | ||
- Add support for dynamically activating/deactivating policy events | ||
1.0.0-beta11 (Dan Reynolds) | ||
@@ -2,0 +7,0 @@ |
@@ -8,2 +8,3 @@ import { InMemoryCache, Cache, NormalizedCacheObject, Reference, StoreObject } from "@apollo/client"; | ||
import { CacheResultProcessor } from "./CacheResultProcessor"; | ||
import { InvalidationPolicyEvent } from "../policies/types"; | ||
/** | ||
@@ -25,5 +26,8 @@ * Extension of Apollo in-memory cache which adds support for invalidation policies. | ||
evict(options: Cache.EvictOptions): boolean; | ||
_expire(reportOnly?: boolean): string[]; | ||
private _expire; | ||
expire(): string[]; | ||
expiredEntities(): string[]; | ||
activatePolicyEvents(...policyEvents: InvalidationPolicyEvent[]): void; | ||
deactivatePolicyEvents(...policyEvents: InvalidationPolicyEvent[]): void; | ||
activePolicyEvents(): InvalidationPolicyEvent[]; | ||
read<T>(options: Cache.ReadOptions<any>): T | null; | ||
@@ -30,0 +34,0 @@ diff<T>(options: Cache.DiffOptions): Cache.DiffResult<T>; |
@@ -87,3 +87,3 @@ "use strict"; | ||
const modifyResult = super.modify(options); | ||
if (!this.invalidationPolicyManager.isPolicyActive(types_1.InvalidationPolicyEvent.Write) || | ||
if (!this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Write) || | ||
!modifyResult) { | ||
@@ -132,4 +132,4 @@ return modifyResult; | ||
// the policy will later be applied when the server data response is received. | ||
if ((!this.invalidationPolicyManager.isPolicyActive(types_1.InvalidationPolicyEvent.Write) && | ||
!this.invalidationPolicyManager.isPolicyActive(types_1.InvalidationPolicyEvent.Read)) || | ||
if ((!this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Write) && | ||
!this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Read)) || | ||
!this.isOperatingOnRootData()) { | ||
@@ -154,3 +154,3 @@ return writeResult; | ||
} | ||
if (this.invalidationPolicyManager.isPolicyActive(types_1.InvalidationPolicyEvent.Evict)) { | ||
if (this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Evict)) { | ||
const { typename } = (_a = this.entityTypeMap.readEntityById(helpers_2.makeEntityId(id, fieldName))) !== null && _a !== void 0 ? _a : {}; | ||
@@ -218,11 +218,40 @@ if (typename) { | ||
} | ||
// 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() { | ||
return this._expire(false); | ||
} | ||
// Returns all expired entities still present in the cache. | ||
expiredEntities() { | ||
return this._expire(true); | ||
} | ||
// Activates the provided policy events (on read, on write, on evict) or by default all policy events. | ||
activatePolicyEvents(...policyEvents) { | ||
if (policyEvents.length > 0) { | ||
this.invalidationPolicyManager.activatePolicies(...policyEvents); | ||
} | ||
else { | ||
this.invalidationPolicyManager.activatePolicies(types_1.InvalidationPolicyEvent.Read, types_1.InvalidationPolicyEvent.Write, types_1.InvalidationPolicyEvent.Evict); | ||
} | ||
} | ||
// Deactivates the provided policy events (on read, on write, on evict) or by default all policy events. | ||
deactivatePolicyEvents(...policyEvents) { | ||
if (policyEvents.length > 0) { | ||
this.invalidationPolicyManager.deactivatePolicies(...policyEvents); | ||
} | ||
else { | ||
this.invalidationPolicyManager.deactivatePolicies(types_1.InvalidationPolicyEvent.Read, types_1.InvalidationPolicyEvent.Write, types_1.InvalidationPolicyEvent.Evict); | ||
} | ||
} | ||
// Returns the policy events that are currently active. | ||
activePolicyEvents() { | ||
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.isPolicyActive(types_1.InvalidationPolicyEvent.Read)) { | ||
if (!this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Read)) { | ||
return result; | ||
@@ -246,3 +275,3 @@ } | ||
// the scope of broadcasts. | ||
if (!this.invalidationPolicyManager.isPolicyActive(types_1.InvalidationPolicyEvent.Read) || | ||
if (!this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Read) || | ||
this.isBroadcasting) { | ||
@@ -249,0 +278,0 @@ return cacheDiff; |
@@ -9,6 +9,6 @@ import { InvalidationPolicyEvent, InvalidationPolicyManagerConfig, PolicyActionMeta } from "./types"; | ||
private mutedCacheOperations; | ||
private policyActivation; | ||
private activePolicyEvents; | ||
private policyActionStorage; | ||
constructor(config: InvalidationPolicyManagerConfig); | ||
private activatePolicies; | ||
private activateInitialPolicyEvents; | ||
private getPolicy; | ||
@@ -28,4 +28,6 @@ private getPolicyActionStorage; | ||
}): boolean; | ||
isPolicyActive(policyEvent: InvalidationPolicyEvent): boolean; | ||
activatePolicies(...policyEvents: InvalidationPolicyEvent[]): void; | ||
deactivatePolicies(...policyEvents: InvalidationPolicyEvent[]): void; | ||
isPolicyEventActive(policyEvent: InvalidationPolicyEvent): boolean; | ||
} | ||
//# sourceMappingURL=InvalidationPolicyManager.d.ts.map |
@@ -37,5 +37,5 @@ "use strict"; | ||
}; | ||
this.policyActivation = this.activatePolicies(); | ||
this.activePolicyEvents = this.activateInitialPolicyEvents(); | ||
} | ||
activatePolicies() { | ||
activateInitialPolicyEvents() { | ||
const { policies } = this.config; | ||
@@ -157,7 +157,13 @@ const { types: policyTypes = {}, timeToLive: defaultTimeToLive } = policies; | ||
} | ||
isPolicyActive(policyEvent) { | ||
return this.policyActivation[policyEvent]; | ||
activatePolicies(...policyEvents) { | ||
policyEvents.forEach(policyEvent => this.activePolicyEvents[policyEvent] = true); | ||
} | ||
deactivatePolicies(...policyEvents) { | ||
policyEvents.forEach(policyEvent => this.activePolicyEvents[policyEvent] = false); | ||
} | ||
isPolicyEventActive(policyEvent) { | ||
return this.activePolicyEvents[policyEvent]; | ||
} | ||
} | ||
exports.default = InvalidationPolicyManager; | ||
//# sourceMappingURL=InvalidationPolicyManager.js.map |
@@ -66,5 +66,5 @@ import { Cache, Reference, StoreObject, StoreValue } from "@apollo/client"; | ||
} | ||
export declare type InvalidationPolicyActivation = { | ||
export declare type InvalidationPolicyEventActivation = { | ||
[key in InvalidationPolicyEvent]: boolean; | ||
}; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "apollo-invalidation-policies", | ||
"version": "1.0.0-beta11", | ||
"version": "1.0.0-beta12", | ||
"description": "An extension to the InMemoryCache from Apollo for type-based invalidation policies.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -60,6 +60,9 @@  | ||
| Extended cache API | Description | Return Type | | ||
| -------------------| -------------------------------------------------------------------------------------------|---------------------------------------------------------------| | ||
| `expire` | Evicts all expired entities from the cache based on their type's or the global timeToLive. | String[] - List of expired entity IDs evicted from the cache. | | ||
| `expiredEntities` | Returns all expired entities still present in the cache | String[] - List of expired entities in the cache. | | ||
| Extended cache API | Description | Return Type | Arguments | | ||
| -------------------------| ------------------------------------------------------------------------------------------|--------------------------------------------------------------|------------------------------| | ||
| `expire` | Evicts all expired entities from the cache based on their type's or the global timeToLive | String[] - List of expired entity IDs evicted from the cache | N/A | | ||
| `expiredEntities` | Returns all expired entities still present in the cache | String[] - List of expired entities in the cache | N/A | | ||
| `activePolicyEvents` | Returns all active policy events (Read, Write, Evict) | InvalidationPolicyEvent[] - List of active policy events | N/A | | ||
| `activatePolicyEvents` | Activates the provided policy events, defaults to all | void | ...InvalidationPolicyEvent[] | | ||
| `deactivatePolicyEvents` | Dectivates the provided policy events, defaults to all | void | ...InvalidationPolicyEvent[] | | ||
@@ -66,0 +69,0 @@ | Policy Action Entity | Description | Type | Example | |
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
570759
0.82%1650
2.55%298
1.02%