Commit af890a47 authored by Evan You's avatar Evan You

basic item/user prefetch

parent f519772c
...@@ -72,6 +72,10 @@ export function fetchItems (ids) { ...@@ -72,6 +72,10 @@ export function fetchItems (ids) {
return Promise.all(ids.map(id => fetchItem(id))) return Promise.all(ids.map(id => fetchItem(id)))
} }
export function fetchUser (id) {
return fetch(`user/${id}`)
}
export function watchList (type, cb) { export function watchList (type, cb) {
let first = true let first = true
const ref = api.child(`${type}stories`) const ref = api.child(`${type}stories`)
......
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import { fetchIdsByType, fetchItem, fetchItems } from './api' import { fetchItem, fetchItems, fetchIdsByType, fetchUser } from './api'
Vue.use(Vuex) Vue.use(Vuex)
...@@ -8,12 +8,10 @@ const store = new Vuex.Store({ ...@@ -8,12 +8,10 @@ const store = new Vuex.Store({
state: { state: {
activeType: null, activeType: null,
itemsPerPage: 20, itemsPerPage: 20,
// fetched items by id. This also serves as a cache to some extent
items: {/* [id: number]: Item */}, items: {/* [id: number]: Item */},
// the id lists for each type of stories users: {/* [id: string]: User */},
// will be periodically updated in realtime
lists: { lists: {
top: [], top: [/* number */],
new: [], new: [],
show: [], show: [],
ask: [], ask: [],
...@@ -45,6 +43,12 @@ const store = new Vuex.Store({ ...@@ -45,6 +43,12 @@ const store = new Vuex.Store({
} else { } else {
return Promise.resolve() return Promise.resolve()
} }
},
FETCH_USER: ({ commit, state }, { id }) => {
return state.users[id]
? Promise.resolve(state.users[id])
: fetchUser(id).then(user => commit('SET_USER', { user }))
} }
}, },
...@@ -63,6 +67,10 @@ const store = new Vuex.Store({ ...@@ -63,6 +67,10 @@ const store = new Vuex.Store({
Vue.set(state.items, item.id, item) Vue.set(state.items, item.id, item)
} }
}) })
},
SET_USER: (state, { user }) => {
Vue.set(state.users, user.id, user)
} }
}, },
......
...@@ -6,6 +6,12 @@ ...@@ -6,6 +6,12 @@
</template> </template>
<script> <script>
function fetchItem (store) {
return store.dispatch('FETCH_ITEMS', {
ids: [store.state.route.params.id]
})
}
export default { export default {
name: 'item-view', name: 'item-view',
computed: { computed: {
...@@ -13,10 +19,9 @@ export default { ...@@ -13,10 +19,9 @@ export default {
return this.$store.state.items[this.$route.params.id] return this.$store.state.items[this.$route.params.id]
} }
}, },
preFetch: fetchItem,
beforeMount () { beforeMount () {
this.$store.dispatch('FETCH_ITEMS', { fetchItem(this.$store)
ids: [this.$route.params.id]
})
} }
} }
</script> </script>
<template> <template>
<div class="user-view"> <div class="user-view">
<h2>User!</h2> <h2 v-if="user">User: {{ user.id }}</h2>
</div> </div>
</template> </template>
<script> <script>
function fetchUser (store) {
return store.dispatch('FETCH_USER', {
id: store.state.route.params.id
})
}
export default { export default {
name: 'user-view', name: 'user-view',
computed: {
preFetch (store) { user () {
return store.dispatch('FETCH_USER', { return this.$store.state.users[this.$route.params.id]
id: store.state.route.params.id }
})
}, },
preFetch: fetchUser,
mounted () { beforeMount () {
fetchUser(this.$store)
} }
} }
</script> </script>
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