react-query
Version:
Hooks for managing, caching and syncing asynchronous and remote data in React
80 lines (67 loc) • 3.17 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
import { QueryObserver } from './queryObserver';
import { hasNextPage, hasPreviousPage, infiniteQueryBehavior } from './infiniteQueryBehavior';
export var InfiniteQueryObserver = /*#__PURE__*/function (_QueryObserver) {
_inheritsLoose(InfiniteQueryObserver, _QueryObserver);
// Type override
// Type override
// Type override
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
function InfiniteQueryObserver(client, options) {
return _QueryObserver.call(this, client, options) || this;
}
var _proto = InfiniteQueryObserver.prototype;
_proto.bindMethods = function bindMethods() {
_QueryObserver.prototype.bindMethods.call(this);
this.fetchNextPage = this.fetchNextPage.bind(this);
this.fetchPreviousPage = this.fetchPreviousPage.bind(this);
};
_proto.setOptions = function setOptions(options) {
_QueryObserver.prototype.setOptions.call(this, _extends({}, options, {
behavior: infiniteQueryBehavior()
}));
};
_proto.getOptimisticResult = function getOptimisticResult(options) {
options.behavior = infiniteQueryBehavior();
return _QueryObserver.prototype.getOptimisticResult.call(this, options);
};
_proto.fetchNextPage = function fetchNextPage(options) {
return this.fetch({
cancelRefetch: true,
throwOnError: options == null ? void 0 : options.throwOnError,
meta: {
fetchMore: {
direction: 'forward',
pageParam: options == null ? void 0 : options.pageParam
}
}
});
};
_proto.fetchPreviousPage = function fetchPreviousPage(options) {
return this.fetch({
cancelRefetch: true,
throwOnError: options == null ? void 0 : options.throwOnError,
meta: {
fetchMore: {
direction: 'backward',
pageParam: options == null ? void 0 : options.pageParam
}
}
});
};
_proto.createResult = function createResult(query, options) {
var _state$data, _state$data2, _state$fetchMeta, _state$fetchMeta$fetc, _state$fetchMeta2, _state$fetchMeta2$fet;
var state = query.state;
var result = _QueryObserver.prototype.createResult.call(this, query, options);
return _extends({}, 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.isFetching && ((_state$fetchMeta = state.fetchMeta) == null ? void 0 : (_state$fetchMeta$fetc = _state$fetchMeta.fetchMore) == null ? void 0 : _state$fetchMeta$fetc.direction) === 'forward',
isFetchingPreviousPage: state.isFetching && ((_state$fetchMeta2 = state.fetchMeta) == null ? void 0 : (_state$fetchMeta2$fet = _state$fetchMeta2.fetchMore) == null ? void 0 : _state$fetchMeta2$fet.direction) === 'backward'
});
};
return InfiniteQueryObserver;
}(QueryObserver);