add and run prettier
This commit is contained in:
@ -1,19 +1,29 @@
|
||||
import eventBus from '../utils/broadcast-channel-pubsub'
|
||||
import eventBus from "../utils/broadcast-channel-pubsub";
|
||||
|
||||
export default (name, getObjKey) => {
|
||||
let cache = {};
|
||||
|
||||
// update data cached on another node
|
||||
eventBus.on('cache-key-set-' + name, ({key, value}, isLocal) => !isLocal ? set(key, value, false) : null);
|
||||
eventBus.on('cache-all-set' + name, ({data}, isLocal) => !isLocal ? setAll(data, false) : null);
|
||||
eventBus.on('cache-merge-' + name, ({data}, isLocal) => !isLocal ? merge(data, false) : null);
|
||||
eventBus.on('cache-key-forget-' + name, ({key}, isLocal) => !isLocal ? forget(key, false) : null);
|
||||
eventBus.on('cache-flush-' + name, (_, isLocal) => !isLocal ? flush(false) : null);
|
||||
eventBus.on("cache-key-set-" + name, ({ key, value }, isLocal) =>
|
||||
!isLocal ? set(key, value, false) : null,
|
||||
);
|
||||
eventBus.on("cache-all-set" + name, ({ data }, isLocal) =>
|
||||
!isLocal ? setAll(data, false) : null,
|
||||
);
|
||||
eventBus.on("cache-merge-" + name, ({ data }, isLocal) =>
|
||||
!isLocal ? merge(data, false) : null,
|
||||
);
|
||||
eventBus.on("cache-key-forget-" + name, ({ key }, isLocal) =>
|
||||
!isLocal ? forget(key, false) : null,
|
||||
);
|
||||
eventBus.on("cache-flush-" + name, (_, isLocal) =>
|
||||
!isLocal ? flush(false) : null,
|
||||
);
|
||||
|
||||
const set = (key, value, emitEvent = true) => {
|
||||
cache[key] = value;
|
||||
|
||||
if (emitEvent) eventBus.publish('cache-key-set-' + name, {key, value});
|
||||
if (emitEvent) eventBus.publish("cache-key-set-" + name, { key, value });
|
||||
|
||||
return value;
|
||||
};
|
||||
@ -21,17 +31,17 @@ export default (name, getObjKey) => {
|
||||
const setAll = (data, emitEvent = true) => {
|
||||
cache = data;
|
||||
|
||||
if (emitEvent) eventBus.publish('cache-all-set-' + name, {data});
|
||||
if (emitEvent) eventBus.publish("cache-all-set-" + name, { data });
|
||||
|
||||
return cache;
|
||||
}
|
||||
};
|
||||
const merge = (data, emitEvent = true) => {
|
||||
cache = {...cache, ...data}
|
||||
cache = { ...cache, ...data };
|
||||
|
||||
if (emitEvent) eventBus.publish('cache-merge-' + name, {data});
|
||||
if (emitEvent) eventBus.publish("cache-merge-" + name, { data });
|
||||
|
||||
return cache;
|
||||
}
|
||||
};
|
||||
|
||||
const get = async (key, fetchFunc) => {
|
||||
if (cache.hasOwnProperty(key)) return cache[key];
|
||||
@ -53,42 +63,43 @@ export default (name, getObjKey) => {
|
||||
const key = getObjKey(value);
|
||||
|
||||
return set(key, value);
|
||||
}
|
||||
};
|
||||
|
||||
const getAll = () => cache;
|
||||
|
||||
const has = key => cache[key] !== undefined;
|
||||
const has = (key) => cache[key] !== undefined;
|
||||
|
||||
const getKeys = () => Object.keys(cache);
|
||||
|
||||
const forget = (key, emitEvent = true) => {
|
||||
delete cache[key];
|
||||
|
||||
if (emitEvent) eventBus.publish('cache-key-forget-' + name, {key});
|
||||
if (emitEvent) eventBus.publish("cache-key-forget-" + name, { key });
|
||||
|
||||
return cache;
|
||||
}
|
||||
};
|
||||
|
||||
const forgetByFilter = (filterFunc, emitEvent = true) => {
|
||||
if (!filterFunc) return false;
|
||||
|
||||
Object.keys(cache).filter(key => filterFunc(cache[key]))
|
||||
.forEach(key => {
|
||||
delete cache[key]
|
||||
Object.keys(cache)
|
||||
.filter((key) => filterFunc(cache[key]))
|
||||
.forEach((key) => {
|
||||
delete cache[key];
|
||||
|
||||
if (emitEvent) eventBus.publish('cache-key-forget-' + name, {key});
|
||||
if (emitEvent) eventBus.publish("cache-key-forget-" + name, { key });
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
const flush = (emitEvent = true) => {
|
||||
cache = {};
|
||||
|
||||
if (emitEvent) eventBus.publish('cache-flush-' + name, {});
|
||||
if (emitEvent) eventBus.publish("cache-flush-" + name, {});
|
||||
|
||||
return cache;
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
has,
|
||||
@ -102,5 +113,5 @@ export default (name, getObjKey) => {
|
||||
forget,
|
||||
forgetByFilter,
|
||||
flush,
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
Reference in New Issue
Block a user