Skip to content

Commit

Permalink
Move shouldNotifyIfLoading logic into QueryInfo#shouldNotify.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Mar 11, 2020
1 parent 85ad03c commit 8bda464
Showing 1 changed file with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,53 @@ export class QueryInfo {
}

notify() {
if (this.dirty &&
this.listeners.size &&
this.observableQuery?.options.fetchPolicy !== "standby") {
if (this.shouldNotify()) {
this.listeners.forEach(listener => listener(this));
this.dirty = false;
}
}

private shouldNotify() {
if (!this.dirty || !this.listeners.size) {
return false;
}

if (!this.observableQuery) {
return true;
}

const {
fetchPolicy,
returnPartialData,
notifyOnNetworkStatusChange,
} = this.observableQuery.options;

if (fetchPolicy === "standby") {
return false;
}

if (isNetworkRequestInFlight(this.networkStatus)) {
const lastResult = this.observableQuery.getLastResult();

const networkStatusChanged = !!(
lastResult &&
lastResult.networkStatus !== this.networkStatus
);

const shouldNotifyIfLoading =
returnPartialData ||
(!this.newData && this.networkStatus === NetworkStatus.setVariables) ||
(networkStatusChanged && notifyOnNetworkStatusChange) ||
fetchPolicy === 'cache-only' ||
fetchPolicy === 'cache-and-network';

if (!shouldNotifyIfLoading) {
return false;
}
}

return true;
}
}

export type QueryStoreValue = Pick<QueryInfo,
Expand Down Expand Up @@ -719,29 +759,11 @@ export class QueryManager<TStore> {
fetchPolicy,
errorPolicy = 'none',
returnPartialData,
notifyOnNetworkStatusChange,
partialRefetch,
} = observableQuery.options;

const loading = isNetworkRequestInFlight(networkStatus);
const lastResult = observableQuery.getLastResult();

const networkStatusChanged = !!(
lastResult &&
lastResult.networkStatus !== networkStatus
);

const shouldNotifyIfLoading =
returnPartialData ||
(!newData && networkStatus === NetworkStatus.setVariables) ||
(networkStatusChanged && notifyOnNetworkStatusChange) ||
fetchPolicy === 'cache-only' ||
fetchPolicy === 'cache-and-network';

if (loading && !shouldNotifyIfLoading) {
return;
}

const hasGraphQLErrors = isNonEmptyArray(graphQLErrors);

// If we have either a GraphQL error or a network error, we create
Expand Down

0 comments on commit 8bda464

Please sign in to comment.