chem-rx 0.0.17 → 0.0.18

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 CHANGED
@@ -9,55 +9,88 @@ simplicity. Useable with or without React!
9
9
  [jotai](https://github.com/pmndrs/jotai) or
10
10
  [Recoil](https://github.com/facebookexperimental/Recoil).
11
11
 
12
- Atoms are state containers that take any value - object, array, or primitive.
12
+ Atoms are state containers that take any value - object, array, or primitive. You can create them by simply passing a value into `Atom`.
13
13
 
14
14
  ```
15
15
  import { Atom } from 'chem-rx'
16
16
 
17
+ const data$: BaseAtom = Atom('hello')
18
+ ```
19
+
20
+ ## Primitives
21
+
22
+ There are five primitives in `chem-rx`:
23
+
24
+ 1. BaseAtom
25
+ 2. ArrayAtom
26
+ 3. NullableAtom
27
+ 4. ReadOnlyAtom
28
+ 5. Signal
29
+
30
+ Their traits are self-explanatory, and they are generally automatically created for you, depending on how you create your Atom.
31
+
32
+ In its simplest form, `chem-rx` can be used with BaseAtom and ArrayAtom, giving you a primitive for managing atomic data, but can be composed and split in numerous ways for more advanced use cases.
33
+
34
+ ### BaseAtom & ArrayAtom
35
+
36
+ `BaseAtom` is the fundamental type that everything else extends. It contains the primary functionality for interacting with your Atom data.
37
+
38
+ `ArrayAtom` is exactly as it sounds - an atom that holds an array of values (as opposed to an individual)
39
+
40
+ `Atom` will automatically return you an ArrayAtom or BaseAtom based on what you pass it.
41
+
42
+ ```
17
43
  const number$: BaseAtom = Atom(0)
18
- const string$: BaseAtom = Atom('hello')
19
- const array$: ArrayAtom = Atom(['hello', 'world'])
20
- const object$: ObjectAtom = Atom({ 'hello': 'world', 'world': 'hello' })
44
+ const string$: BaseAtom<string> = Atom('hello')
45
+
46
+ // You can skip the type hint on your variable.
47
+ // This returns a `BaseAtom<{hello: string, world: string}>`
48
+ const object$ = Atom({ 'hello': 'world', 'world': 'hello' })
49
+
50
+ // You can enforce a generic type on your atoms
51
+ // Note the ArrayAtom's generic type holds the item type held in the array.
52
+ const array$: ArrayAtom<string> = Atom<string[]>(['hello', 'world'])
21
53
  ```
22
54
 
23
55
  ### Getting & setting values
24
56
 
25
- `Atom` will automatically return an instance of `BaseAtom`, `ArrayAtom`, or
26
- `ObjectAtom` depending on the input.
57
+ `BaseAtom` offers simple helpers to access and modify your data.
27
58
 
28
59
  ```
29
- // Base Atom
30
- number$.set(2)
31
- number$.value()
32
- // 2
60
+ // Primitive values (BaseAtom)
61
+ number$.next(2)
62
+ number$.value() // 2
63
+
64
+
65
+ // Object values (BaseAtom)
66
+ object$.get('hello') // 'world'
67
+ object$.get('fakeKey') // undefined
68
+ object$.set('hello', 'werld')
69
+ object$.get('hello') // 'werld'
70
+
33
71
 
34
72
  // ArrayAtom
35
73
  array$.push('!')
36
- array$.value()
37
- // ['hello', 'world', '!']
38
- array$.get(1)
39
- // 'world'
74
+ array$.value() // ['hello', 'world', '!']
75
+ array$.get(2) // '!'
76
+ array$.get(3) // undefined
77
+ ```
40
78
 
41
- // ObjectAtom
42
- object$.set('world', 'hi')
43
- object$.value()
44
- // {'hello': 'world', 'world': 'hi'}
79
+ ## Composability & ReadOnlyAtom's
45
80
 
46
- object$.set('sup', 'earth')
47
- // {'hello': 'world', 'world': 'hi', 'sup': 'earth'}
81
+ Atoms are intended to be easily composed, split, and transformed to handle complex data needs through a simple API.
48
82
 
49
- object$.get('world')
50
- // 'hi'
51
- ```
83
+ ### Selecting Atoms (read-only)
84
+
85
+ You can `select` keys on `BaseAtom` and `ArrayAtom` that returns an Atom that wrap the values
86
+ at that key. Any time the original atom changes, your selected atom will automatically update with the latest value.
52
87
 
53
- ### Selecting Atoms
88
+ This can be especially useful for working with different parts of nested Array and Object atoms.
54
89
 
55
- You can select Object and Array atoms to return new Atoms that wrap the values
56
- at that key. This can be useful for working with different parts of nested Array
57
- and Object atoms.
90
+ Atoms created with `select` are **read-only** (`ReadOnlyAtom`). This prevents you from modifying original values that the atom was created from.
58
91
 
59
92
  ```
60
- const nestedData = Atom({
93
+ const students = Atom({
61
94
  stacy: {
62
95
  nickname: "stace",
63
96
  education: {
@@ -67,40 +100,48 @@ const nestedData = Atom({
67
100
  },
68
101
  });
69
102
 
70
- const stacy = nestedData.select("stacy");
71
- console.log(stacy.get('nickname'))
72
- // 'stace'
103
+ // ReadOnlyAtom<{nickname: string, education: ...}>
104
+ const stacy = students.select("stacy");
105
+ const stacySchool = stacy.select("education");
106
+
107
+ stacy.get('nickname') // 'stace'
108
+ stacySchool.get('graduation') // 2014
109
+
110
+ students.set("stacy", {
111
+ nickname: "spacey",
112
+ education: {
113
+ ...students.get("stacy").education,
114
+ graduation: 2015,
115
+ },
116
+ });
117
+
118
+ stacy.get('nickname') // 'spacey'
119
+ stacySchool.get('graduation') // 2015
120
+
121
+ // ERR: Property 'set' does not exist on type ReadOnlyAtom
122
+ stacy.set('nickname', 'stacy')
73
123
 
74
- const stacySchool = nestedData.select("stacy").select("education");
75
- console.log(stacySchool.get('school'))
76
- // 'Penn'
77
124
  ```
78
125
 
79
126
  ### Derived Atoms (read-only)
80
127
 
81
128
  You can derive new Atoms from any existing atoms. Any time the original atoms
82
- change, your derived atoms will automatically update with new values!
129
+ change, your derived atoms will automatically update with new values.
130
+
131
+ Every derived atom is **read-only**. This prevents you from overriding the
132
+ derived output value, since it is automatically derived from another input.
83
133
 
84
134
  ```
85
135
  const atom$ = Atom(3);
86
136
 
87
- // square it
88
137
  const squared$ = atom$.derive((x) => x * x);
89
138
 
90
- // "9"
91
- console.log(squared$.value())
139
+ squared$.value() // "9"
92
140
 
93
- // Update the original value
94
141
  atom$.set(4)
95
142
 
96
- // "16"
97
- console.log(squared$.value())
98
- ```
99
-
100
- Every derived atom is **read-only**. This prevents you from overriding the
101
- derived output value, since it is automatically derived from another input!
143
+ squared$.value() // "16"
102
144
 
103
- ```
104
145
  // ERR: Property 'set' does not exist on type ReadOnlyAtom
105
146
  squared$.set(2)
106
147
  ```
@@ -116,7 +157,7 @@ atom$.set(2)
116
157
 
117
158
  ### Combining Atoms
118
159
 
119
- Multiple atoms can also be **combined** to create brand new atoms!
160
+ Multiple atoms can also be **combined** to create brand new atoms.
120
161
 
121
162
  Here's an example of joining a set of normalized data models
122
163
 
@@ -150,6 +191,8 @@ console.log(mary$.select('pets').value())
150
191
  */
151
192
  ```
152
193
 
194
+ ## Pub/Sub
195
+
153
196
  ### Subscribing to updates
154
197
 
155
198
  Atoms emit values each time they're updated. You can subscribe callbacks to them
@@ -162,8 +205,7 @@ const subscription = atom$.subscribe(val => {
162
205
  console.log("Received value: ", val)
163
206
  })
164
207
 
165
- atom$.set(4)
166
- // "Received value: 4"
208
+ atom$.set(4) // "Received value: 4"
167
209
 
168
210
  // Unsubscribe to clean up
169
211
  subscription.unsubscribe();
@@ -181,8 +223,7 @@ const subscription = signal$.subscribe(() => {
181
223
  console.log("PONG")
182
224
  })
183
225
 
184
- signal$.ping()
185
- // "PONG"
226
+ signal$.ping() // "PONG"
186
227
 
187
228
  // Unsubscribe to clean up
188
229
  subscription.unsubscribe();
@@ -210,8 +251,7 @@ subscription.unsubscribe();
210
251
 
211
252
  `useAtom` automatically updates with new values in your react components.
212
253
 
213
- If you want to update your atoms, you can simply call the same `set`
214
- (Base/ObjectAtom) or `push` (ArrayAtom) methods you would typically use outside
254
+ If you want to update your atoms, you can simply call the same `next`, `set`, or `push` (ArrayAtom) methods you would typically use outside
215
255
  of react.
216
256
 
217
257
  ```
@@ -268,22 +308,17 @@ const CounterPage = ({ countFromServer }) => {
268
308
 
269
309
  There are several suggested "patterns" when using Atoms:
270
310
 
271
- 1. Suffix all atoms with `$` (for readability).
272
- 2. Keep all data management **outside** of your views (e.g, React)
273
- 3. Avoid using `set` and `push` directly from your client components. Instead,
274
- create helper functions (actions)
275
- 4. Name your helper actions as `<atomName>$<actionName>`, to easily see what
276
- atoms are involved.
277
- 5. Name your derived atoms as `<baseAtom>_<derivedValue>$` to easily see which
278
- atoms it derives from.
279
-
280
- ## Common Issues
281
-
282
- Here are some common issues you might run into when starting out.
283
-
284
311
  1. Keep your atoms in separate files to prevent circular dependencies.
285
312
  1. I typically create a new file for every action, so I can easily see the
286
313
  API surface at a glance
314
+ 2. Suffix all atoms with `$` (for readability).
315
+ 3. Keep all data management **outside** of your views (e.g, React)
316
+ 4. Avoid updating atoms (`next`, `set`, and `push`) inside your client components. Instead,
317
+ create an API of helper functions (actions) and call them.
318
+ 5. Name your helper actions as `<atomName>$<actionName>`, to easily see what
319
+ atoms are involved.
320
+ 6. Name your derived atoms as `<baseAtom>_<derivedValue>$` to easily see which
321
+ atoms it derives from.
287
322
 
288
323
  ## Advanced Usage with `rxjs`
289
324
 
@@ -313,6 +348,4 @@ console.log(squared$.value());
313
348
 
314
349
  ## Why...?
315
350
 
316
- This library spawned out of a love for the flexibility and expressiveness of
317
- [rxjs](https://github.com/ReactiveX/rxjs) Observables, and the simplicity of
318
- atomic libraries like [jotai](https://github.com/pmndrs/jotai).
351
+ [`rxjs`](https://github.com/ReactiveX/rxjs) is awesome, and I wanted a framework-agnostic [jotai](https://github.com/pmndrs/jotai)-like solution with a simpler API.
package/dist/index.cjs.js CHANGED
@@ -323,12 +323,12 @@ function useSelectAtom(atom, key) {
323
323
  }
324
324
 
325
325
  var hydratedAtomsSet = new WeakSet();
326
- function hydrateAtoms(values) {
326
+ function hydrateAtoms(values, options) {
327
327
  for (var _iterator = _createForOfIteratorHelperLoose(values), _step; !(_step = _iterator()).done;) {
328
328
  var _step$value = _step.value,
329
329
  atom = _step$value[0],
330
330
  value = _step$value[1];
331
- if (!hydratedAtomsSet.has(atom)) {
331
+ if (!hydratedAtomsSet.has(atom) || options != null && options.force) {
332
332
  hydratedAtomsSet.add(atom);
333
333
  atom._behavior$.next(value);
334
334
  }
@@ -321,12 +321,12 @@ var chemicalRx = (function (exports, rxjs, react) {
321
321
  }
322
322
 
323
323
  var hydratedAtomsSet = new WeakSet();
324
- function hydrateAtoms(values) {
324
+ function hydrateAtoms(values, options) {
325
325
  for (var _iterator = _createForOfIteratorHelperLoose(values), _step; !(_step = _iterator()).done;) {
326
326
  var _step$value = _step.value,
327
327
  atom = _step$value[0],
328
328
  value = _step$value[1];
329
- if (!hydratedAtomsSet.has(atom)) {
329
+ if (!hydratedAtomsSet.has(atom) || options != null && options.force) {
330
330
  hydratedAtomsSet.add(atom);
331
331
  atom._behavior$.next(value);
332
332
  }
package/dist/index.js CHANGED
@@ -239,9 +239,9 @@ function useSelectAtom(atom, key) {
239
239
  }
240
240
 
241
241
  const hydratedAtomsSet = new WeakSet();
242
- function hydrateAtoms(values) {
242
+ function hydrateAtoms(values, options) {
243
243
  for (const [atom, value] of values) {
244
- if (!hydratedAtomsSet.has(atom)) {
244
+ if (!hydratedAtomsSet.has(atom) || options != null && options.force) {
245
245
  hydratedAtomsSet.add(atom);
246
246
  atom._behavior$.next(value);
247
247
  }
@@ -1,3 +1,5 @@
1
1
  import { BaseAtom, NullableBaseAtom } from "./Atom";
2
- export declare function hydrateAtoms(values: readonly [NullableBaseAtom<any> | BaseAtom<any>, any][]): void;
2
+ export declare function hydrateAtoms(values: readonly [NullableBaseAtom<any> | BaseAtom<any>, any][], options?: {
3
+ force?: boolean;
4
+ }): void;
3
5
  //# sourceMappingURL=useHydrateAtoms.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useHydrateAtoms.d.ts","sourceRoot":"","sources":["../src/useHydrateAtoms.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAgB,MAAM,QAAQ,CAAC;AAIlE,wBAAgB,YAAY,CAC1B,MAAM,EAAE,SAAS,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,EAAE,QAQhE"}
1
+ {"version":3,"file":"useHydrateAtoms.d.ts","sourceRoot":"","sources":["../src/useHydrateAtoms.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAgB,MAAM,QAAQ,CAAC;AAIlE,wBAAgB,YAAY,CAC1B,MAAM,EAAE,SAAS,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,EAAE,EAC/D,OAAO,CAAC,EAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,QAQ9B"}
@@ -1 +1 @@
1
- {"version":3,"file":"useSelectAtom.d.ts","sourceRoot":"","sources":["../src/useSelectAtom.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAElE,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,gBAAgB,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAczE"}
1
+ {"version":3,"file":"useSelectAtom.d.ts","sourceRoot":"","sources":["../src/useSelectAtom.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAElE,wBAAgB,aAAa,CAC3B,CAAC,SACG;KACG,GAAG,IAAI,CAAC,GAAG,CAAC;CACd,EACL,CAAC,SAAS,MAAM,CAAC,EACjB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EACR,IAAI,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAczE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chem-rx",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "react state primitives powered by rx.js",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -4,10 +4,11 @@ import { BaseAtom, NullableBaseAtom, ReadOnlyAtom } from "./Atom";
4
4
  const hydratedAtomsSet: WeakSet<ReadOnlyAtom<any>> = new WeakSet();
5
5
 
6
6
  export function hydrateAtoms(
7
- values: readonly [NullableBaseAtom<any> | BaseAtom<any>, any][]
7
+ values: readonly [NullableBaseAtom<any> | BaseAtom<any>, any][],
8
+ options?: { force?: boolean }
8
9
  ) {
9
10
  for (const [atom, value] of values) {
10
- if (!hydratedAtomsSet.has(atom)) {
11
+ if (!hydratedAtomsSet.has(atom) || options?.force) {
11
12
  hydratedAtomsSet.add(atom);
12
13
  atom._behavior$.next(value);
13
14
  }
@@ -2,9 +2,10 @@ import { useEffect, useState } from "react";
2
2
  import { BaseAtom, NullableBaseAtom, ReadOnlyAtom } from "./Atom";
3
3
 
4
4
  export function useSelectAtom<
5
- T extends {
6
- [key in K]: V;
7
- },
5
+ T extends
6
+ | {
7
+ [key in K]: V;
8
+ },
8
9
  K extends keyof T,
9
10
  V = T[K]
10
11
  >(atom: NullableBaseAtom<T> | BaseAtom<T> | ReadOnlyAtom<T>, key: K): T[K] {
@@ -171,6 +171,8 @@ test("Array Atom get index test", () => {
171
171
  atom.push("second");
172
172
  expect(atom.value().length).toBe(2);
173
173
  expect(atom.get(1)).toBe("second");
174
+
175
+ expect(atom.get(2)).toBe(undefined);
174
176
  });
175
177
 
176
178
  test("Test native pipe", () => {
@@ -415,6 +417,58 @@ test("Test select (nested objects)", () => {
415
417
  expect(stacySchool.get("graduation")).toBe(2014);
416
418
  });
417
419
 
420
+ test("Test parent value when updating child objects ", () => {
421
+ const students = Atom<{
422
+ [key: string]: {
423
+ nickname: string;
424
+ education: {
425
+ school: string;
426
+ graduation: number;
427
+ };
428
+ };
429
+ }>({
430
+ stacy: {
431
+ nickname: "stace",
432
+ education: {
433
+ school: "Penn",
434
+ graduation: 2014,
435
+ },
436
+ },
437
+ annie: {
438
+ nickname: "ann",
439
+ education: {
440
+ school: "Brown",
441
+ graduation: 2015,
442
+ },
443
+ },
444
+ prabhu: {
445
+ nickname: "prab",
446
+ education: {
447
+ school: "MIT",
448
+ graduation: 2016,
449
+ },
450
+ },
451
+ });
452
+ const stacy = students.select("stacy");
453
+ const stacySchool = stacy.select("education");
454
+
455
+ expect(stacy.get("nickname")).toBe("stace");
456
+ expect(students.get("stacy").nickname).toBe("stace");
457
+ expect(stacySchool.get("graduation")).toBe(2014);
458
+
459
+ students.set("stacy", {
460
+ nickname: "spacey",
461
+ education: {
462
+ ...students.get("stacy").education,
463
+ graduation: 2015,
464
+ },
465
+ });
466
+
467
+ expect(stacy.get("nickname")).toBe("spacey");
468
+ expect(students.get("stacy").nickname).toBe("spacey");
469
+ expect(stacySchool.get("graduation")).toBe(2015);
470
+ });
471
+
418
472
  test("Test nullable object", () => {
419
473
  const nestedData = Atom<{
420
474
  [key: string]: {