4
1
mirror of https://github.com/Qortal/qortal-ui.git synced 2025-02-14 11:15:50 +00:00
qortal-ui/core/src/stateAwait.js
AlphaX-Projects fa29ff4c43 Update UI
Refactor and added new functioms
2024-05-08 13:16:23 +02:00

27 lines
614 B
JavaScript

/**
* Simple helper function so that I can do `await stateAwait(state => state.something === true)` or `await stateAwait(state => state.name === 'myName')`
*/
import { store } from './store'
let subscriptions = []
store.subscribe(() => {
const state = store.getState()
subscriptions = subscriptions.filter(fn => fn(state))
})
export const stateAwait = fn => {
return new Promise((resolve, reject) => {
// Check immediately...then if not true store it
if (fn(store.getState())) resolve()
subscriptions.push(state => {
if (fn(state)) {
resolve()
return true
}
return false
})
})
}