UNPKG

@redux-devtools/rtk-query-monitor

Version:
67 lines 2.43 kB
import { QueryStatus } from '@reduxjs/toolkit/query'; export let QueryComparators = /*#__PURE__*/function (QueryComparators) { QueryComparators["fulfilledTimeStamp"] = "timestamp"; QueryComparators["queryKey"] = "key"; QueryComparators["status"] = "status"; QueryComparators["endpointName"] = "endpointName"; QueryComparators["apiReducerPath"] = "apiReducerPath"; return QueryComparators; }({}); export const sortQueryOptions = [{ label: 'last updated', value: QueryComparators.fulfilledTimeStamp }, { label: 'query key', value: QueryComparators.queryKey }, { label: 'status', value: QueryComparators.status }, { label: 'endpoint', value: QueryComparators.endpointName }, { label: 'reducerPath', value: QueryComparators.apiReducerPath }]; function sortQueryByFulfilled(thisQueryInfo, thatQueryInfo) { const thisFulfilled = thisQueryInfo.state.fulfilledTimeStamp ?? -1; const thatFulfilled = thatQueryInfo.state.fulfilledTimeStamp ?? -1; return thisFulfilled - thatFulfilled; } const mapStatusToFactor = { [QueryStatus.uninitialized]: 1, [QueryStatus.pending]: 2, [QueryStatus.rejected]: 3, [QueryStatus.fulfilled]: 4 }; function sortQueryByStatus(thisQueryInfo, thatQueryInfo) { const thisTerm = mapStatusToFactor[thisQueryInfo.state.status] || -1; const thatTerm = mapStatusToFactor[thatQueryInfo.state.status] || -1; return thisTerm - thatTerm; } export function compareJSONPrimitive(a, b) { if (a === b) { return 0; } if (a == null) return -1; if (b == null) return 1; return a > b ? 1 : -1; } function sortByQueryKey(thisQueryInfo, thatQueryInfo) { return compareJSONPrimitive(thisQueryInfo.queryKey, thatQueryInfo.queryKey); } function sortQueryByEndpointName(thisQueryInfo, thatQueryInfo) { const thisEndpointName = thisQueryInfo.state.endpointName ?? ''; const thatEndpointName = thatQueryInfo.state.endpointName ?? ''; return compareJSONPrimitive(thisEndpointName, thatEndpointName); } function sortByApiReducerPath(thisQueryInfo, thatQueryInfo) { return compareJSONPrimitive(thisQueryInfo.reducerPath, thatQueryInfo.reducerPath); } export const queryComparators = { [QueryComparators.fulfilledTimeStamp]: sortQueryByFulfilled, [QueryComparators.status]: sortQueryByStatus, [QueryComparators.endpointName]: sortQueryByEndpointName, [QueryComparators.queryKey]: sortByQueryKey, [QueryComparators.apiReducerPath]: sortByApiReducerPath };