Cache.js 619 B

12345678910111213141516171819202122
  1. const SPLIT = '%';
  2. class Entity {
  3. constructor(instanceId) {
  4. /** @private Internal cache map. Do not access this directly */
  5. this.cache = new Map();
  6. this.instanceId = instanceId;
  7. }
  8. get(keys) {
  9. return this.cache.get(Array.isArray(keys) ? keys.join(SPLIT) : keys) || null;
  10. }
  11. update(keys, valueFn) {
  12. const path = Array.isArray(keys) ? keys.join(SPLIT) : keys;
  13. const prevValue = this.cache.get(path);
  14. const nextValue = valueFn(prevValue);
  15. if (nextValue === null) {
  16. this.cache.delete(path);
  17. } else {
  18. this.cache.set(path, nextValue);
  19. }
  20. }
  21. }
  22. export default Entity;