From 148b6a99cf5e1734ca553eb634772d581471948d Mon Sep 17 00:00:00 2001 From: Radek Stepan Date: Sun, 17 Jan 2016 15:11:42 +0100 Subject: [PATCH] handle system loading state --- src/js/core/Store.js | 12 +++++++++--- src/js/mixins/Page.js | 5 +++-- src/js/stores/appStore.js | 6 +++--- src/js/stores/projectsStore.js | 7 ++----- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/js/core/Store.js b/src/js/core/Store.js index 84c9877..9ba9069 100644 --- a/src/js/core/Store.js +++ b/src/js/core/Store.js @@ -5,6 +5,8 @@ import { diff } from 'deep-diff'; import EventEmitter from './EventEmitter.js'; +import actions from '../actions/appActions.js'; + const DATA = 'data'; export default class Store extends EventEmitter { @@ -17,24 +19,28 @@ export default class Store extends EventEmitter { this._cbs = {}; } - // Register an async function callback. + // Register an async function callback, handle loading state. // TODO: unit-test. cb(fn) { let id = _.uniqueId(); + actions.emit('system.loading', true); return this._cbs[id] = (...args) => { // Still running? - if (!(id in this._cbs)) return console.log(`stop ${id}`); + if (!(id in this._cbs)) return; fn.apply(this, args); delete this._cbs[id]; + if (!(Object.keys(this._cbs).length)) { + actions.emit('system.loading', false); + } }; }; // Cleanup callbacks because a View has changed thus long-running // functions need to end. Unreference any onChange events too. clean(onChange) { - console.log('cleaning up'); for (let id in this._cbs) delete this._cbs[id]; if (_.isFunction(onChange)) this.offAny(onChange); + actions.emit('system.loading', false); } // Set a value on a key. Pass falsy value as 3rd param to not emit changes. diff --git a/src/js/mixins/Page.js b/src/js/mixins/Page.js index 299add2..6c070e5 100644 --- a/src/js/mixins/Page.js +++ b/src/js/mixins/Page.js @@ -28,7 +28,9 @@ export default { }, _onChange(store, val, key) { - this.setState(this._getData(store)); + if (this.isMounted()) { // not ideal + this.setState(this._getData(store)); + } }, getInitialState() { @@ -46,7 +48,6 @@ export default { componentWillUnmount() { let key; for (key in stores) { - console.log('cleanup'); stores[key].clean(this._onChange); } } diff --git a/src/js/stores/appStore.js b/src/js/stores/appStore.js index 62f81ea..9313baa 100644 --- a/src/js/stores/appStore.js +++ b/src/js/stores/appStore.js @@ -15,10 +15,10 @@ class AppStore extends Store { // Initial payload. constructor() { super({ - system: { - loading: false + 'system': { + 'loading': false, }, - user: {} + 'user': {} }); // Listen to all app actions. diff --git a/src/js/stores/projectsStore.js b/src/js/stores/projectsStore.js index 6d961f7..27b05c2 100644 --- a/src/js/stores/projectsStore.js +++ b/src/js/stores/projectsStore.js @@ -64,15 +64,12 @@ class ProjectsStore extends Store { onProjectsLoad() { let list = this.get('list'); - let done = (err) => { - // actions.emit('system.loading', false); - }; + // Empty callback. + let done = () => { }; // Quit if we have no projects. if (!list.length) return done(); - // actions.emit('system.loading', true); - // Wait for the user to get resolved. this.get('user', this.cb((user) => { // async // For all projects.