Cache.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
  2. import _createClass from "@babel/runtime/helpers/esm/createClass";
  3. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  4. // [times, realValue]
  5. var SPLIT = '%';
  6. /** Connect key with `SPLIT` */
  7. export function pathKey(keys) {
  8. return keys.join(SPLIT);
  9. }
  10. var Entity = /*#__PURE__*/function () {
  11. function Entity(instanceId) {
  12. _classCallCheck(this, Entity);
  13. _defineProperty(this, "instanceId", void 0);
  14. /** @private Internal cache map. Do not access this directly */
  15. _defineProperty(this, "cache", new Map());
  16. _defineProperty(this, "extracted", new Set());
  17. this.instanceId = instanceId;
  18. }
  19. _createClass(Entity, [{
  20. key: "get",
  21. value: function get(keys) {
  22. return this.opGet(pathKey(keys));
  23. }
  24. /** A fast get cache with `get` concat. */
  25. }, {
  26. key: "opGet",
  27. value: function opGet(keyPathStr) {
  28. return this.cache.get(keyPathStr) || null;
  29. }
  30. }, {
  31. key: "update",
  32. value: function update(keys, valueFn) {
  33. return this.opUpdate(pathKey(keys), valueFn);
  34. }
  35. /** A fast get cache with `get` concat. */
  36. }, {
  37. key: "opUpdate",
  38. value: function opUpdate(keyPathStr, valueFn) {
  39. var prevValue = this.cache.get(keyPathStr);
  40. var nextValue = valueFn(prevValue);
  41. if (nextValue === null) {
  42. this.cache.delete(keyPathStr);
  43. } else {
  44. this.cache.set(keyPathStr, nextValue);
  45. }
  46. }
  47. }]);
  48. return Entity;
  49. }();
  50. export default Entity;