Abstract waiting for the store to get into a given state

This commit is contained in:
Jordan Eldredge 2018-01-17 18:59:14 -08:00
parent b4b583a3ed
commit f9556fbfb0

View file

@ -10,6 +10,21 @@ import { setSkinFromUrl, loadMediaFromUrl } from "./actionCreators";
import { SET_AVALIABLE_SKINS } from "./actionTypes";
// Return a promise that resolves when the store matches a predicate.
const storeHas = (store, predicate) =>
new Promise(resolve => {
if (predicate(store.getState())) {
resolve();
return;
}
const unsubscribe = store.subscribe(() => {
if (predicate(store.getState())) {
resolve();
unsubscribe();
}
});
});
class Winamp {
constructor(options) {
this.options = options;
@ -18,23 +33,6 @@ class Winamp {
this.store = getStore(this.media, this.options.__initialState);
}
_skinHasLoaded() {
return new Promise(resolve => {
const initialState = this.store.getState();
if (!initialState.display.loading) {
resolve();
return;
}
const unsubscribe = this.store.subscribe(() => {
const state = this.store.getState();
if (!state.display.loading) {
resolve();
unsubscribe();
}
});
});
}
async render(node) {
this.store.dispatch(setSkinFromUrl(this.options.initialSkin.url));
@ -56,7 +54,8 @@ class Winamp {
new Hotkeys(this.store.dispatch);
await this._skinHasLoaded();
// Wait for the skin to load.
await storeHas(this.store, state => !state.display.loading);
render(
<Provider store={this.store}>