Skip to content

Instantly share code, notes, and snippets.

@itrelease
Forked from gaearon/observeStore.js
Last active August 29, 2015 14:11
Show Gist options
  • Save itrelease/5589a4cb858d26ad9f9e to your computer and use it in GitHub Desktop.
Save itrelease/5589a4cb858d26ad9f9e to your computer and use it in GitHub Desktop.
// Usage example:
//
// willTransitionTo(transition, params) {
// transition.wait(
// observeStore(
// DraftStore,
// s => s.isLoaded()
// ).then(() => {
// if (DraftStore.isMissingTitle()) {
// transition.redirect('composeDraft', params);
// }
// })
// );
// }
'use strict';
var Promise = require('bluebird');
function observeStore(store, predicate) {
var performCheck;
return new Promise(resolve => {
performCheck = () => {
if (predicate.call(null, store)) {
resolve();
}
};
store.addChangeListener(performCheck);
performCheck();
}).finally(() => {
store.removeChangeListener(performCheck);
});
}
module.exports = observeStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment