react-query
Version:
Hooks for managing, caching and syncing asynchronous and remote data in React
74 lines (66 loc) • 2.35 kB
JavaScript
import { QueryObserver } from './queryObserver';
import { hasNextPage, hasPreviousPage, infiniteQueryBehavior } from './infiniteQueryBehavior';
export class InfiniteQueryObserver extends QueryObserver {
// Type override
// Type override
// Type override
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
constructor(client, options) {
super(client, options);
}
bindMethods() {
super.bindMethods();
this.fetchNextPage = this.fetchNextPage.bind(this);
this.fetchPreviousPage = this.fetchPreviousPage.bind(this);
}
setOptions(options, notifyOptions) {
super.setOptions({ ...options,
behavior: infiniteQueryBehavior()
}, notifyOptions);
}
getOptimisticResult(options) {
options.behavior = infiniteQueryBehavior();
return super.getOptimisticResult(options);
}
fetchNextPage({
pageParam,
...options
} = {}) {
return this.fetch({ ...options,
meta: {
fetchMore: {
direction: 'forward',
pageParam
}
}
});
}
fetchPreviousPage({
pageParam,
...options
} = {}) {
return this.fetch({ ...options,
meta: {
fetchMore: {
direction: 'backward',
pageParam
}
}
});
}
createResult(query, options) {
var _state$data, _state$data2, _state$fetchMeta, _state$fetchMeta$fetc, _state$fetchMeta2, _state$fetchMeta2$fet;
const {
state
} = query;
const result = super.createResult(query, options);
return { ...result,
fetchNextPage: this.fetchNextPage,
fetchPreviousPage: this.fetchPreviousPage,
hasNextPage: hasNextPage(options, (_state$data = state.data) == null ? void 0 : _state$data.pages),
hasPreviousPage: hasPreviousPage(options, (_state$data2 = state.data) == null ? void 0 : _state$data2.pages),
isFetchingNextPage: state.fetchStatus === 'fetching' && ((_state$fetchMeta = state.fetchMeta) == null ? void 0 : (_state$fetchMeta$fetc = _state$fetchMeta.fetchMore) == null ? void 0 : _state$fetchMeta$fetc.direction) === 'forward',
isFetchingPreviousPage: state.fetchStatus === 'fetching' && ((_state$fetchMeta2 = state.fetchMeta) == null ? void 0 : (_state$fetchMeta2$fet = _state$fetchMeta2.fetchMore) == null ? void 0 : _state$fetchMeta2$fet.direction) === 'backward'
};
}
}