chem-rx 0.0.22 → 0.1.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,416 @@
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);
37
+ }
38
+ function _inheritsLoose(t, o) {
39
+ t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o);
19
40
  }
20
- function _inheritsLoose(subClass, superClass) {
21
- subClass.prototype = Object.create(superClass.prototype);
22
- subClass.prototype.constructor = subClass;
23
- _setPrototypeOf(subClass, superClass);
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);
24
45
  }
25
- function _setPrototypeOf(o, p) {
26
- _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
27
- o.__proto__ = p;
28
- return o;
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
+ }();
69
151
 
70
152
  var ReadOnlyAtom = /*#__PURE__*/function () {
71
- function ReadOnlyAtom(_value) {
153
+ function ReadOnlyAtom(_value, dependency) {
72
154
  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
- });
155
+ this._subscriptions = [];
156
+ this._dependency = null;
157
+ this._dependencySubscription = null;
158
+ this._subscriberCount = 0;
159
+ if (dependency) {
160
+ this._dependency = dependency;
161
+ this._store = new ValueStore(_value);
162
+ } else if (isAtomSource(_value)) {
163
+ this._store = new ValueStore(readAtomSourceValue(_value));
164
+ this._addSubscription(_value.subscribe(function (value) {
165
+ _this._next(value);
166
+ }));
82
167
  } else {
83
- // if it's just a value just use a regular behavior subject
84
- this._behavior$ = new rxjs.BehaviorSubject(_value);
168
+ this._store = new ValueStore(_value);
85
169
  }
86
170
  }
87
-
88
- // taken from Observable
89
171
  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
172
  _proto.derive = function derive(deriveFn) {
98
- return this.pipe(rxjs.map(deriveFn));
173
+ var _this2 = this;
174
+ var index = 0;
175
+ var hasCachedInput = false;
176
+ var cachedInput;
177
+ var cachedValue;
178
+ var getSnapshot = function getSnapshot(force) {
179
+ if (force === void 0) {
180
+ force = false;
181
+ }
182
+ var input = _this2.value();
183
+ if (force || !hasCachedInput || !Object.is(cachedInput, input)) {
184
+ cachedInput = input;
185
+ cachedValue = deriveFn(input, index++);
186
+ hasCachedInput = true;
187
+ }
188
+ return cachedValue;
189
+ };
190
+ return new ReadOnlyAtom(getSnapshot(), {
191
+ getSnapshot: getSnapshot,
192
+ subscribe: function subscribe(onDependencyChange) {
193
+ return _this2._subscribe(onDependencyChange, {
194
+ emitImmediately: false
195
+ });
196
+ },
197
+ shouldNotify: function shouldNotify() {
198
+ return true;
199
+ }
200
+ });
99
201
  };
100
- _proto.subscribe = function subscribe() {
101
- var _this$_behavior$2;
102
- return (_this$_behavior$2 = this._behavior$).subscribe.apply(_this$_behavior$2, arguments);
202
+ _proto.subscribe = function subscribe(observer) {
203
+ return this._subscribe(observer);
103
204
  };
104
205
  _proto.value = function value() {
105
- return this._behavior$.getValue();
106
- }
107
-
108
- // not needed?
109
- ;
206
+ if (this._dependency) {
207
+ this._refresh(false);
208
+ }
209
+ return this._store.getValue();
210
+ };
110
211
  _proto.dispose = function dispose() {
111
- var _this$_fromObservable;
112
- (_this$_fromObservable = this._fromObservableSubscription) == null ? void 0 : _this$_fromObservable.unsubscribe();
212
+ var _this$_dependencySubs;
213
+ (_this$_dependencySubs = this._dependencySubscription) == null || _this$_dependencySubs.unsubscribe();
214
+ this._dependencySubscription = null;
215
+ this._subscriberCount = 0;
216
+ for (var _iterator = _createForOfIteratorHelperLoose(this._subscriptions.splice(0)), _step; !(_step = _iterator()).done;) {
217
+ var subscription = _step.value;
218
+ subscription.unsubscribe();
219
+ }
113
220
  };
