@redux-devtools/rtk-query-monitor
Version:
rtk-query monitor for Redux DevTools
75 lines (74 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.QueryComparators = void 0;
exports.compareJSONPrimitive = compareJSONPrimitive;
exports.sortQueryOptions = exports.queryComparators = void 0;
var _query = require("@reduxjs/toolkit/query");
let QueryComparators = exports.QueryComparators = /*#__PURE__*/function (QueryComparators) {
QueryComparators["fulfilledTimeStamp"] = "timestamp";
QueryComparators["queryKey"] = "key";
QueryComparators["status"] = "status";
QueryComparators["endpointName"] = "endpointName";
QueryComparators["apiReducerPath"] = "apiReducerPath";
return QueryComparators;
}({});
const sortQueryOptions = exports.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 = {
[_query.QueryStatus.uninitialized]: 1,
[_query.QueryStatus.pending]: 2,
[_query.QueryStatus.rejected]: 3,
[_query.QueryStatus.fulfilled]: 4
};
function sortQueryByStatus(thisQueryInfo, thatQueryInfo) {
const thisTerm = mapStatusToFactor[thisQueryInfo.state.status] || -1;
const thatTerm = mapStatusToFactor[thatQueryInfo.state.status] || -1;
return thisTerm - thatTerm;
}
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);
}
const queryComparators = exports.queryComparators = {
[QueryComparators.fulfilledTimeStamp]: sortQueryByFulfilled,
[QueryComparators.status]: sortQueryByStatus,
[QueryComparators.endpointName]: sortQueryByEndpointName,
[QueryComparators.queryKey]: sortByQueryKey,
[QueryComparators.apiReducerPath]: sortByApiReducerPath
};