diff --git a/js/winamp.js b/js/winamp.js index dd0ef133..152aa790 100644 --- a/js/winamp.js +++ b/js/winamp.js @@ -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(