chem-rx 0.0.2 → 0.0.5

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/Atom.d.ts CHANGED
@@ -1,29 +1,62 @@
1
1
  import { BehaviorSubject, Observable, OperatorFunction, Subscription } from "rxjs";
2
2
  export type AtomTuple<T> = {
3
- [K in keyof T]: Atom<T[K]>;
3
+ [K in keyof T]: ReadOnlyAtom<T[K]>;
4
4
  };
5
- export declare class Atom<T> {
5
+ export declare class ReadOnlyAtom<T> {
6
6
  _behavior$: BehaviorSubject<T>;
7
7
  _parent?: BehaviorSubject<T>[];
8
8
  _bonds: BehaviorSubject<T>[];
9
9
  _fromObservable: Observable<T> | null;
10
10
  _fromObservableSubscription: Subscription | null;
11
- static combine<A extends readonly unknown[]>(...atoms: readonly [...AtomTuple<A>]): Atom<A>;
12
11
  constructor(_value: T | Observable<T>);
13
- push(nextVal: T): void;
14
- transform(): Atom<T>;
15
- transform<A>(op1: OperatorFunction<T, A>): Atom<A>;
16
- transform<A, B>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>): Atom<B>;
17
- transform<A, B, C>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>): Atom<C>;
18
- transform<A, B, C, D>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>): Atom<D>;
19
- transform<A, B, C, D, E>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>): Atom<E>;
20
- transform<A, B, C, D, E, F>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>): Atom<F>;
21
- transform<A, B, C, D, E, F, G>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>): Atom<G>;
22
- transform<A, B, C, D, E, F, G, H>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>): Atom<H>;
23
- transform<A, B, C, D, E, F, G, H, I>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>, op9: OperatorFunction<H, I>): Atom<I>;
24
- transform<A, B, C, D, E, F, G, H, I>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>, op9: OperatorFunction<H, I>, ...operations: OperatorFunction<any, any>[]): Atom<unknown>;
12
+ pipe(): ReadOnlyAtom<T>;
13
+ pipe<A>(op1: OperatorFunction<T, A>): ReadOnlyAtom<A>;
14
+ pipe<A, B>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>): ReadOnlyAtom<B>;
15
+ pipe<A, B, C>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>): ReadOnlyAtom<C>;
16
+ pipe<A, B, C, D>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>): ReadOnlyAtom<D>;
17
+ pipe<A, B, C, D, E>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>): ReadOnlyAtom<E>;
18
+ pipe<A, B, C, D, E, F>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>): ReadOnlyAtom<F>;
19
+ pipe<A, B, C, D, E, F, G>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>): ReadOnlyAtom<G>;
20
+ pipe<A, B, C, D, E, F, G, H>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>): ReadOnlyAtom<H>;
21
+ pipe<A, B, C, D, E, F, G, H, I>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>, op9: OperatorFunction<H, I>): ReadOnlyAtom<I>;
22
+ pipe<A, B, C, D, E, F, G, H, I>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>, op9: OperatorFunction<H, I>, ...operations: OperatorFunction<any, any>[]): ReadOnlyAtom<unknown>;
23
+ derive<A>(deriveFn: (value: T, index: number) => A): ReadOnlyAtom<A>;
25
24
  subscribe(...params: Parameters<BehaviorSubject<T>["subscribe"]>): Subscription;
26
- getValue(): T;
25
+ value(): T;
27
26
  dispose(): void;
27
+ get<K extends keyof T>(key: K): T extends (infer W)[] ? T[number] : T extends {
28
+ [key in keyof T]: infer W;
29
+ } ? T[keyof T] : undefined;
30
+ select<K extends keyof T>(key: K): T[K] extends (infer W)[] ? ArrayAtom<W> : T[K] extends {
31
+ [key: string | symbol]: infer W;
32
+ } ? ObjectAtom<T[K]> : BaseAtom<T[K]>;
33
+ }
34
+ export declare class BaseAtom<T> extends ReadOnlyAtom<T> {
35
+ set(nextVal: T): void;
36
+ }
37
+ export declare class ArrayAtom<T> extends ReadOnlyAtom<T[]> {
38
+ constructor(initialValue: T[]);
39
+ push(nextVal: T): void;
40
+ }
41
+ export declare class ObjectAtom<T extends {
42
+ [key in K]: V;
43
+ }, K extends string | number | symbol = keyof T, V = T[K]> extends ReadOnlyAtom<T> {
44
+ set(nextKey: K, nextValue: V): void;
45
+ }
46
+ export declare function Atom<T>(value: T extends {
47
+ [key: string]: infer V;
48
+ } | any[] ? never : Observable<T>): BaseAtom<T>;
49
+ export declare function Atom<T extends any[]>(value: Observable<T>): ArrayAtom<T[number]>;
50
+ export declare function Atom<T>(value: T extends {
51
+ [key in keyof T]: infer V;
52
+ } ? Observable<T> : never): ObjectAtom<T>;
53
+ export declare function Atom<T extends any[]>(value: T): ArrayAtom<T[number]>;
54
+ export declare function Atom<T extends {
55
+ [key: string]: T[keyof T];
56
+ }>(value: T): ObjectAtom<T>;
57
+ export declare function Atom<T>(value: T): BaseAtom<T>;
58
+ export declare function Atom<T>(value: T, readOnly?: boolean): ReadOnlyAtom<T>;
59
+ export declare namespace Atom {
60
+ var combine: <A extends readonly unknown[]>(...atoms_0: AtomTuple<A>) => ReadOnlyAtom<A>;
28
61
  }
29
62
  //# sourceMappingURL=Atom.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Atom.d.ts","sourceRoot":"","sources":["../src/Atom.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAGf,UAAU,EACV,gBAAgB,EAChB,YAAY,EACb,MAAM,MAAM,CAAC;AAGd,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI;KACxB,CAAC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AAGjB,wBAAgB,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;AAG/C,wBAAgB,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,OAAO,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;yBAAvD,IAAI"}
