forked from Qortal/qortal-ui
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
701 B
23 lines
701 B
/** |
|
* 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.js' |
|
|
|
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 |
|
}) |
|
}) |
|
}
|
|
|