Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

useMap.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. }(_wrapNativeSuper(Map));
  42. /**
  43. * 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.
  44. *
  45. * ```tsx
  46. * const customerAges = useMap<number>([
  47. * ['john', 24],
  48. * ['betsy', 25]
  49. * ]);
  50. *
  51. * return (
  52. * <>
  53. * {Array.from(ids, ([name, age]) => (
  54. * <div>
  55. * {name}: {age}. <button onClick={() => ids.delete(name)}>X</button>
  56. * </div>
  57. * )}
  58. * </>
  59. * )
  60. * ```
  61. *
  62. * @param init initial Map entries
  63. */
  64. exports.ObservableMap = ObservableMap;
  65. function useMap(init) {
  66. var forceUpdate = (0, _useForceUpdate.default)();
  67. return (0, _useStableMemo.default)(function () {
  68. return new ObservableMap(forceUpdate, init);
  69. }, []);
  70. }
  71. var _default = useMap;
  72. exports.default = _default;