Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: WeakMap when environments are referenced #3014

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/relay-experimental/FragmentResource.js
Expand Up @@ -42,6 +42,12 @@ type FragmentResourceCache = Cache<
Error | Promise<mixed> | SingularOrPluralSnapshot,
>;

const WEAKMAP_SUPPORTED = typeof WeakMap === 'function';
interface IMap<K, V> {
get(key: K): V | void;
set(key: K, value: V): IMap<K, V>;
}

type SingularOrPluralSnapshot = Snapshot | $ReadOnlyArray<Snapshot>;
opaque type FragmentResult: {data: mixed, ...} = {|
cacheKey: string,
Expand Down Expand Up @@ -437,7 +443,10 @@ function createFragmentResource(environment: IEnvironment): FragmentResource {
return new FragmentResourceImpl(environment);
}

const dataResources: Map<IEnvironment, FragmentResource> = new Map();
const dataResources: IMap<IEnvironment, FragmentResource> = WEAKMAP_SUPPORTED
? new WeakMap()
: new Map();

function getFragmentResourceForEnvironment(
environment: IEnvironment,
): FragmentResourceImpl {
Expand Down
11 changes: 10 additions & 1 deletion packages/relay-experimental/QueryResource.js
Expand Up @@ -66,6 +66,12 @@ opaque type QueryResult: {
operation: OperationDescriptor,
|};

const WEAKMAP_SUPPORTED = typeof WeakMap === 'function';
interface IMap<K, V> {
get(key: K): V | void;
set(key: K, value: V): IMap<K, V>;
}

function getQueryCacheKey(
operation: OperationDescriptor,
fetchPolicy: FetchPolicy,
Expand Down Expand Up @@ -527,7 +533,10 @@ function createQueryResource(environment: IEnvironment): QueryResource {
return new QueryResourceImpl(environment);
}

const dataResources: Map<IEnvironment, QueryResource> = new Map();
const dataResources: IMap<IEnvironment, QueryResource> = WEAKMAP_SUPPORTED
? new WeakMap()
: new Map();

function getQueryResourceForEnvironment(
environment: IEnvironment,
): QueryResourceImpl {
Expand Down
6 changes: 5 additions & 1 deletion packages/relay-runtime/query/fetchQueryInternal.js
Expand Up @@ -34,7 +34,11 @@ type RequestCacheEntry = {|
+subscription: Subscription,
|};

const requestCachesByEnvironment = new Map();
const WEAKMAP_SUPPORTED = typeof WeakMap === 'function';

const requestCachesByEnvironment = WEAKMAP_SUPPORTED
? new WeakMap()
: new Map();

/**
* Fetches the given query and variables on the provided environment,
Expand Down