@data-client/core
Version:
Async State Management without the Management. REST, GraphQL, SSE, Websockets, Fetch
40 lines (37 loc) • 819 B
text/typescript
import { INVALIDATE } from '../../actionTypes.js';
import type {
State,
InvalidateAllAction,
InvalidateAction,
} from '../../types.js';
export function invalidateReducer(
state: State<unknown>,
action: InvalidateAction | InvalidateAllAction,
) {
const endpoints = { ...state.endpoints };
const meta = { ...state.meta };
const invalidateKey = (key: string) => {
delete endpoints[key];
const itemMeta = {
...meta[key],
expiresAt: 0,
invalidated: true,
};
delete itemMeta.error;
meta[key] = itemMeta;
};
if (action.type === INVALIDATE) {
invalidateKey(action.key);
} else {
Object.keys(endpoints).forEach(key => {
if (action.testKey(key)) {
invalidateKey(key);
}
});
}
return {
...state,
endpoints,
meta,
};
}