Cache.js 1.9 KB

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