@exodus/atoms 2.7.0 → 2.8.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 +2 -2
- package/src/countdown-lock.js +22 -0
- package/src/enhancers/combine.js +34 -0
- package/src/index.js +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/atoms",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.0",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"author": "Exodus Movement Inc.",
|
|
6
6
|
"scripts": {
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"delay": "^5.0.0",
|
|
32
32
|
"jest": "^29.1.2"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "b2dcd19ea7cb67f7677b03e2a1ba91998a0db9e0"
|
|
35
35
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import pDefer from 'p-defer'
|
|
2
|
+
|
|
3
|
+
const createCountdownLock = (n) => {
|
|
4
|
+
if (typeof n !== 'number' || n <= 0) throw new Error('lock number must be positive')
|
|
5
|
+
|
|
6
|
+
const deferred = pDefer()
|
|
7
|
+
return {
|
|
8
|
+
promise: deferred.promise,
|
|
9
|
+
unlock: () => {
|
|
10
|
+
if (n > 0) n--
|
|
11
|
+
|
|
12
|
+
if (n <= 0) {
|
|
13
|
+
deferred.resolve()
|
|
14
|
+
return true
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return false
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default createCountdownLock
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import createCountdownLock from '../countdown-lock'
|
|
2
|
+
import enforceObservableRules from '../enforce-rules'
|
|
3
|
+
import createSimpleObserver from '../simple-observer'
|
|
4
|
+
|
|
5
|
+
const combine = (atoms) => {
|
|
6
|
+
const { notify, observe } = createSimpleObserver()
|
|
7
|
+
const countdownLock = createCountdownLock(Object.keys(atoms).length)
|
|
8
|
+
|
|
9
|
+
let values = {}
|
|
10
|
+
|
|
11
|
+
for (const name in atoms) {
|
|
12
|
+
atoms[name].observe(async (value) => {
|
|
13
|
+
values[name] = value
|
|
14
|
+
if (countdownLock.unlock()) await notify(values)
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const get = async () => {
|
|
19
|
+
await countdownLock.promise
|
|
20
|
+
return values
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const set = async () => {
|
|
24
|
+
throw new Error('combine does not support method: set')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return enforceObservableRules({
|
|
28
|
+
get,
|
|
29
|
+
set,
|
|
30
|
+
observe,
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default combine
|
package/src/index.js
CHANGED
|
@@ -10,3 +10,4 @@ export { default as fromEventEmitter } from './event-emitter'
|
|
|
10
10
|
export { default as compute } from './enhancers/compute'
|
|
11
11
|
export { default as difference } from './enhancers/difference'
|
|
12
12
|
export { default as withSerialization } from './enhancers/with-serialization'
|
|
13
|
+
export { default as combine } from './enhancers/combine'
|