synapse-storage
Version:
Набор инструментов для управления состоянием и апи-запросами
33 lines (26 loc) • 1.24 kB
JavaScript
import { VALUE_NOT_CHANGED } from "../utils/middleware-module.js";
import { shallowEqual } from "../utils/state-diff.util.js";
const syncShallowCompareMiddleware = (options = {})=>{
const { comparator = shallowEqual, segments = [] } = options;
const valueCache = new Map();
return {
name: 'sync-shallow-compare',
setup: ()=>{},
reducer: (_api)=>(next)=>(action)=>{
if (action.type !== 'set' || segments.length && !segments.includes(action.metadata?.segment ?? 'default')) {
return next(action);
}
const cacheKey = action.key;
const prevValue = valueCache.get(cacheKey);
const nextValue = action.value;
if (prevValue !== undefined && comparator(prevValue, nextValue)) {
return VALUE_NOT_CHANGED;
}
const result = next(action);
valueCache.set(cacheKey, result !== undefined ? result : nextValue);
return result;
}
};
};
export { syncShallowCompareMiddleware };
//# sourceMappingURL=sync-storage-shallow-compare.middleware.js.map