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.

useSet.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = exports.ObservableSet = 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 ObservableSet =
  15. /*#__PURE__*/
  16. function (_Set) {
  17. _inheritsLoose(ObservableSet, _Set);
  18. function ObservableSet(listener, init) {
  19. var _this;
  20. _this = _Set.call(this, init) || this;
  21. _this.listener = listener;
  22. return _this;
  23. }
  24. var _proto = ObservableSet.prototype;
  25. _proto.add = function add(value) {
  26. _Set.prototype.add.call(this, value); // When initializing the Set, the base Set calls this.add() 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(value) {
  32. var result = _Set.prototype.delete.call(this, value);
  33. this.listener(this);
  34. return result;
  35. };
  36. _proto.clear = function clear() {
  37. _Set.prototype.clear.call(this);
  38. this.listener(this);
  39. };
  40. return ObservableSet;
  41. }(_wrapNativeSuper(Set));
  42. /**
  43. * Create and return a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) that triggers rerenders when it's updated.
  44. *
  45. * ```ts
  46. * const ids = useSet<number>([1,2,3,4]);
  47. *
  48. * return (
  49. * <>
  50. * {Array.from(ids, id => (
  51. * <div>
  52. * id: {id}. <button onClick={() => ids.delete(id)}>X</button>
  53. * </div>
  54. * )}
  55. * </>
  56. * )
  57. * ```
  58. *
  59. * @param init initial Set values
  60. */
  61. exports.ObservableSet = ObservableSet;
  62. function useSet(init) {
  63. var forceUpdate = (0, _useForceUpdate.default)();
  64. return (0, _useStableMemo.default)(function () {
  65. return new ObservableSet(forceUpdate, init);
  66. }, []);
  67. }
  68. var _default = useSet;
  69. exports.default = _default;