Skip to content

Commit

Permalink
Make ObservableQuery responsible for Reobserver creation logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed May 4, 2020
1 parent dd86a71 commit fefbe5d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 51 deletions.
49 changes: 29 additions & 20 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,12 @@ export class ObservableQuery<
};
}

return this.queryManager
.observeQuery(this, this.observer)
.reobserve({
fetchPolicy,
variables: this.options.variables,
// Always disable polling for refetches.
pollInterval: 0,
}, NetworkStatus.refetch);
return this.newReobserver(false).reobserve({
fetchPolicy,
variables: this.options.variables,
// Always disable polling for refetches.
pollInterval: 0,
}, NetworkStatus.refetch);
}

public fetchMore<K extends keyof TVariables>(
Expand Down Expand Up @@ -564,19 +562,30 @@ export class ObservableQuery<
}

private reobserver?: Reobserver<TData, TVariables>;

private getReobserver(): Reobserver<TData, TVariables> {
return this.reobserver || (
this.reobserver = this.queryManager.observeQuery(
this,
this.observer,
// Passing this.options explicitly here prevents observeQuery from
// making a shallow copy of the options object before passing it
// to the constructor of the returned Reobserver object. In other
// words, this extra argument allows this.reobserver.options to be
// === this.options, so we don't have to worry about synchronizing
// the properties of two distinct objects.
this.options,
)
return this.reobserver || (this.reobserver = this.newReobserver(true));
}

private newReobserver(shareOptions: boolean) {
const { queryManager, queryId } = this;
queryManager.setObservableQuery(this);
return new Reobserver<TData, TVariables>(
this.observer,
// Sharing options allows this.reobserver.options to be ===
// this.options, so we don't have to worry about synchronizing the
// properties of two distinct objects.
shareOptions ? this.options : { ...this.options },
(currentOptions, newNetworkStatus) => {
queryManager.setObservableQuery(this);
return queryManager.fetchQueryObservable(
queryId,
currentOptions,
newNetworkStatus,
);
},
// Avoid polling during SSR and when the query is already in flight.
!queryManager.ssrMode && (() => !queryManager.checkInFlight(queryId)),
);
}

Expand Down
34 changes: 3 additions & 31 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { ApolloError, isApolloError } from '../errors/ApolloError';
import {
ObservableSubscription,
Observable,
Observer,
} from '../utilities/observables/Observable';
import { MutationStore } from '../data/mutations';
import {
Expand Down Expand Up @@ -56,7 +55,6 @@ import { isNonEmptyArray } from '../utilities/common/arrays';
import { ApolloCache } from '../cache/core/cache';

import { QueryInfo, QueryStoreValue } from './QueryInfo';
import { Reobserver } from './Reobserver';

const { hasOwnProperty } = Object.prototype;

Expand Down Expand Up @@ -614,34 +612,8 @@ export class QueryManager<TStore> {
return Promise.all(observableQueryPromises);
}

public observeQuery<TData, TVars>(
observableQuery: ObservableQuery<TData, TVars>,
observer: Observer<ApolloQueryResult<TData>>,
// If no options are passed, make a shallow defensive copy of the
// ObservableQuery options.
options = { ...observableQuery.options },
): Reobserver<TData, TVars> {
const { queryId } = observableQuery;

const setObsQuery = () =>
this.getQuery(queryId).setObservableQuery(observableQuery);

setObsQuery();

return new Reobserver<TData, TVars>(
observer,
options,
(currentOptions, newNetworkStatus) => {
setObsQuery();
return this.fetchQueryObservable(
observableQuery.queryId,
currentOptions,
newNetworkStatus,
);
},
// Avoid polling during SSR and when the query is already in flight.
!this.ssrMode && (() => !this.checkInFlight(queryId)),
);
public setObservableQuery(observableQuery: ObservableQuery<any, any>) {
this.getQuery(observableQuery.queryId).setObservableQuery(observableQuery);
}

public startGraphQLSubscription<T = any>({
Expand Down Expand Up @@ -866,7 +838,7 @@ export class QueryManager<TStore> {
);
}

private fetchQueryObservable<TData, TVars>(
public fetchQueryObservable<TData, TVars>(
queryId: string,
options: WatchQueryOptions<TVars>,
// The initial networkStatus for this fetch, most often
Expand Down

0 comments on commit fefbe5d

Please sign in to comment.