@exodus/atoms 5.5.0 → 5.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/atoms",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.7.0",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"description": "Abstraction for encapsulating a piece of data behind a simple unified interface: get, set, observe",
|
|
6
6
|
"author": "Exodus Movement Inc.",
|
|
@@ -31,10 +31,10 @@
|
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@exodus/atom-tests": "^1.0.0",
|
|
34
|
-
"@exodus/storage-memory": "^2.1.
|
|
34
|
+
"@exodus/storage-memory": "^2.1.1",
|
|
35
35
|
"delay": "^5.0.0",
|
|
36
36
|
"eslint": "^8.44.0",
|
|
37
37
|
"jest": "^29.1.2"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "d6c6919e0879a84f30a6fa5c2f2e0d9a35ce7d2f"
|
|
40
40
|
}
|
package/src/enhancers/dedupe.js
CHANGED
|
@@ -9,9 +9,22 @@ const dedupe = (atom) => {
|
|
|
9
9
|
return atom.set((previous) => (isEqual(previous, value) ? previous : value))
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
const observe = (callback) => {
|
|
13
|
+
let called
|
|
14
|
+
let previous
|
|
15
|
+
return atom.observe(async (value) => {
|
|
16
|
+
if (called && isEqual(previous, value)) return
|
|
17
|
+
|
|
18
|
+
called = true
|
|
19
|
+
previous = value
|
|
20
|
+
return callback(value)
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
12
24
|
return {
|
|
13
25
|
...atom,
|
|
14
26
|
set,
|
|
27
|
+
observe,
|
|
15
28
|
}
|
|
16
29
|
}
|
|
17
30
|
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import makeConcurrent from 'make-concurrent'
|
|
2
|
+
import createInMemoryAtom from '../factories/memory'
|
|
3
|
+
|
|
4
|
+
const optimisticNotifier = (atom) => {
|
|
5
|
+
const memoryAtom = createInMemoryAtom()
|
|
6
|
+
|
|
7
|
+
const set = makeConcurrent(async (newValue) => {
|
|
8
|
+
let previous
|
|
9
|
+
await memoryAtom.set((previousValue) => {
|
|
10
|
+
previous = previousValue
|
|
11
|
+
return newValue
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
atom
|
|
15
|
+
.set(newValue)
|
|
16
|
+
// only revert in case the value hasn't changed in the meanwhile
|
|
17
|
+
.catch(() => memoryAtom.set((value) => (value === newValue ? previous : value)))
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
let unsubscribeSource
|
|
21
|
+
let subscribers = 0
|
|
22
|
+
|
|
23
|
+
const maybeUnsubscribeSource = () => {
|
|
24
|
+
subscribers -= 1
|
|
25
|
+
if (subscribers > 0) return
|
|
26
|
+
unsubscribeSource()
|
|
27
|
+
unsubscribeSource = undefined
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const observe = (callback) => {
|
|
31
|
+
if (subscribers === 0) {
|
|
32
|
+
unsubscribeSource = atom.observe(set)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
subscribers += 1
|
|
36
|
+
const unsubscribe = memoryAtom.observe(callback)
|
|
37
|
+
|
|
38
|
+
return () => {
|
|
39
|
+
unsubscribe()
|
|
40
|
+
maybeUnsubscribeSource()
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const get = async () => {
|
|
45
|
+
if (subscribers === 0) {
|
|
46
|
+
return atom.get()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return memoryAtom.get()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
...memoryAtom,
|
|
54
|
+
set,
|
|
55
|
+
get,
|
|
56
|
+
observe,
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export default optimisticNotifier
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
const createAtomObserver = ({ port, atom, event }) => {
|
|
1
|
+
const createAtomObserver = ({ port, atom, event, immediateRegister = true }) => {
|
|
2
2
|
let emitting = false
|
|
3
3
|
let unobserve
|
|
4
4
|
|
|
5
5
|
const register = () => {
|
|
6
|
+
if (unobserve) return
|
|
7
|
+
|
|
6
8
|
unobserve = atom.observe((value) => {
|
|
7
9
|
if (emitting) port.emit(event, value)
|
|
8
10
|
})
|
|
@@ -17,6 +19,10 @@ const createAtomObserver = ({ port, atom, event }) => {
|
|
|
17
19
|
port.emit(event, await atom.get())
|
|
18
20
|
}
|
|
19
21
|
|
|
22
|
+
if (immediateRegister) {
|
|
23
|
+
register()
|
|
24
|
+
}
|
|
25
|
+
|
|
20
26
|
return {
|
|
21
27
|
register,
|
|
22
28
|
unregister,
|