package/dist/Signal.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { Subject, Subscription } from "rxjs";
2
- export declare class Signal<T = void> {
1
+ import { Subject } from "rxjs";
2
+ export declare class BaseSignal<T = any> {
3
3
  _subject$: Subject<T>;
4
4
  constructor();
5
5
  ping(value: T): void;
6
- subscribe(...params: Parameters<Subject<T>["subscribe"]>): Subscription;
7
- dispose(): void;
6
+ subscribe(...params: Parameters<Subject<T>["subscribe"]>): import("rxjs").Subscription;
8
7
  }
8
+ export declare function Signal<T>(): void;
9
9
  //# sourceMappingURL=Signal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Signal.d.ts","sourceRoot":"","sources":["../src/Signal.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EAIP,YAAY,EACb,MAAM,MAAM,CAAC;AAGd,qBAAa,MAAM,CAAC,CAAC,GAAG,IAAI;IAC1B,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;;IAOtB,IAAI,CAAC,KAAK,EAAE,CAAC;IAIb,SAAS,CAAC,GAAG,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;IAKxD,OAAO;CAGR"}
1
+ {"version":3,"file":"Signal.d.ts","sourceRoot":"","sources":["../src/Signal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,qBAAa,UAAU,CAAC,CAAC,GAAG,GAAG;IAC7B,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;;IAOtB,IAAI,CAAC,KAAK,EAAE,CAAC;IAIb,SAAS,CAAC,GAAG,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;CAGzD;AAED,wBAAgB,MAAM,CAAC,CAAC,UAAM"}
@@ -1,3 +1,3 @@
1
- import { Atom } from "./Atom";
2
- export declare function hydrateAtoms(values: readonly [Atom<any>, any][]): void;
1
+ import { BaseAtom } from "./Atom";
2
+ export declare function hydrateAtoms(values: readonly [BaseAtom<any>, any][]): void;
3
3
  //# sourceMappingURL=hydrateAtoms.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"hydrateAtoms.d.ts","sourceRoot":"","sources":["../src/hydrateAtoms.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAE9B,wBAAgB,YAAY,CAAC,MAAM,EAAE,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,EAAE,QAI/D"}
1
+ {"version":3,"file":"hydrateAtoms.d.ts","sourceRoot":"","sources":["../src/hydrateAtoms.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,wBAAgB,YAAY,CAAC,MAAM,EAAE,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,EAAE,QAInE"}
package/dist/index.cjs.js CHANGED
@@ -3,17 +3,66 @@
3
3
  var rxjs = require('rxjs');
4
4
  var react = require('react');
5
5
 
6
- var Atom = /*#__PURE__*/function () {
7
- Atom.combine = function combine() {
8
- for (var _len = arguments.length, atoms = new Array(_len), _key = 0; _key < _len; _key++) {
9
- atoms[_key] = arguments[_key];
6
+ 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
+ }
10
15
  }
11
- var observable = rxjs.combineLatest.apply(void 0, atoms.map(function (a) {
12
- return a._behavior$;
13
- }));
14
- return new Atom(observable);
16
+ return target;
17
+ };
18
+ return _extends.apply(this, arguments);
19
+ }
20
+ function _inheritsLoose(subClass, superClass) {
21
+ subClass.prototype = Object.create(superClass.prototype);
22
+ subClass.prototype.constructor = subClass;
23
+ _setPrototypeOf(subClass, superClass);
24
+ }
25
+ function _setPrototypeOf(o, p) {
26
+ _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
27
+ o.__proto__ = p;
28
+ return o;
15
29
  };
16
- function Atom(_value) {
30
+ return _setPrototypeOf(o, p);
31
+ }
32
+ function _unsupportedIterableToArray(o, minLen) {
33
+ if (!o) return;
34
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
35
+ var n = Object.prototype.toString.call(o).slice(8, -1);
36
+ if (n === "Object" && o.constructor) n = o.constructor.name;
37
+ if (n === "Map" || n === "Set") return Array.from(o);
38
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
39
+ }
40
+ function _arrayLikeToArray(arr, len) {
41
+ if (len == null || len > arr.length) len = arr.length;
42
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
43
+ return arr2;
44
+ }
45
+ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
46
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
47
+ if (it) return (it = it.call(o)).next.bind(it);
48
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
49
+ if (it) o = it;
50
+ var i = 0;
51
+ return function () {
52
+ if (i >= o.length) return {
53
+ done: true
54
+ };
55
+ return {
56
+ done: false,
57
+ value: o[i++]
58
+ };
59
+ };
60
+ }
61
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
62
+ }
63
+
64
+ var ReadOnlyAtom = /*#__PURE__*/function () {
65
+ function ReadOnlyAtom(_value) {
17
66
  var _this = this;
18
67
  this._bonds = [];
19
68
  this._fromObservable = null;
@@ -22,31 +71,31 @@ var Atom = /*#__PURE__*/function () {
22
71
  this._fromObservable = _value;
23
72
  this._behavior$ = new rxjs.BehaviorSubject(null);
24
73
  this._fromObservableSubscription = _value.subscribe(function (value) {
25
- _this.push(value);
74
+ _this._behavior$.next(value);
26
75
  });
27
76
  } else {
28
77
  // if it's just a value just use a regular behavior subject
29
78
  this._behavior$ = new rxjs.BehaviorSubject(_value);
30
79
  }
31
80
  }
32
- var _proto = Atom.prototype;
33
- _proto.push = function push(nextVal) {
34
- this._behavior$.next(nextVal);
35
- }
36
81
 
37
82
  // taken from Observable
38
- ;
39
- _proto.transform = function transform() {
83
+ var _proto = ReadOnlyAtom.prototype;
84
+ _proto.pipe = function pipe() {
40
85
  var _this$_behavior$;
41
86
  // @ts-ignore can't match overloaded function
42
87
  var observable = (_this$_behavior$ = this._behavior$).pipe.apply(_this$_behavior$, arguments);
43
- return new Atom(observable);
88
+ var newAtom = new ReadOnlyAtom(observable);
89
+ return newAtom;
90
+ };
91
+ _proto.derive = function derive(deriveFn) {
92
+ return this.pipe(rxjs.map(deriveFn));
44
93
  };
45
94
  _proto.subscribe = function subscribe() {
46
95
  var _this$_behavior$2;
47
96
  return (_this$_behavior$2 = this._behavior$).subscribe.apply(_this$_behavior$2, arguments);
48
97
  };
49
- _proto.getValue = function getValue() {
98
+ _proto.value = function value() {
50
99
  return this._behavior$.getValue();
51
100
  }
52
101
 
@@ -56,11 +105,127 @@ var Atom = /*#__PURE__*/function () {
56
105
  var _this$_fromObservable;
57
106
  (_this$_fromObservable = this._fromObservableSubscription) == null ? void 0 : _this$_fromObservable.unsubscribe();
58
107
  };
59
- return Atom;
108
+ _proto.get = function get(key) {
109
+ var val = this.value();
110
+ return val[key];
111
+ };
112
+ _proto.select = function select(key) {
113
+ var newObs = this._behavior$.pipe(rxjs.distinctUntilKeyChanged(key), rxjs.map(function (k) {
114
+ return k == null ? void 0 : k[key];
115
+ }));
116
+ // Can't get typescript to recognize the types here so I'm cheating
117
+ return Atom(newObs);
118
+ };
119
+ return ReadOnlyAtom;
60
120
  }();
121
+ var BaseAtom = /*#__PURE__*/function (_ReadOnlyAtom) {
122
+ _inheritsLoose(BaseAtom, _ReadOnlyAtom);
123
+ function BaseAtom() {
124
+ return _ReadOnlyAtom.apply(this, arguments) || this;
125
+ }
126
+ var _proto2 = BaseAtom.prototype;
127
+ _proto2.set = function set(nextVal) {
128
+ this._behavior$.next(nextVal);
129
+ };
130
+ return BaseAtom;
131
+ }(ReadOnlyAtom);
132
+ var ArrayAtom = /*#__PURE__*/function (_ReadOnlyAtom2) {
133
+ _inheritsLoose(ArrayAtom, _ReadOnlyAtom2);
134
+ function ArrayAtom(initialValue) {
135
+ return _ReadOnlyAtom2.call(this, initialValue) || this;
136
+ }
137
+ var _proto3 = ArrayAtom.prototype;
138
+ _proto3.push = function push(nextVal) {
139
+ this._behavior$.next([].concat(this._behavior$.getValue(), [nextVal]));
140
+ };
141
+ return ArrayAtom;
142
+ }(ReadOnlyAtom);
143
+ //
144
+ // export class ReadOnlyObjectAtom<
145
+ // T extends {
146
+ // [key in K]: V;
147
+ // },
148
+ // K extends string | number | symbol = keyof T,
149
+ // V = T[K]
150
+ // > extends ReadOnlyAtom<T> {
151
+ // get(nextKey: K) {
152
+ // return this.value()[nextKey];
153
+ // }
154
+ //
155
+ // select<K extends keyof T>(
156
+ // key: K
157
+ // ): T[K] extends (infer W)[]
158
+ // ? ArrayAtom<W>
159
+ // : T[K] extends { [key in infer L]?: infer W }
160
+ // ? ObjectAtom<T[K]>
161
+ // : BaseAtom<T> {
162
+ // const newObs = this._behavior$.pipe(
163
+ // distinctUntilKeyChanged(key),
164
+ // map((k) => k?.[key])
165
+ // );
166
+ // // Can't get typescript to recognize the types here so I'm cheating
167
+ // return Atom(newObs) as unknown as T[K] extends (infer W)[]
168
+ // ? ArrayAtom<W>
169
+ // : T[K] extends { [key in infer L]?: infer W }
170
+ // ? ObjectAtom<T[K]>
171
+ // : BaseAtom<T>;
172
+ // }
173
+ // }
174
+
175
+ var ObjectAtom = /*#__PURE__*/function (_ReadOnlyAtom3) {
176
+ _inheritsLoose(ObjectAtom, _ReadOnlyAtom3);
177
+ function ObjectAtom() {
178
+ return _ReadOnlyAtom3.apply(this, arguments) || this;
179
+ }
180
+ var _proto4 = ObjectAtom.prototype;
181
+ _proto4.set = function set(nextKey, nextValue) {
182
+ var _extends2;
183
+ this._behavior$.next(_extends({}, this._behavior$.getValue(), (_extends2 = {}, _extends2[nextKey] = nextValue, _extends2)));
184
+ };
185
+ return ObjectAtom;
186
+ }(ReadOnlyAtom);
187
+
188
+ // catch-all for developers
189
+ // export type AnyAtom<T> = BaseAtom<T> | ArrayAtom<T> | ObjectAtom<T>;
190
+
191
+ // observable type (primitive)
192
+ // observable<array> type
193
+ // observable<object> type
194
+ // array type
195
+ // object type
196
+ // primitive type
197
+ // readonly type
198
+ // function definition
199
+ function Atom(_value, readOnly) {
200
+ if (readOnly === void 0) {
201
+ readOnly = false;
202
+ }
203
+ var atom;
204
+ if (readOnly) {
205
+ atom = new ReadOnlyAtom(_value);
206
+ } else if (Array.isArray(_value)) {
207
+ atom = new ArrayAtom(_value); // For arrays
208
+ } else if (typeof _value === "object" && _value !== null) {
209
+ atom = new ObjectAtom(_value); // For objects
210
+ } else {
211
+ atom = new BaseAtom(_value); // For other types
212
+ }
213
+
214
+ return atom;
215
+ }
216
+ Atom.combine = function () {
217
+ for (var _len = arguments.length, atoms = new Array(_len), _key = 0; _key < _len; _key++) {
218
+ atoms[_key] = arguments[_key];
219
+ }
220
+ var observable = rxjs.combineLatest.apply(void 0, atoms.map(function (a) {
221
+ return a._behavior$;
222
+ }));
223
+ var newAtom = new ReadOnlyAtom(observable);
224
+ return newAtom;
225
+ };
61
226
 
62
227
  function useAtom(atom) {
63
- var _useState = react.useState(atom.getValue()),
228
+ var _useState = react.useState(atom.value()),
64
229
  value = _useState[0],
65
230
  setValue = _useState[1];
66
231
  react.useEffect(function () {
@@ -74,36 +239,19 @@ function useAtom(atom) {
74
239
  return value;
75
240
  }
76
241
 
77
- function _unsupportedIterableToArray(o, minLen) {
78
- if (!o) return;
79
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
80
- var n = Object.prototype.toString.call(o).slice(8, -1);
81
- if (n === "Object" && o.constructor) n = o.constructor.name;
82
- if (n === "Map" || n === "Set") return Array.from(o);
83
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
84
- }
85
- function _arrayLikeToArray(arr, len) {
86
- if (len == null || len > arr.length) len = arr.length;
87
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
88
- return arr2;
89
- }
90
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
91
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
92
- if (it) return (it = it.call(o)).next.bind(it);
93
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
94
- if (it) o = it;
95
- var i = 0;
242
+ function useSelectAtom(atom, key) {
243
+ var _useState = react.useState(atom.get(key)),
244
+ value = _useState[0],
245
+ setValue = _useState[1];
246
+ react.useEffect(function () {
247
+ var subscription = atom.select(key).subscribe(function (val) {
248
+ setValue(val);
249
+ });
96
250
  return function () {
97
- if (i >= o.length) return {
98
- done: true
99
- };
100
- return {
101
- done: false,
102
- value: o[i++]
103
- };
251
+ subscription.unsubscribe();
104
252
  };
105
- }
106
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
253
+ }, [atom]);
254
+ return value;
107
255
  }
108
256
 
109
257
  function hydrateAtoms(values) {
@@ -111,33 +259,14 @@ function hydrateAtoms(values) {
111
259
  var _step$value = _step.value,
112
260
  atom = _step$value[0],
113
261
  value = _step$value[1];
114
- atom.push(value);
262
+ atom._behavior$.next(value);
115
263
  }
116
264
  }
117
265
 
118
- var Signal = /*#__PURE__*/function () {
119
- function Signal() {
120
- // if it's just a value just use a regular behavior subject
121
- this._subject$ = new rxjs.Subject();
122
- }
123
- var _proto = Signal.prototype;
124
- _proto.ping = function ping(value) {
125
- this._subject$.next(value);
126
- };
127
- _proto.subscribe = function subscribe() {
128
- var _this$_subject$;
129
- return (_this$_subject$ = this._subject$).subscribe.apply(_this$_subject$, arguments);
130
- }
131
-
132
- // not needed?
133
- ;
134
- _proto.dispose = function dispose() {
135
- this._subject$.unsubscribe();
136
- };
137
- return Signal;
138
- }();
266
+ function Signal() {}
139
267
 
140
268
  exports.Atom = Atom;
141
269
  exports.Signal = Signal;
142
270
  exports.hydrateAtoms = hydrateAtoms;
143
271
  exports.useAtom = useAtom;
272
+ exports.useSelectAtom = useSelectAtom;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { Atom } from "./Atom";
2
2
  export { useAtom } from "./useAtom";
3
+ export { useSelectAtom } from "./useSelectAtom";
3
4
  export { hydrateAtoms } from "./hydrateAtoms";
4
5
  export { Signal } from "./Signal";
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC"}
@@ -1,17 +1,66 @@
1
1
  var chemicalRx = (function (exports, rxjs, react) {
2
2
  'use strict';
3
3
 
4
- var Atom = /*#__PURE__*/function () {
5
- Atom.combine = function combine() {
6
- for (var _len = arguments.length, atoms = new Array(_len), _key = 0; _key < _len; _key++) {
7
- atoms[_key] = arguments[_key];
4
+ 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
+ }
8
13
  }
9
- var observable = rxjs.combineLatest.apply(void 0, atoms.map(function (a) {
10
- return a._behavior$;
11
- }));
12
- return new Atom(observable);
14
+ return target;
15
+ };
16
+ return _extends.apply(this, arguments);
17
+ }
18
+ function _inheritsLoose(subClass, superClass) {
19
+ subClass.prototype = Object.create(superClass.prototype);
20
+ subClass.prototype.constructor = subClass;
21
+ _setPrototypeOf(subClass, superClass);
22
+ }
23
+ function _setPrototypeOf(o, p) {
24
+ _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
25
+ o.__proto__ = p;
26
+ return o;
13
27
  };
14
- function Atom(_value) {
28
+ return _setPrototypeOf(o, p);
29
+ }
30
+ function _unsupportedIterableToArray(o, minLen) {
31
+ if (!o) return;
32
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
33
+ var n = Object.prototype.toString.call(o).slice(8, -1);
34
+ if (n === "Object" && o.constructor) n = o.constructor.name;
35
+ if (n === "Map" || n === "Set") return Array.from(o);
36
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
37
+ }
38
+ function _arrayLikeToArray(arr, len) {
39
+ if (len == null || len > arr.length) len = arr.length;
40
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
41
+ return arr2;
42
+ }
43
+ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
44
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
45
+ if (it) return (it = it.call(o)).next.bind(it);
46
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
47
+ if (it) o = it;
48
+ var i = 0;
49
+ return function () {
50
+ if (i >= o.length) return {
51
+ done: true
52
+ };
53
+ return {
54
+ done: false,
55
+ value: o[i++]
56
+ };
57
+ };
58
+ }
59
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
60
+ }
61
+
62
+ var ReadOnlyAtom = /*#__PURE__*/function () {
63
+ function ReadOnlyAtom(_value) {
15
64
  var _this = this;
16
65
  this._bonds = [];
17
66
  this._fromObservable = null;
@@ -20,31 +69,31 @@ var chemicalRx = (function (exports, rxjs, react) {
20
69
  this._fromObservable = _value;
21
70
  this._behavior$ = new rxjs.BehaviorSubject(null);
22
71
  this._fromObservableSubscription = _value.subscribe(function (value) {
23
- _this.push(value);
72
+ _this._behavior$.next(value);
24
73
  });
25
74
  } else {
26
75
  // if it's just a value just use a regular behavior subject
27
76
  this._behavior$ = new rxjs.BehaviorSubject(_value);
28
77
  }
29
78
  }
30
- var _proto = Atom.prototype;
31
- _proto.push = function push(nextVal) {
32
- this._behavior$.next(nextVal);
33
- }
34
79
 
35
80
  // taken from Observable
36
- ;
37
- _proto.transform = function transform() {
81
+ var _proto = ReadOnlyAtom.prototype;
82
+ _proto.pipe = function pipe() {
38
83
  var _this$_behavior$;
39
84
  // @ts-ignore can't match overloaded function
40
85
  var observable = (_this$_behavior$ = this._behavior$).pipe.apply(_this$_behavior$, arguments);
41
- return new Atom(observable);
86
+ var newAtom = new ReadOnlyAtom(observable);
87
+ return newAtom;
88
+ };
89
+ _proto.derive = function derive(deriveFn) {
90
+ return this.pipe(rxjs.map(deriveFn));
42
91
  };
43
92
  _proto.subscribe = function subscribe() {
44
93
  var _this$_behavior$2;
45
94
  return (_this$_behavior$2 = this._behavior$).subscribe.apply(_this$_behavior$2, arguments);
46
95
  };
47
- _proto.getValue = function getValue() {
96
+ _proto.value = function value() {
48
97
  return this._behavior$.getValue();
49
98
  }
50
99
 
@@ -54,11 +103,127 @@ var chemicalRx = (function (exports, rxjs, react) {
54
103
  var _this$_fromObservable;
55
104
  (_this$_fromObservable = this._fromObservableSubscription) == null ? void 0 : _this$_fromObservable.unsubscribe();
56
105
  };
57
- return Atom;
106
+ _proto.get = function get(key) {
107
+ var val = this.value();
108
+ return val[key];
109
+ };
110
+ _proto.select = function select(key) {
111
+ var newObs = this._behavior$.pipe(rxjs.distinctUntilKeyChanged(key), rxjs.map(function (k) {
112
+ return k == null ? void 0 : k[key];
113
+ }));
114
+ // Can't get typescript to recognize the types here so I'm cheating
115
+ return Atom(newObs);
116
+ };
117
+ return ReadOnlyAtom;
58
118
  }();
119
+ var BaseAtom = /*#__PURE__*/function (_ReadOnlyAtom) {
120
+ _inheritsLoose(BaseAtom, _ReadOnlyAtom);
121
+ function BaseAtom() {
122
+ return _ReadOnlyAtom.apply(this, arguments) || this;
123
+ }
124
+ var _proto2 = BaseAtom.prototype;
125
+ _proto2.set = function set(nextVal) {
126
+ this._behavior$.next(nextVal);
127
+ };
128
+ return BaseAtom;
129
+ }(ReadOnlyAtom);
130
+ var ArrayAtom = /*#__PURE__*/function (_ReadOnlyAtom2) {
131
+ _inheritsLoose(ArrayAtom, _ReadOnlyAtom2);
132
+ function ArrayAtom(initialValue) {
133
+ return _ReadOnlyAtom2.call(this, initialValue) || this;
134
+ }
135
+ var _proto3 = ArrayAtom.prototype;
136
+ _proto3.push = function push(nextVal) {
137
+ this._behavior$.next([].concat(this._behavior$.getValue(), [nextVal]));
138
+ };
139
+ return ArrayAtom;
140
+ }(ReadOnlyAtom);
141
+ //
142
+ // export class ReadOnlyObjectAtom<
143
+ // T extends {
144
+ // [key in K]: V;
145
+ // },
146
+ // K extends string | number | symbol = keyof T,
147
+ // V = T[K]
148
+ // > extends ReadOnlyAtom<T> {
149
+ // get(nextKey: K) {
150
+ // return this.value()[nextKey];
151
+ // }
152
+ //
153
+ // select<K extends keyof T>(
154
+ // key: K
155
+ // ): T[K] extends (infer W)[]
156
+ // ? ArrayAtom<W>
157
+ // : T[K] extends { [key in infer L]?: infer W }
158
+ // ? ObjectAtom<T[K]>
159
+ // : BaseAtom<T> {
160
+ // const newObs = this._behavior$.pipe(
161
+ // distinctUntilKeyChanged(key),
162
+ // map((k) => k?.[key])
163
+ // );
164
+ // // Can't get typescript to recognize the types here so I'm cheating
165
+ // return Atom(newObs) as unknown as T[K] extends (infer W)[]
166
+ // ? ArrayAtom<W>
167
+ // : T[K] extends { [key in infer L]?: infer W }
168
+ // ? ObjectAtom<T[K]>
169
+ // : BaseAtom<T>;
170
+ // }
171
+ // }
172
+
173
+ var ObjectAtom = /*#__PURE__*/function (_ReadOnlyAtom3) {
174
+ _inheritsLoose(ObjectAtom, _ReadOnlyAtom3);
175
+ function ObjectAtom() {
176
+ return _ReadOnlyAtom3.apply(this, arguments) || this;
177
+ }
178
+ var _proto4 = ObjectAtom.prototype;
179
+ _proto4.set = function set(nextKey, nextValue) {
180
+ var _extends2;
181
+ this._behavior$.next(_extends({}, this._behavior$.getValue(), (_extends2 = {}, _extends2[nextKey] = nextValue, _extends2)));
182
+ };
183
+ return ObjectAtom;
184
+ }(ReadOnlyAtom);
185
+
186
+ // catch-all for developers
187
+ // export type AnyAtom<T> = BaseAtom<T> | ArrayAtom<T> | ObjectAtom<T>;
188
+
189
+ // observable type (primitive)
190
+ // observable<array> type
191
+ // observable<object> type
192
+ // array type
193
+ // object type
194
+ // primitive type
195
+ // readonly type
196
+ // function definition
197
+ function Atom(_value, readOnly) {
198
+ if (readOnly === void 0) {
199
+ readOnly = false;
200
+ }
201
+ var atom;
202
+ if (readOnly) {
203
+ atom = new ReadOnlyAtom(_value);
204
+ } else if (Array.isArray(_value)) {
205
+ atom = new ArrayAtom(_value); // For arrays
206
+ } else if (typeof _value === "object" && _value !== null) {
207
+ atom = new ObjectAtom(_value); // For objects
208
+ } else {
209
+ atom = new BaseAtom(_value); // For other types
210
+ }
211
+
212
+ return atom;
213
+ }
214
+ Atom.combine = function () {
215
+ for (var _len = arguments.length, atoms = new Array(_len), _key = 0; _key < _len; _key++) {
216
+ atoms[_key] = arguments[_key];
217
+ }
218
+ var observable = rxjs.combineLatest.apply(void 0, atoms.map(function (a) {
219
+ return a._behavior$;
220
+ }));
221
+ var newAtom = new ReadOnlyAtom(observable);
222
+ return newAtom;
223
+ };
59
224
 
60
225
  function useAtom(atom) {
61
- var _useState = react.useState(atom.getValue()),
226
+ var _useState = react.useState(atom.value()),
62
227
  value = _useState[0],
63
228
  setValue = _useState[1];
64
229
  react.useEffect(function () {
@@ -72,36 +237,19 @@ var chemicalRx = (function (exports, rxjs, react) {
72
237
  return value;
73
238
  }
74
239
 
75
- function _unsupportedIterableToArray(o, minLen) {
76
- if (!o) return;
77
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
78
- var n = Object.prototype.toString.call(o).slice(8, -1);
79
- if (n === "Object" && o.constructor) n = o.constructor.name;
80
- if (n === "Map" || n === "Set") return Array.from(o);
81
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
82
- }
83
- function _arrayLikeToArray(arr, len) {
84
- if (len == null || len > arr.length) len = arr.length;
85
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
86
- return arr2;
87
- }
88
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
89
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
90
- if (it) return (it = it.call(o)).next.bind(it);
91
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
92
- if (it) o = it;
93
- var i = 0;
240
+ function useSelectAtom(atom, key) {
241
+ var _useState = react.useState(atom.get(key)),
242
+ value = _useState[0],
243
+ setValue = _useState[1];
244
+ react.useEffect(function () {
245
+ var subscription = atom.select(key).subscribe(function (val) {
246
+ setValue(val);
247
+ });
94
248
  return function () {
95
- if (i >= o.length) return {
96
- done: true
97
- };
98
- return {
99
- done: false,
100
- value: o[i++]
101
- };
249
+ subscription.unsubscribe();
102
250
  };
103
- }
104
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
251
+ }, [atom]);
252
+ return value;
105
253
  }
106
254
 
107
255
  function hydrateAtoms(values) {
@@ -109,36 +257,17 @@ var chemicalRx = (function (exports, rxjs, react) {
109
257
  var _step$value = _step.value,
110
258
  atom = _step$value[0],
111
259
  value = _step$value[1];
112
- atom.push(value);
260
+ atom._behavior$.next(value);
113
261
  }
114
262
  }
115
263
 
116
- var Signal = /*#__PURE__*/function () {
117
- function Signal() {
118
- // if it's just a value just use a regular behavior subject
119
- this._subject$ = new rxjs.Subject();
120
- }
121
- var _proto = Signal.prototype;
122
- _proto.ping = function ping(value) {
123
- this._subject$.next(value);
124
- };
125
- _proto.subscribe = function subscribe() {
126
- var _this$_subject$;
127
- return (_this$_subject$ = this._subject$).subscribe.apply(_this$_subject$, arguments);
128
- }
129
-
130
- // not needed?
131
- ;
132
- _proto.dispose = function dispose() {
133
- this._subject$.unsubscribe();
134
- };
135
- return Signal;
136
- }();
264
+ function Signal() {}
137
265
 
138
266
  exports.Atom = Atom;
139
267
  exports.Signal = Signal;
140
268
  exports.hydrateAtoms = hydrateAtoms;
141
269
  exports.useAtom = useAtom;
270
+ exports.useSelectAtom = useSelectAtom;
142
271
 
143
272
  return exports;
144
273
 
package/dist/index.js CHANGED
@@ -1,11 +1,22 @@
1
- import { combineLatest, isObservable, BehaviorSubject, Subject } from 'rxjs';
1
+ import { combineLatest, isObservable, BehaviorSubject, map, distinctUntilKeyChanged } from 'rxjs';
2
2
  import { useState, useEffect } from 'react';
3
3
 
4
- class Atom {
5
- static combine(...atoms) {
6
- const observable = combineLatest(...atoms.map(a => a._behavior$));
7
- return new Atom(observable);
8
- }
4
+ 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
+ }
13
+ }
14
+ return target;
15
+ };
16
+ return _extends.apply(this, arguments);
17
+ }
18
+
19
+ class ReadOnlyAtom {
9
20
  constructor(_value) {
10
21
  this._bonds = [];
11
22
  this._fromObservable = null;
@@ -14,28 +25,29 @@ class Atom {
14
25
  this._fromObservable = _value;
15
26
  this._behavior$ = new BehaviorSubject(null);
16
27
  this._fromObservableSubscription = _value.subscribe(value => {
17
- this.push(value);
28
+ this._behavior$.next(value);
18
29
  });
19
30
  } else {
20
31
  // if it's just a value just use a regular behavior subject
21
32
  this._behavior$ = new BehaviorSubject(_value);
22
33
  }
23
34
  }
24
- push(nextVal) {
25
- this._behavior$.next(nextVal);
26
- }
27
35
 
28
36
  // taken from Observable
29
37
 
30
- transform(...operations) {
38
+ pipe(...operations) {
31
39
  // @ts-ignore can't match overloaded function
32
40
  const observable = this._behavior$.pipe(...operations);
33
- return new Atom(observable);
41
+ const newAtom = new ReadOnlyAtom(observable);
42
+ return newAtom;
43
+ }
44
+ derive(deriveFn) {
45
+ return this.pipe(map(deriveFn));
34
46
  }
35
47
  subscribe(...params) {
36
48
  return this._behavior$.subscribe(...params);
37
49
  }
38
- getValue() {
50
+ value() {
39
51
  return this._behavior$.getValue();
40
52
  }
41
53
 
@@ -44,10 +56,102 @@ class Atom {
44
56
  var _this$_fromObservable;
45
57
  (_this$_fromObservable = this._fromObservableSubscription) == null ? void 0 : _this$_fromObservable.unsubscribe();
46
58
  }
59
+ get(key) {
60
+ const val = this.value();
61
+ return val[key];
62
+ }
63
+ select(key) {
64
+ const newObs = this._behavior$.pipe(distinctUntilKeyChanged(key), map(k => k == null ? void 0 : k[key]));
65
+ // Can't get typescript to recognize the types here so I'm cheating
66
+ return Atom(newObs);
67
+ }
68
+ }
69
+ class BaseAtom extends ReadOnlyAtom {
70
+ set(nextVal) {
71
+ this._behavior$.next(nextVal);
72
+ }
73
+ }
74
+ class ArrayAtom extends ReadOnlyAtom {
75
+ constructor(initialValue) {
76
+ super(initialValue);
77
+ }
78
+ push(nextVal) {
79
+ this._behavior$.next([...this._behavior$.getValue(), nextVal]);
80
+ }
81
+ }
82
+ //
83
+ // export class ReadOnlyObjectAtom<
84
+ // T extends {
85
+ // [key in K]: V;
86
+ // },
87
+ // K extends string | number | symbol = keyof T,
88
+ // V = T[K]
89
+ // > extends ReadOnlyAtom<T> {
90
+ // get(nextKey: K) {
91
+ // return this.value()[nextKey];
92
+ // }
93
+ //
94
+ // select<K extends keyof T>(
95
+ // key: K
96
+ // ): T[K] extends (infer W)[]
97
+ // ? ArrayAtom<W>
98
+ // : T[K] extends { [key in infer L]?: infer W }
99
+ // ? ObjectAtom<T[K]>
100
+ // : BaseAtom<T> {
101
+ // const newObs = this._behavior$.pipe(
102
+ // distinctUntilKeyChanged(key),
103
+ // map((k) => k?.[key])
104
+ // );
105
+ // // Can't get typescript to recognize the types here so I'm cheating
106
+ // return Atom(newObs) as unknown as T[K] extends (infer W)[]
107
+ // ? ArrayAtom<W>
108
+ // : T[K] extends { [key in infer L]?: infer W }
109
+ // ? ObjectAtom<T[K]>
110
+ // : BaseAtom<T>;
111
+ // }
112
+ // }
113
+
114
+ class ObjectAtom extends ReadOnlyAtom {
115
+ set(nextKey, nextValue) {
116
+ this._behavior$.next(_extends({}, this._behavior$.getValue(), {
117
+ [nextKey]: nextValue
118
+ }));
119
+ }
120
+ }
121
+
122
+ // catch-all for developers
123
+ // export type AnyAtom<T> = BaseAtom<T> | ArrayAtom<T> | ObjectAtom<T>;
124
+
125
+ // observable type (primitive)
126
+ // observable<array> type
127
+ // observable<object> type
128
+ // array type
129
+ // object type
130
+ // primitive type
131
+ // readonly type
132
+ // function definition
133
+ function Atom(_value, readOnly = false) {
134
+ let atom;
135
+ if (readOnly) {
136
+ atom = new ReadOnlyAtom(_value);
137
+ } else if (Array.isArray(_value)) {
138
+ atom = new ArrayAtom(_value); // For arrays
139
+ } else if (typeof _value === "object" && _value !== null) {
140
+ atom = new ObjectAtom(_value); // For objects
141
+ } else {
142
+ atom = new BaseAtom(_value); // For other types
143
+ }
144
+
145
+ return atom;
47
146
  }
147
+ Atom.combine = (...atoms) => {
148
+ const observable = combineLatest(...atoms.map(a => a._behavior$));
149
+ const newAtom = new ReadOnlyAtom(observable);
150
+ return newAtom;
151
+ };
48
152
 
49
153
  function useAtom(atom) {
50
- const [value, setValue] = useState(atom.getValue());
154
+ const [value, setValue] = useState(atom.value());
51
155
  useEffect(() => {
52
156
  const subscription = atom.subscribe(val => {
53
157
  setValue(val);
@@ -59,28 +163,25 @@ function useAtom(atom) {
59
163
  return value;
60
164
  }
61
165
 
166
+ function useSelectAtom(atom, key) {
167
+ const [value, setValue] = useState(atom.get(key));
168
+ useEffect(() => {
169
+ const subscription = atom.select(key).subscribe(val => {
170
+ setValue(val);
171
+ });
172
+ return () => {
173
+ subscription.unsubscribe();
174
+ };
175
+ }, [atom]);
176
+ return value;
177
+ }
178
+
62
179
  function hydrateAtoms(values) {
63
180
  for (const [atom, value] of values) {
64
- atom.push(value);
181
+ atom._behavior$.next(value);
65
182
  }
66
183
  }
67
184
 
68
- class Signal {
69
- constructor() {
70
- // if it's just a value just use a regular behavior subject
71
- this._subject$ = new Subject();
72
- }
73
- ping(value) {
74
- this._subject$.next(value);
75
- }
76
- subscribe(...params) {
77
- return this._subject$.subscribe(...params);
78
- }
79
-
80
- // not needed?
81
- dispose() {
82
- this._subject$.unsubscribe();
83
- }
84
- }
185
+ function Signal() {}
85
186
 
86
- export { Atom, Signal, hydrateAtoms, useAtom };
187
+ export { Atom, Signal, hydrateAtoms, useAtom, useSelectAtom };
package/dist/useAtom.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { Atom } from "./Atom";
2
- export declare function useAtom<T>(atom: Atom<T>): T;
1
+ import { ReadOnlyAtom } from "./Atom";
2
+ export declare function useAtom<T>(atom: ReadOnlyAtom<T>): T;
3
3
  //# sourceMappingURL=useAtom.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useAtom.d.ts","sourceRoot":"","sources":["../src/useAtom.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAE9B,wBAAgB,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAc3C"}
1
+ {"version":3,"file":"useAtom.d.ts","sourceRoot":"","sources":["../src/useAtom.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEtC,wBAAgB,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAcnD"}
@@ -0,0 +1,5 @@
1
+ import { ObjectAtom } from "./Atom";
2
+ export declare function useSelectAtom<T extends {
3
+ [key in K]: V;
4
+ }, K extends keyof T, V = T[K]>(atom: ObjectAtom<T>, key: K): T[K];
5
+ //# sourceMappingURL=useSelectAtom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useSelectAtom.d.ts","sourceRoot":"","sources":["../src/useSelectAtom.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC,wBAAgB,aAAa,CAC3B,CAAC,SAAS;KACP,GAAG,IAAI,CAAC,GAAG,CAAC;CACd,EACD,CAAC,SAAS,MAAM,CAAC,EACjB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EACR,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAcnC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chem-rx",
3
- "version": "0.0.2",
3
+ "version": "0.0.5",
4
4
  "description": "react state primitives powered by rx.js",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -46,6 +46,7 @@
46
46
  "prebuild": "rimraf dist",
47
47
  "test": "jest",
48
48
  "clean": "rm -rf ./dist",
49
- "build": "npm run prebuild && rollup -c"
49
+ "build": "pnpm run prebuild && rollup -c",
50
+ "bump": "pnpm run build && pnpm version patch"
50
51
  }
51
52
  }
package/src/Atom.ts CHANGED
@@ -14,8 +14,6 @@ export type AtomTuple<T> = {
14
14
  [K in keyof T]: ReadOnlyAtom<T[K]>;
15
15
  };
16
16
 
17
- // export class ReadOnlyAtom<T> {}
18
-
19
17
  export class ReadOnlyAtom<T> {
20
18
  _behavior$: BehaviorSubject<T>;
21
19
 
@@ -140,19 +138,19 @@ export class ReadOnlyAtom<T> {
140
138
  this._fromObservableSubscription?.unsubscribe();
141
139
  }
142
140
 
143
- get(
144
- key: T extends (infer W)[]
145
- ? number
146
- : T extends { [key in keyof T]: infer W }
147
- ? keyof T
148
- : undefined
141
+ get<K extends keyof T>(
142
+ key: K
143
+ // key: T extends (infer W)[]
144
+ // ? number
145
+ // : T extends { [key in keyof T]: infer W }
146
+ // ? keyof T
147
+ // : undefined
149
148
  ): T extends (infer W)[]
150
149
  ? T[number]
151
150
  : T extends { [key in keyof T]: infer W }
152
151
  ? T[keyof T]
153
152
  : undefined {
154
153
  const val = this.value() as T;
155
- // @ts-ignore Can't figure out this type so i'm REALLY cheating
156
154
  return val[key] as T extends (infer W)[]
157
155
  ? T[number]
158
156
  : T extends { [key in keyof T]: infer W }
@@ -1,7 +1,7 @@
1
- import { Atom } from "./Atom";
1
+ import { BaseAtom } from "./Atom";
2
2
 
3
- export function hydrateAtoms(values: readonly [Atom<any>, any][]) {
3
+ export function hydrateAtoms(values: readonly [BaseAtom<any>, any][]) {
4
4
  for (const [atom, value] of values) {
5
- atom.push(value);
5
+ atom._behavior$.next(value);
6
6
  }
7
7
  }
@@ -5,10 +5,10 @@ export function useSelectAtom<
5
5
  T extends {
6
6
  [key in K]: V;
7
7
  },
8
- K extends string | number | symbol = keyof T,
8
+ K extends keyof T,
9
9
  V = T[K]
10
10
  >(atom: ObjectAtom<T>, key: K): T[K] {
11
- const [value, setValue] = useState<T[K]>(atom.get(key));
11
+ const [value, setValue] = useState<T[K]>(atom.get(key)!);
12
12
 
13
13
  useEffect(() => {
14
14
  const subscription = atom.select(key).subscribe((val) => {