114
221
  _proto.get = function get(key) {
115
222
  var val = this.value();
116
223
  return val == null ? void 0 : val[key];
117
224
  };
118
225
  _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);
226
+ var _this3 = this;
227
+ var getSnapshot = function getSnapshot() {
228
+ var value = _this3.value();
229
+ return value == null ? void 0 : value[key];
230
+ };
231
+ return new ReadOnlyAtom(getSnapshot(), {
232
+ getSnapshot: getSnapshot,
233
+ subscribe: function subscribe(onDependencyChange) {
234
+ return _this3._subscribe(onDependencyChange, {
235
+ emitImmediately: false
236
+ });
237
+ }
238
+ });
239
+ };
240
+ _proto._refresh = function _refresh(emit, force) {
241
+ var _this$_dependency$sho, _this$_dependency$sho2, _this$_dependency;
242
+ if (force === void 0) {
243
+ force = false;
244
+ }
245
+ if (!this._dependency) {
246
+ return this._store.getValue();
247
+ }
248
+ var previousValue = this._store.getValue();
249
+ var nextValue = this._dependency.getSnapshot(force);
250
+ var shouldNotify = emit && ((_this$_dependency$sho = (_this$_dependency$sho2 = (_this$_dependency = this._dependency).shouldNotify) == null ? void 0 : _this$_dependency$sho2.call(_this$_dependency, previousValue, nextValue)) != null ? _this$_dependency$sho : !Object.is(previousValue, nextValue));
251
+ if (shouldNotify) {
252
+ this._store.next(nextValue);
253
+ } else {
254
+ this._store.setValue(nextValue);
255
+ }
256
+ return nextValue;
257
+ };
258
+ _proto._retainDependency = function _retainDependency() {
259
+ var _this4 = this;
260
+ if (!this._dependency) {
261
+ return;
262
+ }
263
+ this._subscriberCount += 1;
264
+ if (this._subscriberCount === 1) {
265
+ this._dependencySubscription = normalizeSubscription(this._dependency.subscribe(function () {
266
+ _this4._refresh(true, true);
267
+ }));
268
+ }
269
+ };
270
+ _proto._releaseDependency = function _releaseDependency() {
271
+ if (!this._dependency || this._subscriberCount === 0) {
272
+ return;
273
+ }
274
+ this._subscriberCount -= 1;
275
+ if (this._subscriberCount === 0) {
276
+ var _this$_dependencySubs2;
277
+ (_this$_dependencySubs2 = this._dependencySubscription) == null || _this$_dependencySubs2.unsubscribe();
278
+ this._dependencySubscription = null;
279
+ }
280
+ }
281
+
282
+ /** @internal */;
283
+ _proto._next = function _next(value) {
284
+ this._store.next(value);
285
+ }
126
286
 
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);
287
+ /** @internal */;
288
+ _proto._subscribe = function _subscribe(observer, options) {
289
+ var _this5 = this;
290
+ if (!this._dependency) {
291
+ return this._store.subscribe(observer, options);
292
+ }
293
+ this._refresh(false);
294
+ var subscription;
295
+ try {
296
+ this._retainDependency();
297
+ subscription = this._store.subscribe(observer, options);
298
+ } catch (error) {
299
+ this._releaseDependency();
300
+ throw error;
301
+ }
302
+ return createSubscription(function () {
303
+ subscription.unsubscribe();
304
+ _this5._releaseDependency();
305
+ });
306
+ }
307
+
308
+ /** @internal */;
309
+ _proto._addSubscription = function _addSubscription(subscription) {
310
+ var normalized = normalizeSubscription(subscription);
311
+ this._subscriptions.push(normalized);
312
+ return normalized;
132
313
  };
133
314
  return ReadOnlyAtom;
134
315
  }();
