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

Allow manually associating caches with reactive variables. #7350

Merged
merged 3 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
{
"name": "apollo-client",
"path": "./dist/apollo-client.cjs.min.js",
"maxSize": "25.4 kB"
"maxSize": "25.5 kB"
}
],
"peerDependencies": {
Expand Down
128 changes: 127 additions & 1 deletion src/cache/inmemory/__tests__/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2056,7 +2056,6 @@ describe("InMemoryCache#modify", () => {
cache.modify({
fields: {
comments(comments: Reference[], { readField }) {
debugger;
expect(Object.isFrozen(comments)).toBe(true);
expect(comments.length).toBe(3);
const filtered = comments.filter(comment => {
Expand Down Expand Up @@ -2757,6 +2756,133 @@ describe("ReactiveVar and makeVar", () => {
},
]);
});

it('should broadcast to manually added caches', () => {
const rv = makeVar(0);
const cache = new InMemoryCache;
const query = gql`query { value }`;
const diffs: Cache.DiffResult<any>[] = [];
const watch: Cache.WatchOptions = {
query,
optimistic: true,
callback(diff) {
diffs.push(diff);
},
};

cache.writeQuery({
query,
data: {
value: "oyez",
},
});

const cancel = cache.watch(watch);

// This should not trigger a broadcast, since we haven't associated
// this cache with rv yet.
rv(rv() + 1);
expect(diffs).toEqual([]);

// The rv.attachCache method returns rv, for chaining.
rv.attachCache(cache)(rv() + 1);

expect(diffs).toEqual([
{
complete: true,
result: {
value: "oyez",
},
},
]);

cache.writeQuery({
query,
broadcast: false,
data: {
value: "oyez, oyez",
},
});

expect(diffs).toEqual([
{
complete: true,
result: {
value: "oyez",
},
},
]);

rv(rv() + 1);
expect(diffs).toEqual([
{
complete: true,
result: {
value: "oyez",
},
},
{
complete: true,
result: {
value: "oyez, oyez",
},
},
]);

expect(rv.forgetCache(cache)).toBe(true);

cache.writeQuery({
query,
broadcast: false,
data: {
value: "oyez, oyez, oyez",
},
});

// Since we called rv.forgetCache(cache) above, updating rv here
// should not trigger a broadcast.
rv(rv() + 1);
expect(diffs).toEqual([
{
complete: true,
result: {
value: "oyez",
},
},
{
complete: true,
result: {
value: "oyez, oyez",
},
},
]);

cache["broadcastWatches"]();
expect(diffs).toEqual([
{
complete: true,
result: {
value: "oyez",
},
},
{
complete: true,
result: {
value: "oyez, oyez",
},
},
{
complete: true,
result: {
value: "oyez, oyez, oyez",
},
},
]);

cancel();

expect(rv()).toBe(4);
});
});

describe('TypedDocumentNode<Data, Variables>', () => {
Expand Down
35 changes: 24 additions & 11 deletions src/cache/inmemory/reactiveVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ApolloCache } from '../../core';
export interface ReactiveVar<T> {
(newValue?: T): T;
onNextChange(listener: ReactiveListener<T>): () => void;
attachCache(cache: ApolloCache<any>): this;
forgetCache(cache: ApolloCache<any>): boolean;
}

Expand All @@ -22,10 +23,12 @@ export const cacheSlot = new Slot<ApolloCache<any>>();
// to the original elements of the set before we begin iterating. See
// iterateObserversSafely for another example of this pattern.
function consumeAndIterate<T>(set: Set<T>, callback: (item: T) => any) {
const items: T[] = [];
set.forEach(item => items.push(item));
set.clear();
items.forEach(callback);
if (set.size) {
const items: T[] = [];
set.forEach(item => items.push(item));
set.clear();
items.forEach(callback);
}
}

const varsByCache = new WeakMap<ApolloCache<any>, Set<ReactiveVar<any>>>();
Expand Down Expand Up @@ -61,12 +64,7 @@ export function makeVar<T>(value: T): ReactiveVar<T> {
// 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);
let vars = varsByCache.get(cache)!;
if (!vars) varsByCache.set(cache, vars = new Set);
vars.add(rv);
}
if (cache) attach(cache);
varDep(rv);
}

Expand All @@ -80,7 +78,22 @@ export function makeVar<T>(value: T): ReactiveVar<T> {
};
};

rv.forgetCache = cache => caches.delete(cache);
const attach = rv.attachCache = cache => {
caches.add(cache);
let vars = varsByCache.get(cache)!;
if (!vars) varsByCache.set(cache, vars = new Set);
vars.add(rv);
return rv;
};

rv.forgetCache = cache => {
const deleted = caches.delete(cache);
if (deleted) {
const vars = varsByCache.get(cache);
if (vars) vars.delete(rv);
}
return deleted;
};

return rv;
}
Expand Down