Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useMap.js 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. 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); }
  3. 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; } }
  4. 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); }
  5. function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
  6. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  7. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  8. import useForceUpdate from './useForceUpdate';
  9. import useStableMemo from './useStableMemo';
  10. export var ObservableMap =
  11. /*#__PURE__*/
  12. function (_Map) {
  13. _inheritsLoose(ObservableMap, _Map);
  14. function ObservableMap(listener, init) {
  15. var _this;
  16. _this = _Map.call(this, init) || this;
  17. _this.listener = listener;
  18. return _this;
  19. }
  20. var _proto = ObservableMap.prototype;
  21. _proto.set = function set(key, value) {
  22. _Map.prototype.set.call(this, key, value); // When initializing the Map, the base Map calls this.set() before the
  23. // listener is assigned so it will be undefined
  24. if (this.listener) this.listener(this);
  25. return this;
  26. };
  27. _proto.delete = function _delete(key) {
  28. var result = _Map.prototype.delete.call(this, key);
  29. this.listener(this);
  30. return result;
  31. };
  32. _proto.clear = function clear() {
  33. _Map.prototype.clear.call(this);
  34. this.listener(this);
  35. };
  36. return ObservableMap;
  37. }(_wrapNativeSuper(Map));
  38. /**
  39. * 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.
  40. *
  41. * ```tsx
  42. * const customerAges = useMap<number>([
  43. * ['john', 24],
  44. * ['betsy', 25]
  45. * ]);
  46. *
  47. * return (
  48. * <>
  49. * {Array.from(ids, ([name, age]) => (
  50. * <div>
  51. * {name}: {age}. <button onClick={() => ids.delete(name)}>X</button>
  52. * </div>
  53. * )}
  54. * </>
  55. * )
  56. * ```
  57. *
  58. * @param init initial Map entries
  59. */
  60. function useMap(init) {
  61. var forceUpdate = useForceUpdate();
  62. return useStableMemo(function () {
  63. return new ObservableMap(forceUpdate, init);
  64. }, []);
  65. }
  66. export default useMap;