Skip to content

Commit

Permalink
Update optimism to use lighter-weight dependency API in EntityCache.
Browse files Browse the repository at this point in the history
Background explanation: benjamn/optimism#50
  • Loading branch information
benjamn committed Sep 17, 2019
1 parent 6500d50 commit c98cdcb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 32 deletions.
16 changes: 4 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@wry/equality": "^0.1.9",
"apollo-link": "^1.2.13",
"fast-json-stable-stringify": "^2.0.0",
"optimism": "^0.10.2",
"optimism": "^0.11.1",
"symbol-observable": "^1.2.0",
"ts-invariant": "^0.4.4",
"tslib": "^1.10.0",
Expand Down
23 changes: 4 additions & 19 deletions src/cache/inmemory/entityCache.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NormalizedCache, NormalizedCacheObject, StoreObject } from './types';
import { wrap, OptimisticWrapperFunction } from 'optimism';
import { dep, OptimisticDependencyFunction } from 'optimism';
import { isReference } from './helpers';

const hasOwn = Object.prototype.hasOwnProperty;

type DependType = OptimisticWrapperFunction<[string], StoreObject> | null;
type DependType = OptimisticDependencyFunction<string> | null;

export abstract class EntityCache implements NormalizedCache {
protected data: NormalizedCacheObject = Object.create(null);
Expand All @@ -16,21 +16,6 @@ export abstract class EntityCache implements NormalizedCache {
// instance, because it might be a Root instance (and vice-versa).
public readonly depend: DependType = null;

protected makeDepend(): DependType {
// It's important for this.depend to return a real value instead of
// void, because this.depend(dataId) after this.depend.dirty(dataId)
// marks the ID as clean if the result has not changed since the last
// time the parent computation called this.depend(dataId). Returning
// the StoreObject ensures the ID stays dirty unless its StoreObject
// value is actually === the same as before.
return wrap((dataId: string) => this.data[dataId], {
disposable: true,
makeCacheKey(dataId) {
return dataId;
},
});
}

public abstract addLayer(
layerId: string,
replay: (layer: EntityCache) => any,
Expand Down Expand Up @@ -187,8 +172,8 @@ export namespace EntityCache {
super();
if (resultCaching) {
// Regard this.depend as publicly readonly but privately mutable.
(this as any).depend = this.makeDepend();
this.sharedLayerDepend = this.makeDepend();
(this as any).depend = dep<string>();
this.sharedLayerDepend = dep<string>();
}
if (seed) this.replace(seed);
}
Expand Down

0 comments on commit c98cdcb

Please sign in to comment.