135
- var BaseAtom = /*#__PURE__*/function (_ReadOnlyAtom) {
136
- _inheritsLoose(BaseAtom, _ReadOnlyAtom);
316
+ var BaseAtom = /*#__PURE__*/function (_ReadOnlyAtom2) {
137
317
  function BaseAtom() {
138
- return _ReadOnlyAtom.apply(this, arguments) || this;
318
+ return _ReadOnlyAtom2.apply(this, arguments) || this;
139
319
  }
320
+ _inheritsLoose(BaseAtom, _ReadOnlyAtom2);
140
321
  var _proto2 = BaseAtom.prototype;
141
322
  _proto2.next = function next(nextVal) {
142
- this._behavior$.next(nextVal);
323
+ this._next(nextVal);
143
324
  };
144
325
  _proto2.set = function set(nextKey, nextValue) {
145
326
  var _extends2;
146
- this._behavior$.next(_extends({}, this._behavior$.getValue(), (_extends2 = {}, _extends2[nextKey] = nextValue, _extends2)));
327
+ var currentValue = this.value();
328
+ if (currentValue == null || typeof currentValue !== "object") {
329
+ throw new TypeError("Atom.set can only be used with object values");
330
+ }
331
+ this._next(_extends({}, currentValue, (_extends2 = {}, _extends2[nextKey] = nextValue, _extends2)));
147
332
  };
148
333
  return BaseAtom;
149
334
  }(ReadOnlyAtom);
150
335
  var NullableBaseAtom = /*#__PURE__*/function (_BaseAtom) {
151
- _inheritsLoose(NullableBaseAtom, _BaseAtom);
152
336
  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);
337
+ return _BaseAtom.call(this, _value) || this;
161
338
  }
162
-
163
- // @ts-ignore
339
+ _inheritsLoose(NullableBaseAtom, _BaseAtom);
164
340
  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
341
  _proto3.next = function next(nextVal) {
176
- // @ts-ignore
177
- _BaseAtom.prototype.next.call(this, nextVal);
342
+ this._next(nextVal);
178
343
  };
179
344
  _proto3.reset = function reset() {
180
- // @ts-ignore
181
- _BaseAtom.prototype.next.call(this, undefined);
345
+ this._next(undefined);
182
346
  };
183
347
  return NullableBaseAtom;
184
348
  }(BaseAtom);
185
349
  var ArrayAtom = /*#__PURE__*/function (_BaseAtom2) {
186
- _inheritsLoose(ArrayAtom, _BaseAtom2);
187
350
  function ArrayAtom(initialValue) {
188
- var _this3;
189
- if (initialValue) {
190
- _this3 = _BaseAtom2.call(this, initialValue) || this;
351
+ var _this6;
352
+ if (initialValue === undefined) {
353
+ _this6 = _BaseAtom2.call(this, []) || this;
191
354
  } else {
192
- _this3 = _BaseAtom2.call(this, []) || this;
355
+ _this6 = _BaseAtom2.call(this, initialValue) || this;
193
356
  }
194
- return _assertThisInitialized(_this3);
357
+ return _assertThisInitialized(_this6);
195
358
  }
359
+ _inheritsLoose(ArrayAtom, _BaseAtom2);
196
360
  var _proto4 = ArrayAtom.prototype;
197
361
  _proto4.push = function push(nextVal) {
198
- this._behavior$.next([].concat(this._behavior$.getValue(), [nextVal]));
362
+ this._next([].concat(this.value(), [nextVal]));
199
363
  };
200
364
  return ArrayAtom;
201
365
  }(BaseAtom);
202
-
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
- // }
251
-
252
- // catch-all for developers
253
- // export type AnyAtom<T> = BaseAtom<T> | ArrayAtom<T> | ObjectAtom<T>;
254
-
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
366
  function Atom(_value, readOnly) {
268
367
  if (readOnly === void 0) {
269
368
  readOnly = false;
270
369
  }
271
- var atom;
272
370
  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
371
+ return new ReadOnlyAtom(_value);
280
372
  }
281
-
282
- return atom;
373
+ if (isAtomSource(_value)) {
374
+ var sourceValue = readAtomSourceValue(_value);
375
+ if (Array.isArray(sourceValue)) {
376
+ return new ArrayAtom(_value);
377
+ }
378
+ return new BaseAtom(_value);
379
+ }
380
+ if (Array.isArray(_value)) {
381
+ return new ArrayAtom(_value);
382
+ }
383
+ if (_value == null) {
384
+ return new NullableBaseAtom();
385
+ }
386
+ return new BaseAtom(_value);
283
387
  }
