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

useSet.js 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 ObservableSet =
  11. /*#__PURE__*/
  12. function (_Set) {
  13. _inheritsLoose(ObservableSet, _Set);
  14. function ObservableSet(listener, init) {
  15. var _this;
  16. _this = _Set.call(this, init) || this;
  17. _this.listener = listener;
  18. return _this;
  19. }
  20. var _proto = ObservableSet.prototype;
  21. _proto.add = function add(value) {
  22. _Set.prototype.add.call(this, value); // When initializing the Set, the base Set calls this.add() 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(value) {
  28. var result = _Set.prototype.delete.call(this, value);
  29. this.listener(this);
  30. return result;
  31. };
  32. _proto.clear = function clear() {
  33. _Set.prototype.clear.call(this);
  34. this.listener(this);
  35. };
  36. return ObservableSet;
  37. }(_wrapNativeSuper(Set));
  38. /**
  39. * 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.
  40. *
  41. * ```ts
  42. * const ids = useSet<number>([1,2,3,4]);
  43. *
  44. * return (
  45. * <>
  46. * {Array.from(ids, id => (
  47. * <div>
  48. * id: {id}. <button onClick={() => ids.delete(id)}>X</button>
  49. * </div>
  50. * )}
  51. * </>
  52. * )
  53. * ```
  54. *
  55. * @param init initial Set values
  56. */
  57. function useSet(init) {
  58. var forceUpdate = useForceUpdate();
  59. return useStableMemo(function () {
  60. return new ObservableSet(forceUpdate, init);
  61. }, []);
  62. }
  63. export default useSet;