2024-05-10 18:00:47 +02:00
|
|
|
import { store } from './api'
|
2021-12-25 14:39:47 +01:00
|
|
|
|
|
|
|
let config = false
|
|
|
|
let loaded = false
|
|
|
|
const configWatchers = []
|
|
|
|
const waitingForConfig = []
|
|
|
|
|
|
|
|
const subscribeToStore = () => {
|
2023-01-13 09:52:14 +01:00
|
|
|
if (!store) return setTimeout(() => subscribeToStore(), 50)
|
2021-12-25 14:39:47 +01:00
|
|
|
|
2023-01-13 09:52:14 +01:00
|
|
|
store.subscribe(() => {
|
|
|
|
const cA = store.getState().app
|
|
|
|
const c = store.getState().config
|
|
|
|
if (!c.loaded) return
|
|
|
|
if (!loaded) waitingForConfig.forEach(r => r(cA))
|
|
|
|
configWatchers.forEach(fn => fn(cA))
|
|
|
|
config = cA
|
|
|
|
})
|
2021-12-25 14:39:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
subscribeToStore()
|
|
|
|
|
|
|
|
export function getConfig() {
|
2023-01-13 09:52:14 +01:00
|
|
|
return config
|
2021-12-25 14:39:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function watchConfig(fn) {
|
2023-01-13 09:52:14 +01:00
|
|
|
fn(config)
|
|
|
|
configWatchers.push(fn)
|
2021-12-25 14:39:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function waitForConfig() {
|
2023-01-13 09:52:14 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (config) return resolve(config)
|
|
|
|
waitingForConfig.push(resolve)
|
|
|
|
})
|
|
|
|
}
|