Skip to content

Commit

Permalink
memoise wrapper values
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Feb 28, 2018
1 parent a62eea8 commit 911d297
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/type/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ declare class GraphQLList<+T: GraphQLType> {
// Note: constructors cannot be used for covariant types. Drop the "new".
constructor(ofType: any): void;
}
const listCache = new WeakMap();
// eslint-disable-next-line no-redeclare
export function GraphQLList(ofType) {
if (this instanceof GraphQLList) {
this.ofType = assertType(ofType);
} else {
return new GraphQLList(ofType);
const cachedValue = listCache.get(ofType);
if (cachedValue !== undefined) {
return cachedValue;
}
const newValue = new GraphQLList(ofType);
listCache.set(ofType, newValue);
return newValue;
}
}

Expand Down Expand Up @@ -75,12 +82,19 @@ declare class GraphQLNonNull<+T: GraphQLNullableType> {
// Note: constructors cannot be used for covariant types. Drop the "new".
constructor(ofType: any): void;
}
const nnCache = new WeakMap();
// eslint-disable-next-line no-redeclare
export function GraphQLNonNull(ofType) {
if (this instanceof GraphQLNonNull) {
this.ofType = assertNullableType(ofType);
} else {
return new GraphQLNonNull(ofType);
const cachedValue = nnCache.get(ofType);
if (cachedValue !== undefined) {
return cachedValue;
}
const newValue = new GraphQLNonNull(ofType);
nnCache.set(ofType, newValue);
return newValue;
}
}

Expand Down

0 comments on commit 911d297

Please sign in to comment.