@exodus/atoms 3.4.0 → 3.4.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/atoms",
3
- "version": "3.4.0",
3
+ "version": "3.4.1",
4
4
  "main": "src/index.js",
5
5
  "author": "Exodus Movement Inc.",
6
6
  "scripts": {
@@ -33,5 +33,5 @@
33
33
  "eslint": "^8.33.0",
34
34
  "jest": "^29.1.2"
35
35
  },
36
- "gitHead": "16e977048c07d7dd898ce0555895c43eb0a5402a"
36
+ "gitHead": "0c9ed53bfe02b1d0dc0666bc83c0bd1dcd8f5e36"
37
37
  }
@@ -1,15 +1,22 @@
1
1
  import pDefer from 'p-defer'
2
2
 
3
- const createCountdownLock = (n) => {
4
- if (typeof n !== 'number' || n <= 0) throw new Error('lock number must be positive')
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 (n > 0) n--
16
+ unlock: (key) => {
17
+ if (!unlockedKeys.includes(key)) unlockedKeys.push(key)
11
18
 
12
- if (n <= 0) {
19
+ if (unlockedKeys.length === keys.length) {
13
20
  deferred.resolve()
14
21
  return true
15
22
  }
@@ -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).length)
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