@exodus/atoms 1.1.0 → 2.1.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/index.js +39 -28
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -73,19 +73,48 @@ export const createFusionAtomFactory =
|
|
|
73
73
|
})
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
const createSimpleObserver = ({ enable = true } = {}) => {
|
|
77
|
+
let listeners = []
|
|
78
|
+
const notify = async (value) => Promise.all(listeners.map((listener) => listener(value)))
|
|
79
|
+
|
|
80
|
+
const observe = (listener) => {
|
|
81
|
+
if (!enable) {
|
|
82
|
+
throw new Error('observe method is not supported')
|
|
83
|
+
}
|
|
84
|
+
listeners.push(listener)
|
|
85
|
+
return () => {
|
|
86
|
+
listeners = listeners.filter((fn) => fn !== listener)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
observe,
|
|
92
|
+
notify,
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
76
96
|
export const createStorageAtomFactory =
|
|
77
97
|
({ storage }) =>
|
|
78
|
-
({ key, defaultValue }) => {
|
|
79
|
-
const
|
|
98
|
+
({ key, defaultValue, isSoleWriter }) => {
|
|
99
|
+
const { notify, observe } = createSimpleObserver({ enable: isSoleWriter })
|
|
100
|
+
|
|
101
|
+
const set = async (value) => {
|
|
102
|
+
if (typeof value === 'undefined') {
|
|
103
|
+
await storage.delete(key)
|
|
104
|
+
} else {
|
|
105
|
+
await storage.set(key, value)
|
|
106
|
+
}
|
|
107
|
+
if (isSoleWriter) {
|
|
108
|
+
await notify(value)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
80
111
|
|
|
81
112
|
const get = () => storage.get(key)
|
|
82
113
|
|
|
83
114
|
return enforceObservableRules({
|
|
84
115
|
get,
|
|
85
116
|
set,
|
|
86
|
-
observe
|
|
87
|
-
throw new Error('storage atom does not support observe')
|
|
88
|
-
},
|
|
117
|
+
observe,
|
|
89
118
|
defaultValue,
|
|
90
119
|
})
|
|
91
120
|
}
|
|
@@ -93,32 +122,14 @@ export const createStorageAtomFactory =
|
|
|
93
122
|
export const createRemoteConfigAtomFactory =
|
|
94
123
|
({ remoteConfig }) =>
|
|
95
124
|
({ path, defaultValue }) => {
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
let listeners = []
|
|
99
|
-
|
|
100
|
-
const notify = async (remoteConfigJSON) => {
|
|
101
|
-
const value = getValue(remoteConfigJSON)
|
|
102
|
-
return Promise.all(listeners.map((listener) => listener(value)))
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const get = async () => {
|
|
106
|
-
const remoteConfigJSON = await remoteConfig.get()
|
|
107
|
-
return getValue(remoteConfigJSON)
|
|
108
|
-
}
|
|
125
|
+
const { notify, observe } = createSimpleObserver()
|
|
109
126
|
|
|
127
|
+
const get = () => remoteConfig.get(path)
|
|
110
128
|
const set = async () => {
|
|
111
129
|
throw new Error('remoteConfig is read-only')
|
|
112
130
|
}
|
|
113
131
|
|
|
114
|
-
|
|
115
|
-
listeners.push(listener)
|
|
116
|
-
return () => {
|
|
117
|
-
listeners = listeners.filter((fn) => fn !== listener)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
remoteConfig.on('remote-config', notify)
|
|
132
|
+
remoteConfig.on('sync', ({ current }) => notify(getValueAtPath(current, path)))
|
|
122
133
|
|
|
123
134
|
return enforceObservableRules({
|
|
124
135
|
get,
|
|
@@ -131,8 +142,8 @@ export const createRemoteConfigAtomFactory =
|
|
|
131
142
|
export const createLocalConfigAtomFactory =
|
|
132
143
|
({ localConfig }) =>
|
|
133
144
|
({ key, defaultValue }) => {
|
|
134
|
-
const observe = (
|
|
135
|
-
|
|
145
|
+
const { notify, observe } = createSimpleObserver()
|
|
146
|
+
localConfig.on(`update:${key}`, ({ current }) => notify(current))
|
|
136
147
|
|
|
137
148
|
return enforceObservableRules({
|
|
138
149
|
get: () => localConfig.get(key),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/atoms",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"author": "Exodus Movement Inc.",
|
|
6
6
|
"scripts": {
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"@exodus/storage-memory": "^1.0.0",
|
|
30
30
|
"delay": "^5.0.0"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "231a041f31a91990024a1282067b737a3c9dfe41"
|
|
33
33
|
}
|