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

Implement useReactiveVar hook for consuming reactive variables in React components. #6867

Merged
merged 6 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
- Prevent full reobservation of queries affected by optimistic mutation updates, while still delivering results from the cache. <br/>
[@benjamn](https://github.com/benjamn) in [#6854](https://github.com/apollographql/apollo-client/pull/6854)

- Implement `useReactiveVar` hook for consuming reactive variables in React components. <br/>
[@benjamn](https://github.com/benjamn) in [#6867](https://github.com/apollographql/apollo-client/pull/6867)

## Apollo Client 3.1.3

## Bug Fixes
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Array [
"useLazyQuery",
"useMutation",
"useQuery",
"useReactiveVar",
"useSubscription",
]
`;
Expand Down Expand Up @@ -216,6 +217,7 @@ Array [
"useLazyQuery",
"useMutation",
"useQuery",
"useReactiveVar",
"useSubscription",
]
`;
Expand Down Expand Up @@ -262,6 +264,7 @@ Array [
"useLazyQuery",
"useMutation",
"useQuery",
"useReactiveVar",
"useSubscription",
]
`;
Expand Down
35 changes: 27 additions & 8 deletions src/cache/inmemory/reactiveVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { dep } from "optimism";
import { InMemoryCache } from "./inMemoryCache";
import { ApolloCache } from '../../core';

export type ReactiveVar<T> = (newValue?: T) => T;
export interface ReactiveVar<T> {
(newValue?: T): T;
onNextChange(listener: ReactiveListener<T>): () => void;
}

export type ReactiveListener<T> = (value: T) => any;

const varDep = dep<ReactiveVar<any>>();

Expand All @@ -12,34 +17,48 @@ const varDep = dep<ReactiveVar<any>>();
export const cacheSlot = new Slot<ApolloCache<any>>();

export function makeVar<T>(value: T): ReactiveVar<T> {
const caches = new Set<ApolloCache<any>>();
const listeners = new Set<ReactiveListener<T>>();

return function rv(newValue) {
const rv: ReactiveVar<T> = function (newValue) {
if (arguments.length > 0) {
if (value !== newValue) {
value = newValue!;
varDep.dirty(rv);
// Trigger broadcast for any caches that were previously involved
// in reading this variable.
caches.forEach(broadcast);
listeners.forEach(listener => {
// Listener functions listen only for the next update, not all
// future updates.
listeners.delete(listener);
benjamn marked this conversation as resolved.
Show resolved Hide resolved
listener(value);
});
}
} else {
// When reading from the variable, obtain the current cache from
// context via cacheSlot. This isn't entirely foolproof, but it's
// the same system that powers varDep.
const cache = cacheSlot.getValue();
if (cache) caches.add(cache);
if (cache && (cache as any).broadcastWatches) {
listeners.add(() => broadcast(cache));
benjamn marked this conversation as resolved.
Show resolved Hide resolved
}
varDep(rv);
}

return value;
};

rv.onNextChange = listener => {
listeners.add(listener);
return () => {
listeners.delete(listener);
benjamn marked this conversation as resolved.
Show resolved Hide resolved
};
};

return rv;
}

type Broadcastable = ApolloCache<any> & {
// This method is protected in InMemoryCache, which we are ignoring, but
// we still want some semblance of type safety when we call it.
broadcastWatches: InMemoryCache["broadcastWatches"];
broadcastWatches?: InMemoryCache["broadcastWatches"];
};

function broadcast(cache: Broadcastable) {
Expand Down
183 changes: 183 additions & 0 deletions src/react/hooks/__tests__/useReactiveVar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import React from "react";
import { render, wait, act } from "@testing-library/react";

import { itAsync } from "../../../testing";
import { makeVar } from "../../../core";
import { useReactiveVar } from "../useReactiveVar";

describe("useReactiveVar Hook", () => {
itAsync("works with one component", (resolve, reject) => {
const counterVar = makeVar(0);
let renderCount = 0;

function Component() {
const count = useReactiveVar(counterVar);

switch (++renderCount) {
case 1:
expect(count).toBe(0);
act(() => {
counterVar(count + 1);
});
break;
case 2:
expect(count).toBe(1);
act(() => {
counterVar(counterVar() + 2);
});
break;
case 3:
expect(count).toBe(3);
break;
default:
reject(`too many (${renderCount}) renders`);
}

return null;
}

render(<Component/>);

return wait(() => {
expect(renderCount).toBe(3);
expect(counterVar()).toBe(3);
}).then(resolve, reject);
});

itAsync("works when two components share a variable", async (resolve, reject) => {
const counterVar = makeVar(0);

let parentRenderCount = 0;
function Parent() {
const count = useReactiveVar(counterVar);

switch (++parentRenderCount) {
case 1:
expect(count).toBe(0);
break;
case 2:
expect(count).toBe(1);
break;
case 3:
expect(count).toBe(11);
break;
default:
reject(`too many (${parentRenderCount}) parent renders`);
}

return <Child/>;
}

let childRenderCount = 0;
function Child() {
const count = useReactiveVar(counterVar);

switch (++childRenderCount) {
case 1:
expect(count).toBe(0);
break;
case 2:
expect(count).toBe(1);
break;
case 3:
expect(count).toBe(11);
break;
default:
reject(`too many (${childRenderCount}) child renders`);
}

return null;
}

render(<Parent/>);

await wait(() => {
expect(parentRenderCount).toBe(1);
expect(childRenderCount).toBe(1);
});

expect(counterVar()).toBe(0);
act(() => {
counterVar(1);
});

await wait(() => {
expect(parentRenderCount).toBe(2);
expect(childRenderCount).toBe(2);
});

expect(counterVar()).toBe(1);
act(() => {
counterVar(counterVar() + 10);
});

await wait(() => {
expect(parentRenderCount).toBe(3);
expect(childRenderCount).toBe(3);
});

expect(counterVar()).toBe(11);

resolve();
});

itAsync("does not update if component has been unmounted", (resolve, reject) => {
const counterVar = makeVar(0);
let renderCount = 0;
let attemptedUpdateAfterUnmount = false;

function Component() {
const count = useReactiveVar(counterVar);

switch (++renderCount) {
case 1:
expect(count).toBe(0);
act(() => {
counterVar(count + 1);
});
break;
case 2:
expect(count).toBe(1);
act(() => {
counterVar(counterVar() + 2);
});
break;
case 3:
expect(count).toBe(3);
setTimeout(() => {
unmount();
setTimeout(() => {
counterVar(counterVar() * 2);
attemptedUpdateAfterUnmount = true;
}, 10);
}, 10);
break;
default:
reject(`too many (${renderCount}) renders`);
}

return null;
}

// To detect updates of unmounted components, we have to monkey-patch
// the console.error method.
const consoleErrorArgs: any[][] = [];
const { error } = console;
console.error = function (...args: any[]) {
consoleErrorArgs.push(args);
return error.apply(this, args);
};

const { unmount } = render(<Component/>);

return wait(() => {
expect(attemptedUpdateAfterUnmount).toBe(true);
}).then(() => {
expect(renderCount).toBe(3);
expect(counterVar()).toBe(6);
expect(consoleErrorArgs).toEqual([]);
}).finally(() => {
console.error = error;
}).then(resolve, reject);
});
});
1 change: 1 addition & 0 deletions src/react/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './useLazyQuery';
export * from './useMutation';
export * from './useQuery';
export * from './useSubscription';
export * from './useReactiveVar';
15 changes: 15 additions & 0 deletions src/react/hooks/useReactiveVar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useState, useEffect } from 'react';
import { ReactiveVar } from '../../core';

export function useReactiveVar<T>(rv: ReactiveVar<T>): T {
const value = rv();
// We don't actually care what useState thinks the value of the variable
// is, so we take only the update function from the returned array.
const mute = rv.onNextChange(useState(value)[1]);
// Once the component is unmounted, ignore future updates. Note that the
// useEffect function returns the mute function without calling it,
// allowing it to be called when the component unmounts. This is
// equivalent to useEffect(() => () => mute(), []), but shorter.
useEffect(() => mute, []);
return value;
}