mirror of
https://github.com/captbaritone/webamp.git
synced 2026-07-21 09:09:01 +00:00
Abstract waiting for the store to get into a given state
This commit is contained in:
parent
b4b583a3ed
commit
f9556fbfb0
1 changed files with 17 additions and 18 deletions
35
js/winamp.js
35
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(
|
||||
<Provider store={this.store}>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue