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