Skip to content

Commit

Permalink
refactor loading state to be comptuted value
Browse files Browse the repository at this point in the history
  • Loading branch information
t2t2 committed Oct 18, 2016
1 parent 5590401 commit 036dfad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export default function (Vue) {
[initHook]: beforeCreate(Vue),
created: created(),
beforeDestroy: beforeDestroy(),
computed: {
$loadingSyncers: loadingStateGetter
},
methods: {
$refreshSyncers: refreshSyncers
}
Expand Down Expand Up @@ -44,13 +47,6 @@ function beforeCreate(Vue) {
configurable: true
})
})

// Add state that tells if all are loaded
Vue.util.defineReactive(this, '$loadingSyncers', true)
// The watcher for this is in created() as $watch doesn't work this early
} else {
// Never will change
this.$loadingSyncers = false
}
}
}
Expand All @@ -64,18 +60,6 @@ function created() {
each(this._syncers, syncer => {
syncer.ready()
})

if (Object.keys(this._syncers).length > 0) {
// Watcher for $loadingSyncers
this.$watch(function () {
// If any are true
return some(this._syncers, syncer => {
return 'loading' in syncer ? syncer.loading : false
})
}, newVal => {
this.$loadingSyncers = newVal
}, {sync: true, immediate: true})
}
}
}

Expand All @@ -91,6 +75,20 @@ function beforeDestroy() {
}
}

/**
* Get loading state of the syncers
*
* @returns {boolean}
*/
function loadingStateGetter() {
if (Object.keys(this._syncers).length > 0) {
return some(this._syncers, syncer => {
return syncer.loading
})
}
return false
}

/**
* Refresh syncers state
*
Expand Down
2 changes: 2 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,7 @@ test.cb('Non-used instances work fine', t => {
})
}
})
// No syncers = not loading
t.falsy(instance.$loadingSyncers)
instance.$destroy()
})

0 comments on commit 036dfad

Please sign in to comment.