Skip to content

Commit

Permalink
Unify EntityStore#{get,getFieldValue} methods. (#5772)
Browse files Browse the repository at this point in the history
When I originally wrote this code, I was motivated to keep the get and
getFieldValue methods separate because they had different return types.
Method signature overloading solves that problem using just the type
system, so we can have one method implementation rather than two.

At the same time, I was able to eliminate unnecessary inheritance and
method overriding in the Layer subclass by using an instanceof check in
EntityStore#get.
  • Loading branch information
benjamn authored Jan 10, 2020
1 parent 4f64734 commit abfef50
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 56 deletions.
71 changes: 19 additions & 52 deletions src/cache/inmemory/entityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,24 @@ export abstract class EntityStore implements NormalizedCache {
return { ...this.data };
}

public has(dataId: string): boolean {
return this.get(dataId) !== void 0;
public has(dataId: string, fieldName?: string): boolean {
return this.get(dataId, fieldName) !== void 0;
}

public get(dataId: string): StoreObject {
this.group.depend(dataId);
return this.data[dataId];
}

public getFieldValue(dataId: string, storeFieldName: string): StoreValue {
this.group.depend(dataId, storeFieldName);
const storeObject = this.data[dataId];
return storeObject && storeObject[storeFieldName];
public get(dataId: string): StoreObject;
public get(dataId: string, fieldName: string): StoreValue;
public get(dataId: string, fieldName?: string): StoreValue {
this.group.depend(dataId, fieldName);
if (hasOwn.call(this.data, dataId)) {
const storeObject = this.data[dataId];
if (!fieldName) return storeObject;
if (hasOwn.call(storeObject, fieldName)) {
return storeObject[fieldName];
}
}
if (this instanceof Layer) {
return this.parent.get(dataId, fieldName);
}
}

public merge(dataId: string, incoming: StoreObject): void {
Expand All @@ -59,8 +64,8 @@ export abstract class EntityStore implements NormalizedCache {
this.data[dataId] = merged;
delete this.refs[dataId];
if (this.group.caching) {
// First, invalidate any dependents that called get rather than
// getFieldValue.
// First, invalidate any dependents that called store.get(id)
// rather than store.get(id, fieldName).
this.group.dirty(dataId);
// Now invalidate dependents who called getFieldValue for any
// fields that are changing as a result of this merge.
Expand Down Expand Up @@ -284,11 +289,7 @@ class CacheGroup {

public dirty(dataId: string, storeFieldName?: string) {
if (this.d) {
this.d.dirty(
typeof storeFieldName === "string"
? makeDepKey(dataId, storeFieldName)
: makeDepKey(dataId),
);
this.d.dirty(makeDepKey(dataId, storeFieldName));
}
}

Expand Down Expand Up @@ -390,40 +391,6 @@ class Layer extends EntityStore {
};
}

public get(dataId: string): StoreObject {
if (hasOwn.call(this.data, dataId)) {
return super.get(dataId);
}

// If this layer has a this.depend function and it's not the one
// this.parent is using, we need to depend on the given dataId using
// this.depend before delegating to the parent. This check saves us
// from calling this.depend for every optimistic layer we examine, but
// ensures we call this.depend in the last optimistic layer before we
// reach the root layer.

if (this.group.caching && this.group !== this.parent.group) {
this.group.depend(dataId);
}

return this.parent.get(dataId);
}

public getFieldValue(dataId: string, storeFieldName: string): StoreValue {
if (hasOwn.call(this.data, dataId)) {
const storeObject = this.data[dataId];
if (storeObject && hasOwn.call(storeObject, storeFieldName)) {
return super.getFieldValue(dataId, storeFieldName);
}
}

if (this.group.caching && this.group !== this.parent.group) {
this.group.depend(dataId, storeFieldName);
}

return this.parent.getFieldValue(dataId, storeFieldName);
}

// Return a Set<string> of all the ID strings that have been retained by this
// Layer *and* any layers/roots beneath it.
public getRootIdSet(): Set<string> {
Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function getTypenameFromStoreObject(
objectOrReference: StoreObject | Reference,
): string | undefined {
return isReference(objectOrReference)
? store.getFieldValue(objectOrReference.__ref, "__typename") as string
? store.get(objectOrReference.__ref, "__typename") as string
: objectOrReference && objectOrReference.__typename;
}

Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export class Policies {
let fieldValue: StoreValue;
if (isReference(objectOrReference)) {
const dataId = objectOrReference.__ref;
fieldValue = store.getFieldValue(dataId, storeFieldName);
fieldValue = store.get(dataId, storeFieldName);
if (fieldValue === void 0 && storeFieldName === "__typename") {
// We can infer the __typename of singleton root objects like
// ROOT_QUERY ("Query") and ROOT_MUTATION ("Mutation"), even if
Expand Down
4 changes: 2 additions & 2 deletions src/cache/inmemory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export declare type IdGetter = (
* StoreObjects from the cache
*/
export interface NormalizedCache {
has(dataId: string): boolean;
has(dataId: string, fieldName?: string): boolean;
get(dataId: string): StoreObject;
getFieldValue(dataId: string, storeFieldName: string): StoreValue;
get(dataId: string, fieldName: string): StoreValue;
merge(dataId: string, incoming: StoreObject): void;
delete(dataId: string, fieldName?: string): void;
clear(): void;
Expand Down

0 comments on commit abfef50

Please sign in to comment.