chem-rx 0.0.1 → 0.0.3
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/README.md +268 -30
- package/dist/Atom.d.ts +51 -16
- package/dist/Atom.d.ts.map +1 -1
- package/dist/Signal.d.ts +4 -4
- package/dist/Signal.d.ts.map +1 -1
- package/dist/index.cjs.js +199 -69
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.iife.js +199 -69
- package/dist/index.js +134 -32
- package/dist/useAtom.d.ts +2 -2
- package/dist/useAtom.d.ts.map +1 -1
- package/dist/useSelectAtom.d.ts +5 -0
- package/dist/useSelectAtom.d.ts.map +1 -0
- package/package.json +3 -2
- package/src/Atom.ts +122 -22
- package/src/Signal.ts +4 -14
- package/src/index.ts +1 -0
- package/src/useAtom.ts +3 -3
- package/src/useSelectAtom.ts +24 -0
- package/tests/atom.test.ts +242 -19
- package/tests/sample.ts +123 -0
- package/dist/hydrateAtoms.d.ts +0 -3
- package/dist/hydrateAtoms.d.ts.map +0 -1
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
|
-
|
|
7
|
-
|
|
8
|
-
for (var
|
|
9
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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.
|
|
98
|
+
_proto.value = function value() {
|
|
50
99
|
return this._behavior$.getValue();
|
|
51
100
|
}
|
|
52
101
|
|
|
@@ -56,11 +105,128 @@ var Atom = /*#__PURE__*/function () {
|
|
|
56
105
|
var _this$_fromObservable;
|
|
57
106
|
(_this$_fromObservable = this._fromObservableSubscription) == null ? void 0 : _this$_fromObservable.unsubscribe();
|
|
58
107
|
};
|
|
59
|
-
|
|
108
|
+
_proto.get = function get(key) {
|
|
109
|
+
var val = this.value();
|
|
110
|
+
// @ts-ignore Can't figure out this type so i'm REALLY cheating
|
|
111
|
+
return val[key];
|
|
112
|
+
};
|
|
113
|
+
_proto.select = function select(key) {
|
|
114
|
+
var newObs = this._behavior$.pipe(rxjs.distinctUntilKeyChanged(key), rxjs.map(function (k) {
|
|
115
|
+
return k == null ? void 0 : k[key];
|
|
116
|
+
}));
|
|
117
|
+
// Can't get typescript to recognize the types here so I'm cheating
|
|
118
|
+
return Atom(newObs);
|
|
119
|
+
};
|
|
120
|
+
return ReadOnlyAtom;
|
|
60
121
|
}();
|
|
122
|
+
var BaseAtom = /*#__PURE__*/function (_ReadOnlyAtom) {
|
|
123
|
+
_inheritsLoose(BaseAtom, _ReadOnlyAtom);
|
|
124
|
+
function BaseAtom() {
|
|
125
|
+
return _ReadOnlyAtom.apply(this, arguments) || this;
|
|
126
|
+
}
|
|
127
|
+
var _proto2 = BaseAtom.prototype;
|
|
128
|
+
_proto2.set = function set(nextVal) {
|
|
129
|
+
this._behavior$.next(nextVal);
|
|
130
|
+
};
|
|
131
|
+
return BaseAtom;
|
|
132
|
+
}(ReadOnlyAtom);
|
|
133
|
+
var ArrayAtom = /*#__PURE__*/function (_ReadOnlyAtom2) {
|
|
134
|
+
_inheritsLoose(ArrayAtom, _ReadOnlyAtom2);
|
|
135
|
+
function ArrayAtom(initialValue) {
|
|
136
|
+
return _ReadOnlyAtom2.call(this, initialValue) || this;
|
|
137
|
+
}
|
|
138
|
+
var _proto3 = ArrayAtom.prototype;
|
|
139
|
+
_proto3.push = function push(nextVal) {
|
|
140
|
+
this._behavior$.next([].concat(this._behavior$.getValue(), [nextVal]));
|
|
141
|
+
};
|
|
142
|
+
return ArrayAtom;
|
|
143
|
+
}(ReadOnlyAtom);
|
|
144
|
+
//
|
|
145
|
+
// export class ReadOnlyObjectAtom<
|
|
146
|
+
// T extends {
|
|
147
|
+
// [key in K]: V;
|
|
148
|
+
// },
|
|
149
|
+
// K extends string | number | symbol = keyof T,
|
|
150
|
+
// V = T[K]
|
|
151
|
+
// > extends ReadOnlyAtom<T> {
|
|
152
|
+
// get(nextKey: K) {
|
|
153
|
+
// return this.value()[nextKey];
|
|
154
|
+
// }
|
|
155
|
+
//
|
|
156
|
+
// select<K extends keyof T>(
|
|
157
|
+
// key: K
|
|
158
|
+
// ): T[K] extends (infer W)[]
|
|
159
|
+
// ? ArrayAtom<W>
|
|
160
|
+
// : T[K] extends { [key in infer L]?: infer W }
|
|
161
|
+
// ? ObjectAtom<T[K]>
|
|
162
|
+
// : BaseAtom<T> {
|
|
163
|
+
// const newObs = this._behavior$.pipe(
|
|
164
|
+
// distinctUntilKeyChanged(key),
|
|
165
|
+
// map((k) => k?.[key])
|
|
166
|
+
// );
|
|
167
|
+
// // Can't get typescript to recognize the types here so I'm cheating
|
|
168
|
+
// return Atom(newObs) as unknown as T[K] extends (infer W)[]
|
|
169
|
+
// ? ArrayAtom<W>
|
|
170
|
+
// : T[K] extends { [key in infer L]?: infer W }
|
|
171
|
+
// ? ObjectAtom<T[K]>
|
|
172
|
+
// : BaseAtom<T>;
|
|
173
|
+
// }
|
|
174
|
+
// }
|
|
175
|
+
|
|
176
|
+
var ObjectAtom = /*#__PURE__*/function (_ReadOnlyAtom3) {
|
|
177
|
+
_inheritsLoose(ObjectAtom, _ReadOnlyAtom3);
|
|
178
|
+
function ObjectAtom() {
|
|
179
|
+
return _ReadOnlyAtom3.apply(this, arguments) || this;
|
|
180
|
+
}
|
|
181
|
+
var _proto4 = ObjectAtom.prototype;
|
|
182
|
+
_proto4.set = function set(nextKey, nextValue) {
|
|
183
|
+
var _extends2;
|
|
184
|
+
this._behavior$.next(_extends({}, this._behavior$.getValue(), (_extends2 = {}, _extends2[nextKey] = nextValue, _extends2)));
|
|
185
|
+
};
|
|
186
|
+
return ObjectAtom;
|
|
187
|
+
}(ReadOnlyAtom);
|
|
188
|
+
|
|
189
|
+
// catch-all for developers
|
|
190
|
+
// export type AnyAtom<T> = BaseAtom<T> | ArrayAtom<T> | ObjectAtom<T>;
|
|
191
|
+
|
|
192
|
+
// observable type (primitive)
|
|
193
|
+
// observable<array> type
|
|
194
|
+
// observable<object> type
|
|
195
|
+
// array type
|
|
196
|
+
// object type
|
|
197
|
+
// primitive type
|
|
198
|
+
// readonly type
|
|
199
|
+
// function definition
|
|
200
|
+
function Atom(_value, readOnly) {
|
|
201
|
+
if (readOnly === void 0) {
|
|
202
|
+
readOnly = false;
|
|
203
|
+
}
|
|
204
|
+
var atom;
|
|
205
|
+
if (readOnly) {
|
|
206
|
+
atom = new ReadOnlyAtom(_value);
|
|
207
|
+
} else if (Array.isArray(_value)) {
|
|
208
|
+
atom = new ArrayAtom(_value); // For arrays
|
|
209
|
+
} else if (typeof _value === "object" && _value !== null) {
|
|
210
|
+
atom = new ObjectAtom(_value); // For objects
|
|
211
|
+
} else {
|
|
212
|
+
atom = new BaseAtom(_value); // For other types
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return atom;
|
|
216
|
+
}
|
|
217
|
+
Atom.combine = function () {
|
|
218
|
+
for (var _len = arguments.length, atoms = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
219
|
+
atoms[_key] = arguments[_key];
|
|
220
|
+
}
|
|
221
|
+
var observable = rxjs.combineLatest.apply(void 0, atoms.map(function (a) {
|
|
222
|
+
return a._behavior$;
|
|
223
|
+
}));
|
|
224
|
+
var newAtom = new ReadOnlyAtom(observable);
|
|
225
|
+
return newAtom;
|
|
226
|
+
};
|
|
61
227
|
|
|
62
228
|
function useAtom(atom) {
|
|
63
|
-
var _useState = react.useState(atom.
|
|
229
|
+
var _useState = react.useState(atom.value()),
|
|
64
230
|
value = _useState[0],
|
|
65
231
|
setValue = _useState[1];
|
|
66
232
|
react.useEffect(function () {
|
|
@@ -74,36 +240,19 @@ function useAtom(atom) {
|
|
|
74
240
|
return value;
|
|
75
241
|
}
|
|
76
242
|
|
|
77
|
-
function
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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;
|
|
243
|
+
function useSelectAtom(atom, key) {
|
|
244
|
+
var _useState = react.useState(atom.get(key)),
|
|
245
|
+
value = _useState[0],
|
|
246
|
+
setValue = _useState[1];
|
|
247
|
+
react.useEffect(function () {
|
|
248
|
+
var subscription = atom.select(key).subscribe(function (val) {
|
|
249
|
+
setValue(val);
|
|
250
|
+
});
|
|
96
251
|
return function () {
|
|
97
|
-
|
|
98
|
-
done: true
|
|
99
|
-
};
|
|
100
|
-
return {
|
|
101
|
-
done: false,
|
|
102
|
-
value: o[i++]
|
|
103
|
-
};
|
|
252
|
+
subscription.unsubscribe();
|
|
104
253
|
};
|
|
105
|
-
}
|
|
106
|
-
|
|
254
|
+
}, [atom]);
|
|
255
|
+
return value;
|
|
107
256
|
}
|
|
108
257
|
|
|
109
258
|
function hydrateAtoms(values) {
|
|
@@ -115,29 +264,10 @@ function hydrateAtoms(values) {
|
|
|
115
264
|
}
|
|
116
265
|
}
|
|
117
266
|
|
|
118
|
-
|
|
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
|
-
}();
|
|
267
|
+
function Signal() {}
|
|
139
268
|
|
|
140
269
|
exports.Atom = Atom;
|
|
141
270
|
exports.Signal = Signal;
|
|
142
271
|
exports.hydrateAtoms = hydrateAtoms;
|
|
143
272
|
exports.useAtom = useAtom;
|
|
273
|
+
exports.useSelectAtom = useSelectAtom;
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -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"}
|
package/dist/index.iife.js
CHANGED
|
@@ -1,17 +1,66 @@
|
|
|
1
1
|
var chemicalRx = (function (exports, rxjs, react) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
for (var
|
|
7
|
-
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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.
|
|
96
|
+
_proto.value = function value() {
|
|
48
97
|
return this._behavior$.getValue();
|
|
49
98
|
}
|
|
50
99
|
|
|
@@ -54,11 +103,128 @@ 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
|
-
|
|
106
|
+
_proto.get = function get(key) {
|
|
107
|
+
var val = this.value();
|
|
108
|
+
// @ts-ignore Can't figure out this type so i'm REALLY cheating
|
|
109
|
+
return val[key];
|
|
110
|
+
};
|
|
111
|
+
_proto.select = function select(key) {
|
|
112
|
+
var newObs = this._behavior$.pipe(rxjs.distinctUntilKeyChanged(key), rxjs.map(function (k) {
|
|
113
|
+
return k == null ? void 0 : k[key];
|
|
114
|
+
}));
|
|
115
|
+
// Can't get typescript to recognize the types here so I'm cheating
|
|
116
|
+
return Atom(newObs);
|
|
117
|
+
};
|
|
118
|
+
return ReadOnlyAtom;
|
|
58
119
|
}();
|
|
120
|
+
var BaseAtom = /*#__PURE__*/function (_ReadOnlyAtom) {
|
|
121
|
+
_inheritsLoose(BaseAtom, _ReadOnlyAtom);
|
|
122
|
+
function BaseAtom() {
|
|
123
|
+
return _ReadOnlyAtom.apply(this, arguments) || this;
|
|
124
|
+
}
|
|
125
|
+
var _proto2 = BaseAtom.prototype;
|
|
126
|
+
_proto2.set = function set(nextVal) {
|
|
127
|
+
this._behavior$.next(nextVal);
|
|
128
|
+
};
|
|
129
|
+
return BaseAtom;
|
|
130
|
+
}(ReadOnlyAtom);
|
|
131
|
+
var ArrayAtom = /*#__PURE__*/function (_ReadOnlyAtom2) {
|
|
132
|
+
_inheritsLoose(ArrayAtom, _ReadOnlyAtom2);
|
|
133
|
+
function ArrayAtom(initialValue) {
|
|
134
|
+
return _ReadOnlyAtom2.call(this, initialValue) || this;
|
|
135
|
+
}
|
|
136
|
+
var _proto3 = ArrayAtom.prototype;
|
|
137
|
+
_proto3.push = function push(nextVal) {
|
|
138
|
+
this._behavior$.next([].concat(this._behavior$.getValue(), [nextVal]));
|
|
139
|
+
};
|
|
140
|
+
return ArrayAtom;
|
|
141
|
+
}(ReadOnlyAtom);
|
|
142
|
+
//
|
|
143
|
+
// export class ReadOnlyObjectAtom<
|
|
144
|
+
// T extends {
|
|
145
|
+
// [key in K]: V;
|
|
146
|
+
// },
|
|
147
|
+
// K extends string | number | symbol = keyof T,
|
|
148
|
+
// V = T[K]
|
|
149
|
+
// > extends ReadOnlyAtom<T> {
|
|
150
|
+
// get(nextKey: K) {
|
|
151
|
+
// return this.value()[nextKey];
|
|
152
|
+
// }
|
|
153
|
+
//
|
|
154
|
+
// select<K extends keyof T>(
|
|
155
|
+
// key: K
|
|
156
|
+
// ): T[K] extends (infer W)[]
|
|
157
|
+
// ? ArrayAtom<W>
|
|
158
|
+
// : T[K] extends { [key in infer L]?: infer W }
|
|
159
|
+
// ? ObjectAtom<T[K]>
|
|
160
|
+
// : BaseAtom<T> {
|
|
161
|
+
// const newObs = this._behavior$.pipe(
|
|
162
|
+
// distinctUntilKeyChanged(key),
|
|
163
|
+
// map((k) => k?.[key])
|
|
164
|
+
// );
|
|
165
|
+
// // Can't get typescript to recognize the types here so I'm cheating
|
|
166
|
+
// return Atom(newObs) as unknown as T[K] extends (infer W)[]
|
|
167
|
+
// ? ArrayAtom<W>
|
|
168
|
+
// : T[K] extends { [key in infer L]?: infer W }
|
|
169
|
+
// ? ObjectAtom<T[K]>
|
|
170
|
+
// : BaseAtom<T>;
|
|
171
|
+
// }
|
|
172
|
+
// }
|
|
173
|
+
|
|
174
|
+
var ObjectAtom = /*#__PURE__*/function (_ReadOnlyAtom3) {
|
|
175
|
+
_inheritsLoose(ObjectAtom, _ReadOnlyAtom3);
|
|
176
|
+
function ObjectAtom() {
|
|
177
|
+
return _ReadOnlyAtom3.apply(this, arguments) || this;
|
|
178
|
+
}
|
|
179
|
+
var _proto4 = ObjectAtom.prototype;
|
|
180
|
+
_proto4.set = function set(nextKey, nextValue) {
|
|
181
|
+
var _extends2;
|
|
182
|
+
this._behavior$.next(_extends({}, this._behavior$.getValue(), (_extends2 = {}, _extends2[nextKey] = nextValue, _extends2)));
|
|
183
|
+
};
|
|
184
|
+
return ObjectAtom;
|
|
185
|
+
}(ReadOnlyAtom);
|
|
186
|
+
|
|
187
|
+
// catch-all for developers
|
|
188
|
+
// export type AnyAtom<T> = BaseAtom<T> | ArrayAtom<T> | ObjectAtom<T>;
|
|
189
|
+
|
|
190
|
+
// observable type (primitive)
|
|
191
|
+
// observable<array> type
|
|
192
|
+
// observable<object> type
|
|
193
|
+
// array type
|
|
194
|
+
// object type
|
|
195
|
+
// primitive type
|
|
196
|
+
// readonly type
|
|
197
|
+
// function definition
|
|
198
|
+
function Atom(_value, readOnly) {
|
|
199
|
+
if (readOnly === void 0) {
|
|
200
|
+
readOnly = false;
|
|
201
|
+
}
|
|
202
|
+
var atom;
|
|
203
|
+
if (readOnly) {
|
|
204
|
+
atom = new ReadOnlyAtom(_value);
|
|
205
|
+
} else if (Array.isArray(_value)) {
|
|
206
|
+
atom = new ArrayAtom(_value); // For arrays
|
|
207
|
+
} else if (typeof _value === "object" && _value !== null) {
|
|
208
|
+
atom = new ObjectAtom(_value); // For objects
|
|
209
|
+
} else {
|
|
210
|
+
atom = new BaseAtom(_value); // For other types
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return atom;
|
|
214
|
+
}
|
|
215
|
+
Atom.combine = function () {
|
|
216
|
+
for (var _len = arguments.length, atoms = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
217
|
+
atoms[_key] = arguments[_key];
|
|
218
|
+
}
|
|
219
|
+
var observable = rxjs.combineLatest.apply(void 0, atoms.map(function (a) {
|
|
220
|
+
return a._behavior$;
|
|
221
|
+
}));
|
|
222
|
+
var newAtom = new ReadOnlyAtom(observable);
|
|
223
|
+
return newAtom;
|
|
224
|
+
};
|
|
59
225
|
|
|
60
226
|
function useAtom(atom) {
|
|
61
|
-
var _useState = react.useState(atom.
|
|
227
|
+
var _useState = react.useState(atom.value()),
|
|
62
228
|
value = _useState[0],
|
|
63
229
|
setValue = _useState[1];
|
|
64
230
|
react.useEffect(function () {
|
|
@@ -72,36 +238,19 @@ var chemicalRx = (function (exports, rxjs, react) {
|
|
|
72
238
|
return value;
|
|
73
239
|
}
|
|
74
240
|
|
|
75
|
-
function
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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;
|
|
241
|
+
function useSelectAtom(atom, key) {
|
|
242
|
+
var _useState = react.useState(atom.get(key)),
|
|
243
|
+
value = _useState[0],
|
|
244
|
+
setValue = _useState[1];
|
|
245
|
+
react.useEffect(function () {
|
|
246
|
+
var subscription = atom.select(key).subscribe(function (val) {
|
|
247
|
+
setValue(val);
|
|
248
|
+
});
|
|
94
249
|
return function () {
|
|
95
|
-
|
|
96
|
-
done: true
|
|
97
|
-
};
|
|
98
|
-
return {
|
|
99
|
-
done: false,
|
|
100
|
-
value: o[i++]
|
|
101
|
-
};
|
|
250
|
+
subscription.unsubscribe();
|
|
102
251
|
};
|
|
103
|
-
}
|
|
104
|
-
|
|
252
|
+
}, [atom]);
|
|
253
|
+
return value;
|
|
105
254
|
}
|
|
106
255
|
|
|
107
256
|
function hydrateAtoms(values) {
|
|
@@ -113,32 +262,13 @@ var chemicalRx = (function (exports, rxjs, react) {
|
|
|
113
262
|
}
|
|
114
263
|
}
|
|
115
264
|
|
|
116
|
-
|
|
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
|
-
}();
|
|
265
|
+
function Signal() {}
|
|
137
266
|
|
|
138
267
|
exports.Atom = Atom;
|
|
139
268
|
exports.Signal = Signal;
|
|
140
269
|
exports.hydrateAtoms = hydrateAtoms;
|
|
141
270
|
exports.useAtom = useAtom;
|
|
271
|
+
exports.useSelectAtom = useSelectAtom;
|
|
142
272
|
|
|
143
273
|
return exports;
|
|
144
274
|
|