@exodus/atoms 2.5.1 → 2.7.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/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # `@exodus/atoms`
2
+
3
+ Module to store and manage wallet accounts instances.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ yarn add @exodus/atoms
9
+ ```
10
+
11
+ ## What is an atom?
12
+
13
+ An atom is a data source wrapper that exposes a single piece of data through 3 different methods:
14
+
15
+ - **get()**: read data
16
+ - **set(newValue)**: write data
17
+ - **observe(async (data) => {})**: observes data changes. Will be called initially with current data value. Observers are awaited in series.
18
+
19
+ ## Data sources
20
+
21
+ This library provides helpers for creating atoms from multiple data sources we use in our apps
22
+
23
+ | | get | set | observe |
24
+ | ------------- | --- | ----- | ------- |
25
+ | Memory | ✅ | ✅ | ✅ |
26
+ | Storage | ✅ | 🟡 \* | ✅ |
27
+ | Fusion | ✅ | ✅ | ✅ |
28
+ | Remote config | ✅ | ❌ | ✅ |
29
+ | Local config | ✅ | ✅ | ✅ |
30
+ | Event emitter | ✅ | ❌ | ✅ |
31
+
32
+ \* A storage atom needs a special `isSoleWriter` param to allow write access. This is because storage instances can overlap, e.g. a parent namespace can mutate a child namespace, and our [storage-spec](https://github.com/ExodusMovement/exodus-hydra/tree/master/modules/storage-spec) doesn't currently provide for detecting changes across those instances.
33
+
34
+ ## Usage
35
+
36
+ ```js
37
+ import {
38
+ createInMemoryAtom,
39
+ createStorageAtomFactory,
40
+ createFusionAtomFactory,
41
+ createRemoteConfigAtomFactory,
42
+ fromEventEmitter,
43
+ } from '@exodus/atoms'
44
+
45
+ // In memory atoms
46
+ const availableAssetNamesAtom = createInMemoryAtom({
47
+ defaultValue: {},
48
+ })
49
+
50
+ // Storage atoms
51
+ const storageAtomFactory = createStorageAtomFactory({ storage })
52
+
53
+ const acceptedTermsAtom = storageAtomFactory({
54
+ key: 'acceptedTerms',
55
+ defaultValue: false,
56
+ isSoleWriter: true,
57
+ })
58
+
59
+ // Fusion atoms
60
+ const fusionAtomFactory = createFusionAtomFactory({ fusion })
61
+
62
+ const shareActivityAtom = createFusionAtom({
63
+ path: 'shareActivity',
64
+ defaultValue: true,
65
+ })
66
+
67
+ // Remote config atoms
68
+ const createRemoteConfigAtom = createRemoteConfigAtomFactory({ remoteConfig })
69
+
70
+ const fiatOnrampConfigAtom = createRemoteConfigAtom({
71
+ path: `dapps.fiatOnramp`,
72
+ defaultValue: {},
73
+ })
74
+
75
+ // Event emitter
76
+ const geolocationAtom = fromEventEmitter({
77
+ emitter: geolocation,
78
+ event: 'geolocation',
79
+ get: geolocation.get,
80
+ })
81
+ ```
82
+
83
+ ## Helper functions
84
+
85
+ ### compute({ atom, selector })
86
+
87
+ Computes an atom from another by applying a selector function to the observed data source. Returned atom is read-only, i.e. **set** will fail.
88
+
89
+ ### withSerialization({ atom, serialize, deserialize })
90
+
91
+ Computes an atom from another by serializing it's data after reading it and deserializing it before writing it.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/atoms",
3
- "version": "2.5.1",
3
+ "version": "2.7.0",
4
4
  "main": "src/index.js",
5
5
  "author": "Exodus Movement Inc.",
6
6
  "scripts": {
@@ -23,6 +23,7 @@
23
23
  "dependencies": {
24
24
  "lodash": "^4.17.21",
25
25
  "make-concurrent": ">=4 <6",
26
+ "p-defer": "^4.0.0",
26
27
  "proxy-freeze": "^1.0.0"
27
28
  },
28
29
  "devDependencies": {
@@ -30,5 +31,5 @@
30
31
  "delay": "^5.0.0",
31
32
  "jest": "^29.1.2"
32
33
  },
33
- "gitHead": "791cafc56909b0eb584a1e6526dc2cbd3f6fd235"
34
+ "gitHead": "876d717e70f271148807b5c13eb607d154169717"
34
35
  }
@@ -0,0 +1,17 @@
1
+ import { compute } from '../index'
2
+
3
+ const createDisabledAssetsAtom = ({ enabledAndDisabledAssetsAtom }) => {
4
+ const selector = (values) =>
5
+ Object.fromEntries(
6
+ Object.keys(values.disabled)
7
+ .filter((assetName) => {
8
+ const disabled = values.disabled[assetName]
9
+ return disabled === true
10
+ })
11
+ .map((assetName) => [assetName, true])
12
+ )
13
+
14
+ return compute({ atom: enabledAndDisabledAssetsAtom, selector })
15
+ }
16
+
17
+ export default createDisabledAssetsAtom
@@ -1,16 +1,27 @@
1
1
  import { EventEmitter } from 'events'
2
2
 
3
3
  import fromEventEmitter from '../event-emitter'
4
+ import pDefer from 'p-defer'
5
+
6
+ const createAtomMock = (options = {}) => {
7
+ const { defaultValue } = options
4
8
 
5
- const createAtomMock = ({ defaultValue }) => {
6
9
  let latestValue = defaultValue
7
10
 
8
11
  const emitter = new EventEmitter()
9
12
 
10
- const get = async () => latestValue
13
+ const initialized = pDefer()
14
+
15
+ const get = async () => {
16
+ if (!('defaultValue' in options)) {
17
+ await initialized.promise
18
+ }
19
+ return latestValue
20
+ }
11
21
 
12
22
  const set = (data) => {
13
23
  latestValue = data
24
+ initialized.resolve()
14
25
  emitter.emit('data', data)
15
26
  }
16
27
 
package/src/index.js CHANGED
@@ -5,6 +5,7 @@ export { default as createStorageAtomFactory } from './factories/storage'
5
5
  export { default as createLocalConfigAtomFactory } from './factories/local-config'
6
6
  export { default as createRemoteConfigAtomFactory } from './factories/remote-config'
7
7
  export { default as createEnabledAssetsAtom } from './factories/enabled-assets'
8
+ export { default as createDisabledAssetsAtom } from './factories/disabled-assets'
8
9
  export { default as fromEventEmitter } from './event-emitter'
9
10
  export { default as compute } from './enhancers/compute'
10
11
  export { default as difference } from './enhancers/difference'