@exodus/atoms 3.4.0 → 3.5.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/package.json +3 -2
- package/src/countdown-lock.js +12 -5
- package/src/enhancers/combine.js +2 -2
- package/src/factories/sequenced-keystore.js +108 -0
- package/src/index.js +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/atoms",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.0",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"author": "Exodus Movement Inc.",
|
|
6
6
|
"scripts": {
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"events": "^3.3.0",
|
|
25
25
|
"lodash": "^4.17.21",
|
|
26
26
|
"make-concurrent": ">=4 <6",
|
|
27
|
+
"minimalistic-assert": "^1.0.1",
|
|
27
28
|
"p-defer": "^4.0.0",
|
|
28
29
|
"proxy-freeze": "^1.0.0"
|
|
29
30
|
},
|
|
@@ -33,5 +34,5 @@
|
|
|
33
34
|
"eslint": "^8.33.0",
|
|
34
35
|
"jest": "^29.1.2"
|
|
35
36
|
},
|
|
36
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "913199891d798e6fd2970ffda30b2a0ddf5e397c"
|
|
37
38
|
}
|
package/src/countdown-lock.js
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import pDefer from 'p-defer'
|
|
2
2
|
|
|
3
|
-
const createCountdownLock = (
|
|
4
|
-
if (
|
|
3
|
+
const createCountdownLock = (keys) => {
|
|
4
|
+
if (!Array.isArray(keys)) throw new Error('lock keys must be an array')
|
|
5
|
+
|
|
6
|
+
for (const key of keys) {
|
|
7
|
+
if (typeof key !== 'string') throw new Error(`lock keys must be all strings. Invalid ${key}`)
|
|
8
|
+
}
|
|
5
9
|
|
|
6
10
|
const deferred = pDefer()
|
|
11
|
+
|
|
12
|
+
const unlockedKeys = []
|
|
13
|
+
|
|
7
14
|
return {
|
|
8
15
|
promise: deferred.promise,
|
|
9
|
-
unlock: () => {
|
|
10
|
-
if (
|
|
16
|
+
unlock: (key) => {
|
|
17
|
+
if (!unlockedKeys.includes(key)) unlockedKeys.push(key)
|
|
11
18
|
|
|
12
|
-
if (
|
|
19
|
+
if (unlockedKeys.length === keys.length) {
|
|
13
20
|
deferred.resolve()
|
|
14
21
|
return true
|
|
15
22
|
}
|
package/src/enhancers/combine.js
CHANGED
|
@@ -4,7 +4,7 @@ import createSimpleObserver from '../simple-observer'
|
|
|
4
4
|
|
|
5
5
|
const combine = (atoms) => {
|
|
6
6
|
const { notify, observe } = createSimpleObserver()
|
|
7
|
-
const countdownLock = createCountdownLock(Object.keys(atoms)
|
|
7
|
+
const countdownLock = createCountdownLock(Object.keys(atoms))
|
|
8
8
|
|
|
9
9
|
let values = {}
|
|
10
10
|
|
|
@@ -14,7 +14,7 @@ const combine = (atoms) => {
|
|
|
14
14
|
...values,
|
|
15
15
|
[name]: value,
|
|
16
16
|
}
|
|
17
|
-
if (countdownLock.unlock()) await notify(values)
|
|
17
|
+
if (countdownLock.unlock(name)) await notify(values)
|
|
18
18
|
})
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import assert from 'minimalistic-assert'
|
|
2
|
+
|
|
3
|
+
import enforceObservableRules from '../enforce-rules'
|
|
4
|
+
import createSimpleObserver from '../simple-observer'
|
|
5
|
+
|
|
6
|
+
const getRange = (offset, length) => Array.from({ length }, (_, i) => i + offset)
|
|
7
|
+
|
|
8
|
+
const createSequencedKeystoreAtom = ({
|
|
9
|
+
keystore,
|
|
10
|
+
config: {
|
|
11
|
+
//
|
|
12
|
+
key,
|
|
13
|
+
separator = '.',
|
|
14
|
+
defaultValue = [],
|
|
15
|
+
isSoleWriter,
|
|
16
|
+
getOpts,
|
|
17
|
+
setOpts,
|
|
18
|
+
deleteOpts,
|
|
19
|
+
},
|
|
20
|
+
}) => {
|
|
21
|
+
assert(key, 'sequence keystore atom: key missing')
|
|
22
|
+
|
|
23
|
+
assert(typeof key === 'string', 'sequence keystore atom: key must be a string')
|
|
24
|
+
|
|
25
|
+
assert(Array.isArray(defaultValue), 'sequence keystore atom: default value must be an array')
|
|
26
|
+
|
|
27
|
+
const { notify, observe } = createSimpleObserver({ enable: isSoleWriter })
|
|
28
|
+
|
|
29
|
+
let cache
|
|
30
|
+
|
|
31
|
+
const getKey = (index) =>
|
|
32
|
+
key.endsWith(separator) ? `${key}${index}` : `${key}${separator}${index}`
|
|
33
|
+
|
|
34
|
+
const getByIndex = async (index) => {
|
|
35
|
+
const key = getKey(index)
|
|
36
|
+
const value = await keystore.getSecret(key, getOpts)
|
|
37
|
+
if (value) return { key, value }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const list = async () => {
|
|
41
|
+
let result = []
|
|
42
|
+
let batch = []
|
|
43
|
+
let offset = 0
|
|
44
|
+
const batchSize = 5
|
|
45
|
+
|
|
46
|
+
// paginate until we're out of items
|
|
47
|
+
do {
|
|
48
|
+
const range = getRange(offset, batchSize)
|
|
49
|
+
batch = await Promise.all(range.map(getByIndex))
|
|
50
|
+
batch = batch.filter(Boolean)
|
|
51
|
+
result = [...result, ...batch]
|
|
52
|
+
offset += batchSize
|
|
53
|
+
} while (batch.length > 0)
|
|
54
|
+
|
|
55
|
+
return result
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const clear = async () => {
|
|
59
|
+
const items = await list()
|
|
60
|
+
await Promise.all(items.map((item) => keystore.deleteSecret(item.key, deleteOpts)))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const _set = async (value) => {
|
|
64
|
+
assert(Array.isArray(value), 'sequence keystore atom: set value must be an array')
|
|
65
|
+
|
|
66
|
+
await Promise.all(
|
|
67
|
+
value.map((value, index) => {
|
|
68
|
+
return keystore.setSecret(getKey(index), value, setOpts)
|
|
69
|
+
})
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const set = async (value) => {
|
|
74
|
+
await clear()
|
|
75
|
+
|
|
76
|
+
if (value != null) {
|
|
77
|
+
await _set(value)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (isSoleWriter) {
|
|
81
|
+
cache = value
|
|
82
|
+
await notify(value)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const get = async () => {
|
|
87
|
+
if (cache) return cache
|
|
88
|
+
|
|
89
|
+
const items = await list()
|
|
90
|
+
const value = items.map((item) => item.value)
|
|
91
|
+
|
|
92
|
+
if (isSoleWriter) {
|
|
93
|
+
cache = value
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// let default value be used
|
|
97
|
+
return items.length === 0 ? undefined : value
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return enforceObservableRules({
|
|
101
|
+
get,
|
|
102
|
+
set,
|
|
103
|
+
observe,
|
|
104
|
+
defaultValue,
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export default createSequencedKeystoreAtom
|
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 createKeystoreAtom } from './factories/keystore'
|
|
8
|
+
export { default as createSequencedKeystoreAtom } from './factories/sequenced-keystore'
|
|
8
9
|
export { default as fromEventEmitter } from './event-emitter'
|
|
9
10
|
export { default as compute } from './enhancers/compute'
|
|
10
11
|
export { default as blockUntil } from './enhancers/block-until'
|