284
388
  Atom.combine = function () {
285
389
  for (var _len = arguments.length, atoms = new Array(_len), _key = 0; _key < _len; _key++) {
286
390
  atoms[_key] = arguments[_key];
287
391
  }
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);
392
+ var getSnapshot = function getSnapshot() {
393
+ return atoms.map(function (atom) {
394
+ return atom.value();
302
395
  });
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);
396
+ };
397
+ return new ReadOnlyAtom(getSnapshot(), {
398
+ getSnapshot: getSnapshot,
399
+ subscribe: function subscribe(onDependencyChange) {
400
+ var subscriptions = atoms.map(function (atom) {
401
+ return atom._subscribe(onDependencyChange, {
402
+ emitImmediately: false
403
+ });
404
+ });
405
+ return function () {
406
+ for (var _iterator2 = _createForOfIteratorHelperLoose(subscriptions), _step2; !(_step2 = _iterator2()).done;) {
407
+ var subscription = _step2.value;
408
+ subscription.unsubscribe();
409
+ }
410
+ };
318
411
  }
319
- return function () {
320
- if (subscription) {
321
- subscription.unsubscribe();
322
- }
323
- };
324
- }, [signal]);
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
- }
412
+ });
413
+ };
341
414
 
342
415
  var hydratedAtomsSet = new WeakSet();
343
416
  function hydrateAtoms(values, options) {
@@ -347,52 +420,56 @@ function hydrateAtoms(values, options) {
347
420
  value = _step$value[1];
348
421
  if (!hydratedAtomsSet.has(atom) || options != null && options.force) {
349
422
  hydratedAtomsSet.add(atom);
350
- atom._behavior$.next(value);
423
+ atom.next(value);
351
424
  }
352
425
  }
353
426
  }
354
427
 
355
428
  var Signal = /*#__PURE__*/function () {
356
429
  function Signal() {
357
- this._subjects = new Map();
358
- this._defaultSubject = new rxjs.Subject();
430
+ this._listeners = [];
431
+ this._listenersById = new Map();
359
432
  }
360
433
  var _proto = Signal.prototype;
361
434
  _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
- });
435
+ if (id && this._listenersById.has(id)) {
436
+ notifyListeners(this._listenersById.get(id), value);
437
+ return;
438
+ }
439
+ notifyListeners(this._listeners, value);
440
+ for (var _iterator = _createForOfIteratorHelperLoose(this._listenersById.values()), _step; !(_step = _iterator()).done;) {
441
+ var listeners = _step.value;
442
+ notifyListeners(listeners, value);
373
443
  }
374
444
  };
375
445
  _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);
446
+ var _this = this,
447
+ _this$_listenersById$;
448
+ if (!id) {
449
+ this._listeners.push(callback);
450
+ return createSubscription(function () {
451
+ removeListener(_this._listeners, callback);
452
+ });
385
453
  }
386
- }
387
-
388
- // Optionally, implement unsubscribe logic to manage subscriptions.
389
- ;
454
+ var listeners = (_this$_listenersById$ = this._listenersById.get(id)) != null ? _this$_listenersById$ : [];
455
+ listeners.push(callback);
456
+ this._listenersById.set(id, listeners);
457
+ return createSubscription(function () {
458
+ removeListener(listeners, callback);
459
+ if (listeners.length === 0) {
460
+ _this._listenersById.delete(id);
461
+ }
462
+ });
463
+ };
390
464
  return Signal;
391
465
  }();
466
+ function removeListener(listeners, callback) {
467
+ var index = listeners.indexOf(callback);
468
+ if (index >= 0) {
469
+ listeners.splice(index, 1);
470
+ }
471
+ }
392
472
 
393
473
  exports.Atom = Atom;
394
474
  exports.Signal = Signal;
395
475
  exports.hydrateAtoms = hydrateAtoms;
396
- exports.useAtom = useAtom;
397
- exports.useSelectAtom = useSelectAtom;
398
- exports.useSignal = useSignal;