File tree 3 files changed +40
-3
lines changed
3 files changed +40
-3
lines changed Original file line number Diff line number Diff line change 27
27
<script >
28
28
import Spinner from ' ./Spinner.vue'
29
29
import NewsItem from ' ./NewsItem.vue'
30
+ import { watchList } from ' ../store/api'
30
31
31
32
export default {
32
33
name: ' NewsList' ,
@@ -46,7 +47,9 @@ export default {
46
47
loading: false ,
47
48
transition: ' slide-left' ,
48
49
// if this is the initial render, directly render with the store state
49
- // otherwise this is a page switch, start with blank and wait for data load
50
+ // otherwise this is a page switch, start with blank and wait for data load.
51
+ // we need these local state so that we can precisely control the timing
52
+ // of the transitions.
50
53
displayedPage: isInitialRender ? Number (this .$store .state .route .params .page ) || 1 : - 1 ,
51
54
displayedItems: isInitialRender ? this .$store .getters .activeItems : []
52
55
}
@@ -69,6 +72,16 @@ export default {
69
72
if (this .$root ._isMounted ) {
70
73
this .loadItems (this .page )
71
74
}
75
+ this .unwatchList = watchList (this .type , ids => {
76
+ this .$store .commit (' SET_LIST' , { type: this .type , ids })
77
+ this .$store .dispatch (' FETCH_ACTIVE_ITEMS' ).then (() => {
78
+ this .displayedItems = this .$store .getters .activeItems
79
+ })
80
+ })
81
+ },
82
+
83
+ destroyed () {
84
+ this .unwatchList ()
72
85
},
73
86
74
87
watch: {
Original file line number Diff line number Diff line change @@ -71,3 +71,19 @@ export function fetchItem (id, forceRefresh) {
71
71
export function fetchItems ( ids ) {
72
72
return Promise . all ( ids . map ( id => fetchItem ( id ) ) )
73
73
}
74
+
75
+ export function watchList ( type , cb ) {
76
+ let first = true
77
+ const ref = api . child ( `${ type } stories` )
78
+ const handler = snapshot => {
79
+ if ( first ) {
80
+ first = false
81
+ } else {
82
+ cb ( snapshot . val ( ) )
83
+ }
84
+ }
85
+ ref . on ( 'value' , handler )
86
+ return ( ) => {
87
+ ref . off ( 'value' , handler )
88
+ }
89
+ }
Original file line number Diff line number Diff line change @@ -26,8 +26,16 @@ const store = new Vuex.Store({
26
26
commit ( 'SET_ACTIVE_TYPE' , { type } )
27
27
return fetchIdsByType ( type )
28
28
. then ( ids => commit ( 'SET_LIST' , { type, ids } ) )
29
- . then ( ( ) => fetchItems ( getters . activeIds . filter ( id => ! state . items [ id ] ) ) )
30
- . then ( items => commit ( 'SET_ITEMS' , { items } ) )
29
+ . then ( ( ) => dispatch ( 'FETCH_ACTIVE_ITEMS' ) )
30
+ } ,
31
+
32
+ FETCH_ACTIVE_ITEMS : ( { commit, state, getters } ) => {
33
+ const ids = getters . activeIds . filter ( id => ! state . items [ id ] )
34
+ if ( ids . length ) {
35
+ return fetchItems ( ids ) . then ( items => commit ( 'SET_ITEMS' , { items } ) )
36
+ } else {
37
+ return Promise . resolve ( )
38
+ }
31
39
}
32
40
} ,
33
41
You can’t perform that action at this time.
0 commit comments