Skip to content

Commit

Permalink
Use private non-method function for reobserveCacheFirst.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Mar 29, 2022
1 parent e5ed91a commit 5036dc5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
77 changes: 39 additions & 38 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
// the cache, we still want fetchMore to deliver its final loading:false
// result with the unchanged data.
if (!updatedQuerySet.has(this.options.query)) {
this.reobserveCacheFirst();
reobserveCacheFirst(this);
}
});
}
Expand Down Expand Up @@ -824,43 +824,6 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
);
}

// Reobserve with fetchPolicy effectively set to "cache-first", triggering
// delivery of any new data from the cache, possibly falling back to the
// network if any cache data are missing. This allows _complete_ cache results
// to be delivered without also kicking off unnecessary network requests when
// this.options.fetchPolicy is "cache-and-network" or "network-only". When
// this.options.fetchPolicy is any other policy ("cache-first", "cache-only",
// "standby", or "no-cache"), we call this.reobserve() as usual.
public reobserveCacheFirst() {
const { fetchPolicy, nextFetchPolicy } = this.options;

if (
fetchPolicy === "cache-and-network" ||
fetchPolicy === "network-only"
) {
return this.reobserve({
fetchPolicy: "cache-first",
// Use a temporary nextFetchPolicy function that replaces itself with
// the previous nextFetchPolicy value and returns the original
// fetchPolicy.
nextFetchPolicy(...args) {
// Replace this nextFetchPolicy function in the options object with
// the original this.options.nextFetchPolicy value.
this.nextFetchPolicy = nextFetchPolicy;
// If the original nextFetchPolicy value was a function, give it a
// chance to decide what happens here.
if (typeof nextFetchPolicy === "function") {
return nextFetchPolicy.apply(this, args);
}
// Otherwise go back to the original this.options.fetchPolicy.
return fetchPolicy!;
},
});
}

return this.reobserve();
}

private reportResult(
result: ApolloQueryResult<TData>,
variables: TVariables | undefined,
Expand Down Expand Up @@ -920,6 +883,44 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
// signature than the Observable constructor.
fixObservableSubclass(ObservableQuery);

// Reobserve with fetchPolicy effectively set to "cache-first", triggering
// delivery of any new data from the cache, possibly falling back to the network
// if any cache data are missing. This allows _complete_ cache results to be
// delivered without also kicking off unnecessary network requests when
// this.options.fetchPolicy is "cache-and-network" or "network-only". When
// this.options.fetchPolicy is any other policy ("cache-first", "cache-only",
// "standby", or "no-cache"), we call this.reobserve() as usual.
export function reobserveCacheFirst<TData, TVars>(
obsQuery: ObservableQuery<TData, TVars>,
) {
const { fetchPolicy, nextFetchPolicy } = obsQuery.options;

if (
fetchPolicy === "cache-and-network" ||
fetchPolicy === "network-only"
) {
return obsQuery.reobserve({
fetchPolicy: "cache-first",
// Use a temporary nextFetchPolicy function that replaces itself with the
// previous nextFetchPolicy value and returns the original fetchPolicy.
nextFetchPolicy(this: WatchQueryOptions<TVars, TData>) {
// Replace this nextFetchPolicy function in the options object with the
// original this.options.nextFetchPolicy value.
this.nextFetchPolicy = nextFetchPolicy;
// If the original nextFetchPolicy value was a function, give it a
// chance to decide what happens here.
if (typeof nextFetchPolicy === "function") {
return nextFetchPolicy.apply(this, arguments);
}
// Otherwise go back to the original this.options.fetchPolicy.
return fetchPolicy!;
},
});
}

return obsQuery.reobserve();
}

function defaultSubscriptionObserverErrorCallback(error: ApolloError) {
invariant.error('Unhandled error', error.message, error.stack);
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/QueryInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { equal } from "@wry/equality";

import { Cache, ApolloCache } from '../cache';
import { WatchQueryOptions, ErrorPolicy } from './watchQueryOptions';
import { ObservableQuery } from './ObservableQuery';
import { ObservableQuery, reobserveCacheFirst } from './ObservableQuery';
import { QueryListener } from './types';
import { FetchResult } from '../link/core';
import {
Expand Down Expand Up @@ -246,7 +246,7 @@ export class QueryInfo {
// this method, and are handled by calling oq.reobserve(). If this
// reobservation is spurious, isDifferentFromLastResult still has a
// chance to catch it before delivery to ObservableQuery subscribers.
oq.reobserveCacheFirst();
reobserveCacheFirst(oq);
}
});
} else {
Expand Down

0 comments on commit 5036dc5

Please sign in to comment.