Commit e9257b85 authored by Evan You's avatar Evan You

comments

parent 8db2ee92
...@@ -72,6 +72,7 @@ export default { ...@@ -72,6 +72,7 @@ export default {
if (this.$root._isMounted) { if (this.$root._isMounted) {
this.loadItems(this.page) this.loadItems(this.page)
} }
// watch the current list for realtime updates
this.unwatchList = watchList(this.type, ids => { this.unwatchList = watchList(this.type, ids => {
this.$store.commit('SET_LIST', { type: this.type, ids }) this.$store.commit('SET_LIST', { type: this.type, ids })
this.$store.dispatch('FETCH_ACTIVE_ITEMS').then(() => { this.$store.dispatch('FETCH_ACTIVE_ITEMS').then(() => {
......
...@@ -79,11 +79,14 @@ export function watchList (type, cb) { ...@@ -79,11 +79,14 @@ export function watchList (type, cb) {
if (first) { if (first) {
first = false first = false
} else { } else {
console.log(`update for ${type}`)
cb(snapshot.val()) cb(snapshot.val())
} }
} }
console.log(`watching ${type}...`)
ref.on('value', handler) ref.on('value', handler)
return () => { return () => {
console.log(`stop watching ${type}`)
ref.off('value', handler) ref.off('value', handler)
} }
} }
...@@ -22,6 +22,7 @@ const store = new Vuex.Store({ ...@@ -22,6 +22,7 @@ const store = new Vuex.Store({
}, },
actions: { actions: {
// ensure data for rendering given list type
FETCH_DATA_FOR_TYPE: ({ commit, dispatch, state, getters }, { type }) => { FETCH_DATA_FOR_TYPE: ({ commit, dispatch, state, getters }, { type }) => {
commit('SET_ACTIVE_TYPE', { type }) commit('SET_ACTIVE_TYPE', { type })
return fetchIdsByType(type) return fetchIdsByType(type)
...@@ -29,7 +30,9 @@ const store = new Vuex.Store({ ...@@ -29,7 +30,9 @@ const store = new Vuex.Store({
.then(() => dispatch('FETCH_ACTIVE_ITEMS')) .then(() => dispatch('FETCH_ACTIVE_ITEMS'))
}, },
// ensure all active items are fetched
FETCH_ACTIVE_ITEMS: ({ commit, state, getters }) => { FETCH_ACTIVE_ITEMS: ({ commit, state, getters }) => {
// only fetch items that we don't already have.
const ids = getters.activeIds.filter(id => !state.items[id]) const ids = getters.activeIds.filter(id => !state.items[id])
if (ids.length) { if (ids.length) {
return fetchItems(ids).then(items => commit('SET_ITEMS', { items })) return fetchItems(ids).then(items => commit('SET_ITEMS', { items }))
...@@ -58,6 +61,8 @@ const store = new Vuex.Store({ ...@@ -58,6 +61,8 @@ const store = new Vuex.Store({
}, },
getters: { getters: {
// ids of the items that should be currently displayed based on
// current list type and current pagination
activeIds (state) { activeIds (state) {
const { activeType, itemsPerPage, lists } = state const { activeType, itemsPerPage, lists } = state
const page = Number(state.route.params.page) || 1 const page = Number(state.route.params.page) || 1
...@@ -70,6 +75,8 @@ const store = new Vuex.Store({ ...@@ -70,6 +75,8 @@ const store = new Vuex.Store({
} }
}, },
// items that should be currently displayed.
// this Array may not be fully fetched.
activeItems (state, getters) { activeItems (state, getters) {
return getters.activeIds.map(id => state.items[id]).filter(_ => _) return getters.activeIds.map(id => state.items[id]).filter(_ => _)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment