Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

101 строка
4.1 KiB

  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = exports.ObservableMap = void 0;
  4. var _useForceUpdate = _interopRequireDefault(require("./useForceUpdate"));
  5. var _useStableMemo = _interopRequireDefault(require("./useStableMemo"));
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  7. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  8. function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
  9. function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  10. function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
  11. function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
  12. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  13. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  14. var ObservableMap =
  15. /*#__PURE__*/
  16. function (_Map) {
  17. _inheritsLoose(ObservableMap, _Map);
  18. function ObservableMap(listener, init) {
  19. var _this;
  20. _this = _Map.call(this, init) || this;
  21. _this.listener = listener;
  22. return _this;
  23. }
  24. var _proto = ObservableMap.prototype;
  25. _proto.set = function set(key, value) {
  26. _Map.prototype.set.call(this, key, value); // When initializing the Map, the base Map calls this.set() before the
  27. // listener is assigned so it will be undefined
  28. if (this.listener) this.listener(this);
  29. return this;
  30. };
  31. _proto.delete = function _delete(key) {
  32. var result = _Map.prototype.delete.call(this, key);
  33. this.listener(this);
  34. return result;
  35. };
  36. _proto.clear = function clear() {
  37. _Map.prototype.clear.call(this);
  38. this.listener(this);
  39. };
  40. return ObservableMap;
  41. }(
  42. /*#__PURE__*/
  43. _wrapNativeSuper(Map));
  44. /**
  45. * Create and return a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) that triggers rerenders when it's updated.
  46. *
  47. * ```tsx
  48. * const customerAges = useMap<number>([
  49. * ['john', 24],
  50. * ['betsy', 25]
  51. * ]);
  52. *
  53. * return (
  54. * <>
  55. * {Array.from(ids, ([name, age]) => (
  56. * <div>
  57. * {name}: {age}. <button onClick={() => ids.delete(name)}>X</button>
  58. * </div>
  59. * )}
  60. * </>
  61. * )
  62. * ```
  63. *
  64. * @param init initial Map entries
  65. */
  66. exports.ObservableMap = ObservableMap;
  67. function useMap(init) {
  68. var forceUpdate = (0, _useForceUpdate.default)();
  69. return (0, _useStableMemo.default)(function () {
  70. return new ObservableMap(forceUpdate, init);
  71. }, []);
  72. }
  73. var _default = useMap;
  74. exports.default = _default;