@exodus/atoms
Advanced tools
Comparing version 5.2.1 to 5.2.2
{ | ||
"name": "@exodus/atoms", | ||
"version": "5.2.1", | ||
"version": "5.2.2", | ||
"main": "src/index.js", | ||
@@ -19,3 +19,3 @@ "description": "Abstraction for encapsulating a piece of data behind a simple unified interface: get, set, observe", | ||
}, | ||
"homepage": "https://github.com/ExodusMovement/exodus-hydra/tree/master/modules/atom", | ||
"homepage": "https://github.com/ExodusMovement/exodus-hydra/tree/master/libraries/atoms", | ||
"license": "UNLICENSED", | ||
@@ -36,6 +36,6 @@ "bugs": { | ||
"delay": "^5.0.0", | ||
"eslint": "^8.33.0", | ||
"eslint": "^8.44.0", | ||
"jest": "^29.1.2" | ||
}, | ||
"gitHead": "3cc58449431fce7a8fa18be6d7f389727d09665b" | ||
"gitHead": "4b2c9803b5d53c65b77475187777ae5633e5d619" | ||
} |
@@ -6,20 +6,42 @@ import createCountdownLock from '../countdown-lock' | ||
const combine = (atoms) => { | ||
const { notify, observe } = createSimpleObserver() | ||
const { notify, observe: observeSimpleObserver } = createSimpleObserver() | ||
const countdownLock = createCountdownLock(Object.keys(atoms)) | ||
let values = {} | ||
let subscriptions = [] | ||
let subscribers = 0 | ||
for (const name in atoms) { | ||
atoms[name].observe(async (value) => { | ||
values = { | ||
...values, | ||
[name]: value, | ||
} | ||
if (countdownLock.unlock(name)) await notify(values) | ||
}) | ||
const maybeUnsubscribeSourceObservables = () => { | ||
subscribers -= 1 | ||
if (subscribers > 0) return | ||
subscriptions.forEach((unsubscribe) => unsubscribe()) | ||
subscriptions = [] | ||
} | ||
const observe = (callback) => { | ||
subscribers += 1 | ||
if (subscribers === 1) { | ||
subscriptions = Object.entries(atoms).map(([name, atom]) => | ||
atom.observe(async (value) => { | ||
values = { | ||
...values, | ||
[name]: value, | ||
} | ||
if (countdownLock.unlock(name)) await notify(values) | ||
}) | ||
) | ||
} | ||
const unsubscribe = observeSimpleObserver(callback) | ||
return () => { | ||
unsubscribe() | ||
maybeUnsubscribeSourceObservables() | ||
} | ||
} | ||
const get = async () => { | ||
await countdownLock.promise | ||
return values | ||
return Object.fromEntries( | ||
await Promise.all(Object.entries(atoms).map(async ([name, atom]) => [name, await atom.get()])) | ||
) | ||
} | ||
@@ -26,0 +48,0 @@ |
import createSimpleObserver from '../simple-observer' | ||
import enforceObservableRules from '../enforce-rules' | ||
import pDefer from 'p-defer' | ||
@@ -10,8 +11,12 @@ const createStorageAtomFactory = | ||
let cached | ||
let writePromiseDefer | ||
const set = async (value) => { | ||
if (value === undefined) { | ||
writePromiseDefer = pDefer() | ||
await storage.delete(key) | ||
} else { | ||
writePromiseDefer = pDefer() | ||
await storage.set(key, value) | ||
} | ||
writePromiseDefer.resolve() | ||
@@ -29,2 +34,9 @@ if (isSoleWriter) { | ||
if (writePromiseDefer) { | ||
await writePromiseDefer.promise | ||
} | ||
if (cached) { | ||
return cached | ||
} | ||
const value = await storage.get(key) | ||
@@ -31,0 +43,0 @@ |
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
20500
582