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.
@@ -1,341 +1,468 @@
1
- var chemicalRx = (function (exports, rxjs, react) {
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 (target) {
6
- for (var i = 1; i < arguments.length; i++) {
7
- var source = arguments[i];
8
- for (var key in source) {
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 target;
15
- };
16
- return _extends.apply(this, arguments);
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 _inheritsLoose(subClass, superClass) {
19
- subClass.prototype = Object.create(superClass.prototype);
20
- subClass.prototype.constructor = subClass;
21
- _setPrototypeOf(subClass, superClass);
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
- function _setPrototypeOf(o, p) {
24
- _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
25
- o.__proto__ = p;
26
- return o;
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 _assertThisInitialized(self) {
31
- if (self === void 0) {
32
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
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 self;
75
+ return createSubscription(noop);
35
76
  }
36
- function _unsupportedIterableToArray(o, minLen) {
37
- if (!o) return;
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 _arrayLikeToArray(arr, len) {
45
- if (len == null || len > arr.length) len = arr.length;
46
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
47
- return arr2;
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 _createForOfIteratorHelperLoose(o, allowArrayLike) {
50
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
51
- if (it) return (it = it.call(o)).next.bind(it);
52
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
53
- if (it) o = it;
54
- var i = 0;
55
- return function () {
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
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
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
- function ReadOnlyAtom(_value) {
70
- var _this = this;
71
- this._bonds = [];
72
- this._fromObservable = null;
73
- this._fromObservableSubscription = null;
74
- if (rxjs.isObservable(_value)) {
75
- this._fromObservable = _value;
76
- this._behavior$ = new rxjs.BehaviorSubject(null);
77
- this._fromObservableSubscription = _value.subscribe(function (value) {
78
- _this._behavior$.next(value);
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
- // if it's just a value just use a regular behavior subject
82
- this._behavior$ = new rxjs.BehaviorSubject(_value);
185
+ this._store = new ValueStore(_value);
83
186
  }
84
187
  }
85
188
 
86
- // taken from Observable
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.pipe = function pipe() {
89
- var _this$_behavior$;
90
- // @ts-ignore can't match overloaded function
91
- var observable = (_this$_behavior$ = this._behavior$).pipe.apply(_this$_behavior$, arguments);
92
- var newAtom = new ReadOnlyAtom(observable);
93
- return newAtom;
94
- };
95
- _proto.derive = function derive(deriveFn) {
96
- return this.pipe(rxjs.map(deriveFn));
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
- var _this$_behavior$2;
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
- return this._behavior$.getValue();
104
- }
105
-
106
- // not needed?
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$_fromObservable;
110
- (_this$_fromObservable = this._fromObservableSubscription) == null ? void 0 : _this$_fromObservable.unsubscribe();
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.select = function select(key) {
117
- // console.log("key", key);
118
- var newObs = this._behavior$.pipe(rxjs.distinctUntilChanged(function (a, b) {
119
- if (a === b) return true;
120
- if (a && b && a[key] === b[key]) return true;
121
- return false;
122
- }), rxjs.map(function (k) {
123
- // console.log("insideee", k, key);
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
- return k == null ? void 0 : k[key];
126
- }));
127
- // console.log("obs", newObs);
128
- // Can't get typescript to recognize the types here so I'm cheating
129
- return Atom(newObs);
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 (_ReadOnlyAtom) {
134
- _inheritsLoose(BaseAtom, _ReadOnlyAtom);
351
+ var BaseAtom = /*#__PURE__*/function (_ReadOnlyAtom2) {
135
352
  function BaseAtom() {
136
- return _ReadOnlyAtom.apply(this, arguments) || this;
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._behavior$.next(nextVal);
358
+ this._next(nextVal);
141
359
  };
142
360
  _proto2.set = function set(nextKey, nextValue) {
143
361
  var _extends2;
144
- this._behavior$.next(_extends({}, this._behavior$.getValue(), (_extends2 = {}, _extends2[nextKey] = nextValue, _extends2)));
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
- _inheritsLoose(NullableBaseAtom, _BaseAtom);
150
- function NullableBaseAtom(_value) {
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
- // @ts-ignore
175
- _BaseAtom.prototype.next.call(this, nextVal);
377
+ this._next(nextVal);
176
378
  };
177
379
  _proto3.reset = function reset() {
178
- // @ts-ignore
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
- _inheritsLoose(ArrayAtom, _BaseAtom2);
185
- function ArrayAtom(initialValue) {
186
- var _this3;
187
- if (initialValue) {
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
- _this3 = _BaseAtom2.call(this, []) || this;
390
+ _this6 = _BaseAtom2.call(this, initialValue, undefined, options) || this;
191
391
  }
192
- return _assertThisInitialized(_this3);
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._behavior$.next([].concat(this._behavior$.getValue(), [nextVal]));
397
+ this._next([].concat(this.value(), [nextVal]));
197
398
  };
198
399
  return ArrayAtom;
199
400
  }(BaseAtom);
200
401
 
201
- //
202
- // export class ReadOnlyObjectAtom<
203
- // T extends {
204
- // [key in K]: V;
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
- // catch-all for developers
251
- // export type AnyAtom<T> = BaseAtom<T> | ArrayAtom<T> | ObjectAtom<T>;
408
+ // Legacy positional `readOnly` form.
252
409
 
253
- // observable<array> type
254
- // observable<object> type
255
- // nullable observable<object> type
256
- // observable type (primitive)
257
- // nullable observable type (primitive)
258
- // array type
259
- // object type
260
- // nullable (unitialized) object type
261
- // primitive type
262
- // nullable (unitialized) primitive type
263
- // readonly type
264
- // function definition
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
- atom = new ReadOnlyAtom(_value);
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
- return atom;
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 observable = rxjs.combineLatest.apply(void 0, atoms.map(function (a) {
287
- return a._behavior$;
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
- return function () {
302
- subscription.unsubscribe();
303
- };
304
- }, [atom]);
305
- return value;
306
- }
307
-
308
- function useSignal(signal, callback, id) {
309
- react.useEffect(function () {
310
- // Assuming the signal might not have an initial value method like `atom.value()`,
311
- // If your signal class has a method to get the current/latest value, use it here to initialize.
312
-
313
- var subscription;
314
- if (callback) {
315
- subscription = signal.subscribe(callback, id);
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
- return function () {
318
- if (subscription) {
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._behavior$.next(value);
475
+ atom.next(value);
349
476
  }
350
477
  }
351
478
  }
352
479
 
353
480
  var Signal = /*#__PURE__*/function () {
354
481
  function Signal() {
355
- this._subjects = new Map();
356
- this._defaultSubject = new rxjs.Subject();
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._subjects.has(id)) {
361
- var _this$_subjects$get;
362
- // If an ID is provided and exists, notify only that subscription.
363
- (_this$_subjects$get = this._subjects.get(id)) == null ? void 0 : _this$_subjects$get.next(value);
364
- } else {
365
- // No ID provided, notify all default subscribers.
366
- this._defaultSubject.next(value);
367
- // Additionally, notify all subscriptions as a broadcast.
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
- if (id) {
375
- // If an ID is provided, subscribe to the specific ID.
376
- if (!this._subjects.has(id)) {
377
- this._subjects.set(id, new rxjs.Subject());
378
- }
379
- return this._subjects.get(id).subscribe(callback);
380
- } else {
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
- // Optionally, implement unsubscribe logic to manage subscriptions.
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
- })({}, rxjs, React);
531
+ })({});