UNPKG

4.13 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = exports.ObservableSet = void 0;
5
6var _useForceUpdate = _interopRequireDefault(require("./useForceUpdate"));
7
8var _useStableMemo = _interopRequireDefault(require("./useStableMemo"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
13
14function _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); }
15
16function _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); }
17
18function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
19
20function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
21
22function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
23
24function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
25
26var ObservableSet = /*#__PURE__*/function (_Set) {
27 _inheritsLoose(ObservableSet, _Set);
28
29 function ObservableSet(listener, init) {
30 var _this;
31
32 _this = _Set.call(this, init) || this;
33 _this.listener = listener;
34 return _this;
35 }
36
37 var _proto = ObservableSet.prototype;
38
39 _proto.add = function add(value) {
40 _Set.prototype.add.call(this, value); // When initializing the Set, the base Set calls this.add() before the
41 // listener is assigned so it will be undefined
42
43
44 if (this.listener) this.listener(this);
45 return this;
46 };
47
48 _proto.delete = function _delete(value) {
49 var result = _Set.prototype.delete.call(this, value);
50
51 this.listener(this);
52 return result;
53 };
54
55 _proto.clear = function clear() {
56 _Set.prototype.clear.call(this);
57
58 this.listener(this);
59 };
60
61 return ObservableSet;
62}( /*#__PURE__*/_wrapNativeSuper(Set));
63/**
64 * 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.
65 *
66 * ```ts
67 * const ids = useSet<number>([1,2,3,4]);
68 *
69 * return (
70 * <>
71 * {Array.from(ids, id => (
72 * <div>
73 * id: {id}. <button onClick={() => ids.delete(id)}>X</button>
74 * </div>
75 * )}
76 * </>
77 * )
78 * ```
79 *
80 * @param init initial Set values
81 */
82
83
84exports.ObservableSet = ObservableSet;
85
86function useSet(init) {
87 var forceUpdate = (0, _useForceUpdate.default)();
88 return (0, _useStableMemo.default)(function () {
89 return new ObservableSet(forceUpdate, init);
90 }, []);
91}
92
93var _default = useSet;
94exports.default = _default;
\No newline at end of file