chem-rx 0.0.23 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/README.md +145 -33
- package/dist/Atom.d.ts +88 -48
- package/dist/Atom.d.ts.map +1 -1
- package/dist/Signal.d.ts +4 -5
- package/dist/Signal.d.ts.map +1 -1
- package/dist/index.cjs.js +423 -295
- package/dist/index.d.ts +1 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.iife.js +425 -294
- package/dist/index.mjs +423 -0
- package/dist/react.cjs.js +50 -0
- package/{src/index.ts → dist/react.d.ts} +1 -3
- package/dist/react.d.ts.map +1 -0
- package/dist/react.mjs +40 -0
- package/dist/store.d.ts +30 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/useAtom.d.ts +2 -2
- package/dist/useAtom.d.ts.map +1 -1
- package/dist/useHydrateAtoms.d.ts.map +1 -1
- package/dist/useSelectAtom.d.ts.map +1 -1
- package/package.json +34 -16
- package/.size-snapshot.json +0 -26
- package/babel.config.js +0 -24
- package/dist/index.js +0 -301
- package/dist/types.d.ts +0 -32
- package/dist/types.d.ts.map +0 -1
- package/rollup.config.js +0 -92
- package/src/Atom.ts +0 -374
- package/src/Signal.ts +0 -38
- package/src/types.ts +0 -66
- package/src/useAtom.ts +0 -20
- package/src/useHydrateAtoms.ts +0 -16
- package/src/useSelectAtom.ts +0 -25
- package/src/useSignal.ts +0 -24
- package/tests/atom.test.ts +0 -625
- package/tests/sample.ts +0 -123
- package/tsconfig.json +0 -23
package/dist/index.iife.js
CHANGED
|
@@ -1,341 +1,468 @@
|
|
|
1
|
-
var chemicalRx = (function (exports
|
|
1
|
+
var chemicalRx = (function (exports) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
+
function _arrayLikeToArray(r, a) {
|
|
5
|
+
(null == a || a > r.length) && (a = r.length);
|
|
6
|
+
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
7
|
+
return n;
|
|
8
|
+
}
|
|
9
|
+
function _assertThisInitialized(e) {
|
|
10
|
+
if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
11
|
+
return e;
|
|
12
|
+
}
|
|
13
|
+
function _createForOfIteratorHelperLoose(r, e) {
|
|
14
|
+
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
15
|
+
if (t) return (t = t.call(r)).next.bind(t);
|
|
16
|
+
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
|
|
17
|
+
t && (r = t);
|
|
18
|
+
var o = 0;
|
|
19
|
+
return function () {
|
|
20
|
+
return o >= r.length ? {
|
|
21
|
+
done: !0
|
|
22
|
+
} : {
|
|
23
|
+
done: !1,
|
|
24
|
+
value: r[o++]
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
29
|
+
}
|
|
4
30
|
function _extends() {
|
|
5
|
-
_extends = Object.assign ? Object.assign.bind() : function (
|
|
6
|
-
for (var
|
|
7
|
-
var
|
|
8
|
-
for (var
|
|
9
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
10
|
-
target[key] = source[key];
|
|
11
|
-
}
|
|
12
|
-
}
|
|
31
|
+
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
|
32
|
+
for (var e = 1; e < arguments.length; e++) {
|
|
33
|
+
var t = arguments[e];
|
|
34
|
+
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
|
13
35
|
}
|
|
14
|
-
return
|
|
15
|
-
};
|
|
16
|
-
|
|
36
|
+
return n;
|
|
37
|
+
}, _extends.apply(null, arguments);
|
|
38
|
+
}
|
|
39
|
+
function _inheritsLoose(t, o) {
|
|
40
|
+
t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o);
|
|
41
|
+
}
|
|
42
|
+
function _setPrototypeOf(t, e) {
|
|
43
|
+
return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
|
|
44
|
+
return t.__proto__ = e, t;
|
|
45
|
+
}, _setPrototypeOf(t, e);
|
|
17
46
|
}
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
47
|
+
function _unsupportedIterableToArray(r, a) {
|
|
48
|
+
if (r) {
|
|
49
|
+
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
50
|
+
var t = {}.toString.call(r).slice(8, -1);
|
|
51
|
+
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
52
|
+
}
|
|
22
53
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
54
|
+
|
|
55
|
+
var noop = function noop() {};
|
|
56
|
+
function createSubscription(_unsubscribe) {
|
|
57
|
+
var closed = false;
|
|
58
|
+
return {
|
|
59
|
+
unsubscribe: function unsubscribe() {
|
|
60
|
+
if (closed) return;
|
|
61
|
+
closed = true;
|
|
62
|
+
_unsubscribe();
|
|
63
|
+
}
|
|
27
64
|
};
|
|
28
|
-
return _setPrototypeOf(o, p);
|
|
29
65
|
}
|
|
30
|
-
function
|
|
31
|
-
if (
|
|
32
|
-
|
|
66
|
+
function normalizeSubscription(subscription) {
|
|
67
|
+
if (typeof subscription === "function") {
|
|
68
|
+
return createSubscription(subscription);
|
|
69
|
+
}
|
|
70
|
+
if (subscription && typeof subscription.unsubscribe === "function") {
|
|
71
|
+
return createSubscription(function () {
|
|
72
|
+
return subscription.unsubscribe();
|
|
73
|
+
});
|
|
33
74
|
}
|
|
34
|
-
return
|
|
75
|
+
return createSubscription(noop);
|
|
35
76
|
}
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
39
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
40
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
41
|
-
if (n === "Map" || n === "Set") return Array.from(o);
|
|
42
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
77
|
+
function isAtomSource(value) {
|
|
78
|
+
return value != null && typeof value === "object" && typeof value.subscribe === "function";
|
|
43
79
|
}
|
|
44
|
-
function
|
|
45
|
-
if (
|
|
46
|
-
|
|
47
|
-
|
|
80
|
+
function readAtomSourceValue(source) {
|
|
81
|
+
if (typeof source.getValue === "function") {
|
|
82
|
+
return source.getValue();
|
|
83
|
+
}
|
|
84
|
+
if (typeof source.value === "function") {
|
|
85
|
+
return source.value();
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
48
88
|
}
|
|
49
|
-
function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (i >= o.length) return {
|
|
57
|
-
done: true
|
|
58
|
-
};
|
|
59
|
-
return {
|
|
60
|
-
done: false,
|
|
61
|
-
value: o[i++]
|
|
62
|
-
};
|
|
89
|
+
function toListener(observer) {
|
|
90
|
+
if (typeof observer === "function") {
|
|
91
|
+
return observer;
|
|
92
|
+
}
|
|
93
|
+
if (observer && typeof observer.next === "function") {
|
|
94
|
+
return function (value) {
|
|
95
|
+
return observer.next == null ? void 0 : observer.next(value);
|
|
63
96
|
};
|
|
64
97
|
}
|
|
65
|
-
|
|
98
|
+
return noop;
|
|
66
99
|
}
|
|
100
|
+
function notifyListeners(listeners, value) {
|
|
101
|
+
var errors = [];
|
|
102
|
+
for (var _i = 0, _Array$from = Array.from(listeners); _i < _Array$from.length; _i++) {
|
|
103
|
+
var listener = _Array$from[_i];
|
|
104
|
+
try {
|
|
105
|
+
listener(value);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
errors.push(error);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (errors.length === 1) {
|
|
111
|
+
throw errors[0];
|
|
112
|
+
}
|
|
113
|
+
if (errors.length > 1) {
|
|
114
|
+
throw new AggregateError(errors, "Multiple atom subscribers failed");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
var ValueStore = /*#__PURE__*/function () {
|
|
118
|
+
function ValueStore(value) {
|
|
119
|
+
this._listeners = [];
|
|
120
|
+
this._value = value;
|
|
121
|
+
}
|
|
122
|
+
var _proto = ValueStore.prototype;
|
|
123
|
+
_proto.getValue = function getValue() {
|
|
124
|
+
return this._value;
|
|
125
|
+
};
|
|
126
|
+
_proto.setValue = function setValue(value) {
|
|
127
|
+
this._value = value;
|
|
128
|
+
};
|
|
129
|
+
_proto.next = function next(value) {
|
|
130
|
+
this._value = value;
|
|
131
|
+
notifyListeners(this._listeners, value);
|
|
132
|
+
};
|
|
133
|
+
_proto.subscribe = function subscribe(observer, options) {
|
|
134
|
+
var _this = this;
|
|
135
|
+
if (options === void 0) {
|
|
136
|
+
options = {};
|
|
137
|
+
}
|
|
138
|
+
var listener = toListener(observer);
|
|
139
|
+
this._listeners.push(listener);
|
|
140
|
+
if (options.emitImmediately !== false) {
|
|
141
|
+
listener(this._value);
|
|
142
|
+
}
|
|
143
|
+
return createSubscription(function () {
|
|
144
|
+
var index = _this._listeners.indexOf(listener);
|
|
145
|
+
if (index >= 0) {
|
|
146
|
+
_this._listeners.splice(index, 1);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
return ValueStore;
|
|
151
|
+
}();
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Comparator used to decide whether an atom's value has changed.
|
|
155
|
+
*
|
|
156
|
+
* An atom notifies subscribers only when `equals(previous, next)` returns
|
|
157
|
+
* `false`. The default for every atom is {@link Object.is} (value equality for
|
|
158
|
+
* primitives, reference identity for objects/arrays). Supply a content
|
|
159
|
+
* comparator to dedup by data, or `() => false` to force every update to emit.
|
|
160
|
+
*/
|
|
67
161
|
|
|
68
162
|
var ReadOnlyAtom = /*#__PURE__*/function () {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
163
|
+
/**
|
|
164
|
+
* @param options.equals Comparator deciding when the value has changed.
|
|
165
|
+
* Defaults to {@link Object.is}. Subscribers are notified only when it
|
|
166
|
+
* returns `false`.
|
|
167
|
+
*/
|
|
168
|
+
function ReadOnlyAtom(_value, dependency, options) {
|
|
169
|
+
var _options$equals,
|
|
170
|
+
_this = this;
|
|
171
|
+
this._subscriptions = [];
|
|
172
|
+
this._dependency = null;
|
|
173
|
+
this._dependencySubscription = null;
|
|
174
|
+
this._subscriberCount = 0;
|
|
175
|
+
this._equals = (_options$equals = options == null ? void 0 : options.equals) != null ? _options$equals : Object.is;
|
|
176
|
+
if (dependency) {
|
|
177
|
+
this._dependency = dependency;
|
|
178
|
+
this._store = new ValueStore(_value);
|
|
179
|
+
} else if (isAtomSource(_value)) {
|
|
180
|
+
this._store = new ValueStore(readAtomSourceValue(_value));
|
|
181
|
+
this._addSubscription(_value.subscribe(function (value) {
|
|
182
|
+
_this._next(value);
|
|
183
|
+
}));
|
|
80
184
|
} else {
|
|
81
|
-
|
|
82
|
-
this._behavior$ = new rxjs.BehaviorSubject(_value);
|
|
185
|
+
this._store = new ValueStore(_value);
|
|
83
186
|
}
|
|
84
187
|
}
|
|
85
188
|
|
|
86
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Create a read-only atom whose value is derived from this atom.
|
|
191
|
+
*
|
|
192
|
+
* By default the derived atom notifies subscribers only when its computed
|
|
193
|
+
* output differs by {@link Object.is}. Pass `options.equals` to dedup by
|
|
194
|
+
* content (useful when `deriveFn` returns a fresh object/array each time), or
|
|
195
|
+
* `equals: () => false` to re-emit on every parent update.
|
|
196
|
+
*/
|
|
87
197
|
var _proto = ReadOnlyAtom.prototype;
|
|
88
|
-
_proto.
|
|
89
|
-
var
|
|
90
|
-
|
|
91
|
-
var
|
|
92
|
-
var
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
198
|
+
_proto.derive = function derive(deriveFn, options) {
|
|
199
|
+
var _this2 = this;
|
|
200
|
+
var index = 0;
|
|
201
|
+
var hasCachedInput = false;
|
|
202
|
+
var cachedInput;
|
|
203
|
+
var cachedValue;
|
|
204
|
+
var getSnapshot = function getSnapshot(force) {
|
|
205
|
+
if (force === void 0) {
|
|
206
|
+
force = false;
|
|
207
|
+
}
|
|
208
|
+
var input = _this2.value();
|
|
209
|
+
if (force || !hasCachedInput || !Object.is(cachedInput, input)) {
|
|
210
|
+
cachedInput = input;
|
|
211
|
+
cachedValue = deriveFn(input, index++);
|
|
212
|
+
hasCachedInput = true;
|
|
213
|
+
}
|
|
214
|
+
return cachedValue;
|
|
215
|
+
};
|
|
216
|
+
return new ReadOnlyAtom(getSnapshot(), {
|
|
217
|
+
getSnapshot: getSnapshot,
|
|
218
|
+
subscribe: function subscribe(onDependencyChange) {
|
|
219
|
+
return _this2._subscribe(onDependencyChange, {
|
|
220
|
+
emitImmediately: false
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}, options);
|
|
97
224
|
};
|
|
98
|
-
_proto.subscribe = function subscribe() {
|
|
99
|
-
|
|
100
|
-
return (_this$_behavior$2 = this._behavior$).subscribe.apply(_this$_behavior$2, arguments);
|
|
225
|
+
_proto.subscribe = function subscribe(observer) {
|
|
226
|
+
return this._subscribe(observer);
|
|
101
227
|
};
|
|
102
228
|
_proto.value = function value() {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
;
|
|
229
|
+
if (this._dependency) {
|
|
230
|
+
this._refresh(false);
|
|
231
|
+
}
|
|
232
|
+
return this._store.getValue();
|
|
233
|
+
};
|
|
108
234
|
_proto.dispose = function dispose() {
|
|
109
|
-
var _this$
|
|
110
|
-
(_this$
|
|
235
|
+
var _this$_dependencySubs;
|
|
236
|
+
(_this$_dependencySubs = this._dependencySubscription) == null || _this$_dependencySubs.unsubscribe();
|
|
237
|
+
this._dependencySubscription = null;
|
|
238
|
+
this._subscriberCount = 0;
|
|
239
|
+
for (var _iterator = _createForOfIteratorHelperLoose(this._subscriptions.splice(0)), _step; !(_step = _iterator()).done;) {
|
|
240
|
+
var subscription = _step.value;
|
|
241
|
+
subscription.unsubscribe();
|
|
242
|
+
}
|
|
111
243
|
};
|
|
112
244
|
_proto.get = function get(key) {
|
|
113
245
|
var val = this.value();
|
|
114
246
|
return val == null ? void 0 : val[key];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Create a read-only atom that tracks `key` on this atom's value.
|
|
251
|
+
*
|
|
252
|
+
* By default the selected atom notifies subscribers only when the selected
|
|
253
|
+
* value differs by {@link Object.is}. Pass `options.equals` to dedup by
|
|
254
|
+
* content instead.
|
|
255
|
+
*/;
|
|
256
|
+
_proto.select = function select(key, options) {
|
|
257
|
+
var _this3 = this;
|
|
258
|
+
var getSnapshot = function getSnapshot() {
|
|
259
|
+
var value = _this3.value();
|
|
260
|
+
return value == null ? void 0 : value[key];
|
|
261
|
+
};
|
|
262
|
+
return new ReadOnlyAtom(getSnapshot(), {
|
|
263
|
+
getSnapshot: getSnapshot,
|
|
264
|
+
subscribe: function subscribe(onDependencyChange) {
|
|
265
|
+
return _this3._subscribe(onDependencyChange, {
|
|
266
|
+
emitImmediately: false
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}, options);
|
|
115
270
|
};
|
|
116
|
-
_proto.
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return
|
|
122
|
-
}
|
|
123
|
-
|
|
271
|
+
_proto._refresh = function _refresh(emit, force) {
|
|
272
|
+
if (force === void 0) {
|
|
273
|
+
force = false;
|
|
274
|
+
}
|
|
275
|
+
if (!this._dependency) {
|
|
276
|
+
return this._store.getValue();
|
|
277
|
+
}
|
|
278
|
+
var previousValue = this._store.getValue();
|
|
279
|
+
var nextValue = this._dependency.getSnapshot(force);
|
|
280
|
+
var shouldNotify = emit && !this._equals(previousValue, nextValue);
|
|
281
|
+
if (shouldNotify) {
|
|
282
|
+
this._store.next(nextValue);
|
|
283
|
+
} else {
|
|
284
|
+
this._store.setValue(nextValue);
|
|
285
|
+
}
|
|
286
|
+
return nextValue;
|
|
287
|
+
};
|
|
288
|
+
_proto._retainDependency = function _retainDependency() {
|
|
289
|
+
var _this4 = this;
|
|
290
|
+
if (!this._dependency) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
this._subscriberCount += 1;
|
|
294
|
+
if (this._subscriberCount === 1) {
|
|
295
|
+
this._dependencySubscription = normalizeSubscription(this._dependency.subscribe(function () {
|
|
296
|
+
_this4._refresh(true, true);
|
|
297
|
+
}));
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
_proto._releaseDependency = function _releaseDependency() {
|
|
301
|
+
if (!this._dependency || this._subscriberCount === 0) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
this._subscriberCount -= 1;
|
|
305
|
+
if (this._subscriberCount === 0) {
|
|
306
|
+
var _this$_dependencySubs2;
|
|
307
|
+
(_this$_dependencySubs2 = this._dependencySubscription) == null || _this$_dependencySubs2.unsubscribe();
|
|
308
|
+
this._dependencySubscription = null;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
124
311
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
312
|
+
/** @internal */;
|
|
313
|
+
_proto._next = function _next(value) {
|
|
314
|
+
var previousValue = this._store.getValue();
|
|
315
|
+
if (this._equals(previousValue, value)) {
|
|
316
|
+
this._store.setValue(value);
|
|
317
|
+
} else {
|
|
318
|
+
this._store.next(value);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** @internal */;
|
|
323
|
+
_proto._subscribe = function _subscribe(observer, options) {
|
|
324
|
+
var _this5 = this;
|
|
325
|
+
if (!this._dependency) {
|
|
326
|
+
return this._store.subscribe(observer, options);
|
|
327
|
+
}
|
|
328
|
+
this._refresh(false);
|
|
329
|
+
var subscription;
|
|
330
|
+
try {
|
|
331
|
+
this._retainDependency();
|
|
332
|
+
subscription = this._store.subscribe(observer, options);
|
|
333
|
+
} catch (error) {
|
|
334
|
+
this._releaseDependency();
|
|
335
|
+
throw error;
|
|
336
|
+
}
|
|
337
|
+
return createSubscription(function () {
|
|
338
|
+
subscription.unsubscribe();
|
|
339
|
+
_this5._releaseDependency();
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** @internal */;
|
|
344
|
+
_proto._addSubscription = function _addSubscription(subscription) {
|
|
345
|
+
var normalized = normalizeSubscription(subscription);
|
|
346
|
+
this._subscriptions.push(normalized);
|
|
347
|
+
return normalized;
|
|
130
348
|
};
|
|
131
349
|
return ReadOnlyAtom;
|
|
132
350
|
}();
|
|
133
|
-
var BaseAtom = /*#__PURE__*/function (
|
|
134
|
-
_inheritsLoose(BaseAtom, _ReadOnlyAtom);
|
|
351
|
+
var BaseAtom = /*#__PURE__*/function (_ReadOnlyAtom2) {
|
|
135
352
|
function BaseAtom() {
|
|
136
|
-
return
|
|
353
|
+
return _ReadOnlyAtom2.apply(this, arguments) || this;
|
|
137
354
|
}
|
|
355
|
+
_inheritsLoose(BaseAtom, _ReadOnlyAtom2);
|
|
138
356
|
var _proto2 = BaseAtom.prototype;
|
|
139
357
|
_proto2.next = function next(nextVal) {
|
|
140
|
-
this.
|
|
358
|
+
this._next(nextVal);
|
|
141
359
|
};
|
|
142
360
|
_proto2.set = function set(nextKey, nextValue) {
|
|
143
361
|
var _extends2;
|
|
144
|
-
|
|
362
|
+
var currentValue = this.value();
|
|
363
|
+
if (currentValue == null || typeof currentValue !== "object") {
|
|
364
|
+
throw new TypeError("Atom.set can only be used with object values");
|
|
365
|
+
}
|
|
366
|
+
this._next(_extends({}, currentValue, (_extends2 = {}, _extends2[nextKey] = nextValue, _extends2)));
|
|
145
367
|
};
|
|
146
368
|
return BaseAtom;
|
|
147
369
|
}(ReadOnlyAtom);
|
|
148
370
|
var NullableBaseAtom = /*#__PURE__*/function (_BaseAtom) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
var _this2;
|
|
152
|
-
if (_value != null) {
|
|
153
|
-
_this2 = _BaseAtom.call(this, _value) || this;
|
|
154
|
-
} else {
|
|
155
|
-
// @ts-ignore
|
|
156
|
-
_this2 = _BaseAtom.call(this, undefined) || this;
|
|
157
|
-
}
|
|
158
|
-
return _assertThisInitialized(_this2);
|
|
371
|
+
function NullableBaseAtom(_value, options) {
|
|
372
|
+
return _BaseAtom.call(this, _value, undefined, options) || this;
|
|
159
373
|
}
|
|
160
|
-
|
|
161
|
-
// @ts-ignore
|
|
374
|
+
_inheritsLoose(NullableBaseAtom, _BaseAtom);
|
|
162
375
|
var _proto3 = NullableBaseAtom.prototype;
|
|
163
|
-
_proto3.get = function get(key) {
|
|
164
|
-
return _BaseAtom.prototype.get.call(this, key);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// @ts-ignore
|
|
168
|
-
;
|
|
169
|
-
_proto3.select = function select(key) {
|
|
170
|
-
// @ts-ignore
|
|
171
|
-
return _BaseAtom.prototype.select.call(this, key);
|
|
172
|
-
};
|
|
173
376
|
_proto3.next = function next(nextVal) {
|
|
174
|
-
|
|
175
|
-
_BaseAtom.prototype.next.call(this, nextVal);
|
|
377
|
+
this._next(nextVal);
|
|
176
378
|
};
|
|
177
379
|
_proto3.reset = function reset() {
|
|
178
|
-
|
|
179
|
-
_BaseAtom.prototype.next.call(this, undefined);
|
|
380
|
+
this._next(undefined);
|
|
180
381
|
};
|
|
181
382
|
return NullableBaseAtom;
|
|
182
383
|
}(BaseAtom);
|
|
183
384
|
var ArrayAtom = /*#__PURE__*/function (_BaseAtom2) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
_this3 = _BaseAtom2.call(this, initialValue) || this;
|
|
385
|
+
function ArrayAtom(initialValue, options) {
|
|
386
|
+
var _this6;
|
|
387
|
+
if (initialValue === undefined) {
|
|
388
|
+
_this6 = _BaseAtom2.call(this, [], undefined, options) || this;
|
|
189
389
|
} else {
|
|
190
|
-
|
|
390
|
+
_this6 = _BaseAtom2.call(this, initialValue, undefined, options) || this;
|
|
191
391
|
}
|
|
192
|
-
return _assertThisInitialized(
|
|
392
|
+
return _assertThisInitialized(_this6);
|
|
193
393
|
}
|
|
394
|
+
_inheritsLoose(ArrayAtom, _BaseAtom2);
|
|
194
395
|
var _proto4 = ArrayAtom.prototype;
|
|
195
396
|
_proto4.push = function push(nextVal) {
|
|
196
|
-
this.
|
|
397
|
+
this._next([].concat(this.value(), [nextVal]));
|
|
197
398
|
};
|
|
198
399
|
return ArrayAtom;
|
|
199
400
|
}(BaseAtom);
|
|
200
401
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
// K extends string | number | symbol = keyof T,
|
|
207
|
-
// V = T[K]
|
|
208
|
-
// > extends ReadOnlyAtom<T> {
|
|
209
|
-
// get(nextKey: K) {
|
|
210
|
-
// return this.value()[nextKey];
|
|
211
|
-
// }
|
|
212
|
-
//
|
|
213
|
-
// select<K extends keyof T>(
|
|
214
|
-
// key: K
|
|
215
|
-
// ): T[K] extends (infer W)[]
|
|
216
|
-
// ? ArrayAtom<W>
|
|
217
|
-
// : T[K] extends { [key in infer L]?: infer W }
|
|
218
|
-
// ? ObjectAtom<T[K]>
|
|
219
|
-
// : BaseAtom<T> {
|
|
220
|
-
// const newObs = this._behavior$.pipe(
|
|
221
|
-
// distinctUntilKeyChanged(key),
|
|
222
|
-
// map((k) => k?.[key])
|
|
223
|
-
// );
|
|
224
|
-
// // Can't get typescript to recognize the types here so I'm cheating
|
|
225
|
-
// return Atom(newObs) as unknown as T[K] extends (infer W)[]
|
|
226
|
-
// ? ArrayAtom<W>
|
|
227
|
-
// : T[K] extends { [key in infer L]?: infer W }
|
|
228
|
-
// ? ObjectAtom<T[K]>
|
|
229
|
-
// : BaseAtom<T>;
|
|
230
|
-
// }
|
|
231
|
-
// }
|
|
232
|
-
//
|
|
233
|
-
// export class ObjectAtom<
|
|
234
|
-
// T extends
|
|
235
|
-
// | {
|
|
236
|
-
// [key in K]: V;
|
|
237
|
-
// }
|
|
238
|
-
// | null,
|
|
239
|
-
// K extends string | number | symbol = keyof T,
|
|
240
|
-
// V = T extends { [key in K]: infer W } ? T[K] : null
|
|
241
|
-
// > extends ReadOnlyAtom<T> {
|
|
242
|
-
// set(nextKey: K, nextValue: V) {
|
|
243
|
-
// this._behavior$.next({
|
|
244
|
-
// ...this._behavior$.getValue(),
|
|
245
|
-
// [nextKey]: nextValue,
|
|
246
|
-
// });
|
|
247
|
-
// }
|
|
248
|
-
// }
|
|
402
|
+
/**
|
|
403
|
+
* Options for the {@link Atom} factory. `readOnly` produces a
|
|
404
|
+
* {@link ReadOnlyAtom}; `equals` sets the change comparator (defaults to
|
|
405
|
+
* {@link Object.is}).
|
|
406
|
+
*/
|
|
249
407
|
|
|
250
|
-
//
|
|
251
|
-
// export type AnyAtom<T> = BaseAtom<T> | ArrayAtom<T> | ObjectAtom<T>;
|
|
408
|
+
// Legacy positional `readOnly` form.
|
|
252
409
|
|
|
253
|
-
//
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
function Atom(_value, readOnly) {
|
|
266
|
-
if (readOnly === void 0) {
|
|
267
|
-
readOnly = false;
|
|
268
|
-
}
|
|
269
|
-
var atom;
|
|
410
|
+
// Options-object form.
|
|
411
|
+
|
|
412
|
+
function Atom(_value, optionsOrReadOnly) {
|
|
413
|
+
var factoryOptions = typeof optionsOrReadOnly === "boolean" ? {
|
|
414
|
+
readOnly: optionsOrReadOnly
|
|
415
|
+
} : optionsOrReadOnly != null ? optionsOrReadOnly : {};
|
|
416
|
+
var _factoryOptions$readO = factoryOptions.readOnly,
|
|
417
|
+
readOnly = _factoryOptions$readO === void 0 ? false : _factoryOptions$readO,
|
|
418
|
+
equals = factoryOptions.equals;
|
|
419
|
+
var atomOptions = equals ? {
|
|
420
|
+
equals: equals
|
|
421
|
+
} : undefined;
|
|
270
422
|
if (readOnly) {
|
|
271
|
-
|
|
272
|
-
} else if (Array.isArray(_value)) {
|
|
273
|
-
atom = new ArrayAtom(_value); // For arrays
|
|
274
|
-
} else if (_value == null) {
|
|
275
|
-
atom = new NullableBaseAtom();
|
|
276
|
-
} else {
|
|
277
|
-
atom = new BaseAtom(_value); // For other types
|
|
423
|
+
return new ReadOnlyAtom(_value, undefined, atomOptions);
|
|
278
424
|
}
|
|
279
|
-
|
|
280
|
-
|
|
425
|
+
if (isAtomSource(_value)) {
|
|
426
|
+
var sourceValue = readAtomSourceValue(_value);
|
|
427
|
+
if (Array.isArray(sourceValue)) {
|
|
428
|
+
return new ArrayAtom(_value, atomOptions);
|
|
429
|
+
}
|
|
430
|
+
return new BaseAtom(_value, undefined, atomOptions);
|
|
431
|
+
}
|
|
432
|
+
if (Array.isArray(_value)) {
|
|
433
|
+
return new ArrayAtom(_value, atomOptions);
|
|
434
|
+
}
|
|
435
|
+
if (_value == null) {
|
|
436
|
+
return new NullableBaseAtom(undefined, atomOptions);
|
|
437
|
+
}
|
|
438
|
+
return new BaseAtom(_value, undefined, atomOptions);
|
|
281
439
|
}
|
|
282
440
|
Atom.combine = function () {
|
|
283
441
|
for (var _len = arguments.length, atoms = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
284
442
|
atoms[_key] = arguments[_key];
|
|
285
443
|
}
|
|
286
|
-
var
|
|
287
|
-
return
|
|
288
|
-
|
|
289
|
-
var newAtom = new ReadOnlyAtom(observable);
|
|
290
|
-
return newAtom;
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
function useAtom(atom) {
|
|
294
|
-
var _useState = react.useState(atom.value()),
|
|
295
|
-
value = _useState[0],
|
|
296
|
-
setValue = _useState[1];
|
|
297
|
-
react.useEffect(function () {
|
|
298
|
-
var subscription = atom.subscribe(function (val) {
|
|
299
|
-
setValue(val);
|
|
444
|
+
var getSnapshot = function getSnapshot() {
|
|
445
|
+
return atoms.map(function (atom) {
|
|
446
|
+
return atom.value();
|
|
300
447
|
});
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
448
|
+
};
|
|
449
|
+
return new ReadOnlyAtom(getSnapshot(), {
|
|
450
|
+
getSnapshot: getSnapshot,
|
|
451
|
+
subscribe: function subscribe(onDependencyChange) {
|
|
452
|
+
var subscriptions = atoms.map(function (atom) {
|
|
453
|
+
return atom._subscribe(onDependencyChange, {
|
|
454
|
+
emitImmediately: false
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
return function () {
|
|
458
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(subscriptions), _step2; !(_step2 = _iterator2()).done;) {
|
|
459
|
+
var subscription = _step2.value;
|
|
460
|
+
subscription.unsubscribe();
|
|
461
|
+
}
|
|
462
|
+
};
|
|
316
463
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
subscription.unsubscribe();
|
|
320
|
-
}
|
|
321
|
-
};
|
|
322
|
-
}, [signal, callback, id]);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
function useSelectAtom(atom, key) {
|
|
326
|
-
var _useState = react.useState(atom.get(key)),
|
|
327
|
-
value = _useState[0],
|
|
328
|
-
setValue = _useState[1];
|
|
329
|
-
react.useEffect(function () {
|
|
330
|
-
var subscription = atom.select(key).subscribe(function (val) {
|
|
331
|
-
setValue(val);
|
|
332
|
-
});
|
|
333
|
-
return function () {
|
|
334
|
-
subscription.unsubscribe();
|
|
335
|
-
};
|
|
336
|
-
}, [atom]);
|
|
337
|
-
return value;
|
|
338
|
-
}
|
|
464
|
+
});
|
|
465
|
+
};
|
|
339
466
|
|
|
340
467
|
var hydratedAtomsSet = new WeakSet();
|
|
341
468
|
function hydrateAtoms(values, options) {
|
|
@@ -345,56 +472,60 @@ var chemicalRx = (function (exports, rxjs, react) {
|
|
|
345
472
|
value = _step$value[1];
|
|
346
473
|
if (!hydratedAtomsSet.has(atom) || options != null && options.force) {
|
|
347
474
|
hydratedAtomsSet.add(atom);
|
|
348
|
-
atom.
|
|
475
|
+
atom.next(value);
|
|
349
476
|
}
|
|
350
477
|
}
|
|
351
478
|
}
|
|
352
479
|
|
|
353
480
|
var Signal = /*#__PURE__*/function () {
|
|
354
481
|
function Signal() {
|
|
355
|
-
this.
|
|
356
|
-
this.
|
|
482
|
+
this._listeners = [];
|
|
483
|
+
this._listenersById = new Map();
|
|
357
484
|
}
|
|
358
485
|
var _proto = Signal.prototype;
|
|
359
486
|
_proto.ping = function ping(value, id) {
|
|
360
|
-
if (id && this.
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
this._subjects.forEach(function (subject) {
|
|
369
|
-
return subject.next(value);
|
|
370
|
-
});
|
|
487
|
+
if (id && this._listenersById.has(id)) {
|
|
488
|
+
notifyListeners(this._listenersById.get(id), value);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
notifyListeners(this._listeners, value);
|
|
492
|
+
for (var _iterator = _createForOfIteratorHelperLoose(this._listenersById.values()), _step; !(_step = _iterator()).done;) {
|
|
493
|
+
var listeners = _step.value;
|
|
494
|
+
notifyListeners(listeners, value);
|
|
371
495
|
}
|
|
372
496
|
};
|
|
373
497
|
_proto.subscribe = function subscribe(callback, id) {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
// No ID provided, subscribe to the default subject.
|
|
382
|
-
return this._defaultSubject.subscribe(callback);
|
|
498
|
+
var _this = this,
|
|
499
|
+
_this$_listenersById$;
|
|
500
|
+
if (!id) {
|
|
501
|
+
this._listeners.push(callback);
|
|
502
|
+
return createSubscription(function () {
|
|
503
|
+
removeListener(_this._listeners, callback);
|
|
504
|
+
});
|
|
383
505
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
506
|
+
var listeners = (_this$_listenersById$ = this._listenersById.get(id)) != null ? _this$_listenersById$ : [];
|
|
507
|
+
listeners.push(callback);
|
|
508
|
+
this._listenersById.set(id, listeners);
|
|
509
|
+
return createSubscription(function () {
|
|
510
|
+
removeListener(listeners, callback);
|
|
511
|
+
if (listeners.length === 0) {
|
|
512
|
+
_this._listenersById.delete(id);
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
};
|
|
388
516
|
return Signal;
|
|
389
517
|
}();
|
|
518
|
+
function removeListener(listeners, callback) {
|
|
519
|
+
var index = listeners.indexOf(callback);
|
|
520
|
+
if (index >= 0) {
|
|
521
|
+
listeners.splice(index, 1);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
390
524
|
|
|
391
525
|
exports.Atom = Atom;
|
|
392
526
|
exports.Signal = Signal;
|
|
393
527
|
exports.hydrateAtoms = hydrateAtoms;
|
|
394
|
-
exports.useAtom = useAtom;
|
|
395
|
-
exports.useSelectAtom = useSelectAtom;
|
|
396
|
-
exports.useSignal = useSignal;
|
|
397
528
|
|
|
398
529
|
return exports;
|
|
399
530
|
|
|
400
|
-
})({}
|
|
531
|
+
})({});
|