This repository has been archived by the owner on Nov 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.ts
162 lines (146 loc) · 3.81 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { MutationTree, ActionTree } from "vuex"
import Vue from "vue"
import { validFeeds } from "~/common/api"
import { lazy } from "~/common/utils"
// import { CancelToken } from "axios"
const baseUrl = "https://api.hnpwa.com/v0" // "/api"
interface Dictionary<T> {
[key: string]: T
}
// export interface Users {
// [key: string]: User
// }
export interface User {
created: string
created_time: number
id: string
karma: number
}
export interface Items {
[key: number]: Item
}
export interface Item {
comments: any[]
comments_count: number
content: string
domain: string
id: number
points: number
time: number | Date | undefined
time_ago: string
title: string
type: string
url: string
user: string
}
// export interface Feeds {
// [key: string]: Feed
// }
export interface Feed {
comments_count: number
domain: string
id: number
points: number
time: number | Date | undefined
time_ago: string
title: string
type: string
url: string
user: string
}
export interface IRootState {
items: Items
users: Dictionary<User>
feeds: Dictionary<Feed>
}
// =================================================
// State
// =================================================
const s = (): IRootState => {
const state = {
items: {
/* [id: number]: Item */
},
users: {
/* [id: string]: User */
},
feeds: {
/* [page: number] : [ [id: number] ] */
}
}
validFeeds.forEach((feed: string) => {
state.feeds[feed] = {}
})
return state
}
// =================================================
// Mutations
// =================================================
const mutations: MutationTree<IRootState> = {
SET_FEED: (state, { feed, ids, page }) => {
Vue.set(state.feeds[feed], page, ids)
},
SET_ITEM: (state, { item }: { item: Item }) => {
if (item) {
Vue.set(state.items as Item[], item.id, item)
}
},
SET_ITEMS: (state, { items }: { items: Item[] }) => {
items.forEach(item => {
if (item) {
Vue.set(state.items as Item[], item.id, item)
}
})
},
SET_USER: (state, { id, user }: { id: string; user: User }) => {
Vue.set(state.users, id, user || false) /* false means user not found */
}
}
// =================================================
// Actions
// =================================================
const actions: ActionTree<IRootState, any> = {
FETCH_FEED({ commit, state }, { feed, page, prefetch }) {
// Don't priorotize already fetched feeds
if (state.feeds[feed][page] && state.feeds[feed][page].length) {
prefetch = true
}
if (!prefetch) {
if ((this as any).feedCancelSource) {
;(this as any).feedCancelSource.cancel(
"prioritize feed: " + feed + " page: " + page
)
}
// ;(this as any).feedCancelSource = new CancelToken.source()
}
return lazy(
items => {
const ids = items.map(item => item.id)
commit("SET_FEED", { feed, ids, page })
commit("SET_ITEMS", { items })
},
() =>
(this as any).$axios.$get(`${baseUrl}/${feed}/${page}.json`, {
cancelToken:
(this as any).feedCancelSource &&
(this as any).feedCancelSource.token
}),
(state.feeds[feed][page] || []).map(id => state.items[id])
)
},
FETCH_ITEM({ commit, state }, { id }) {
return lazy(
item => commit("SET_ITEM", { item }),
() => (this as any).$axios.$get(`${baseUrl}/item/${id}.json`),
Object.assign({ id, loading: true, comments: [] }, state.items[id])
)
},
FETCH_USER({ state, commit }, { id }) {
return lazy(
user => commit("SET_USER", { id, user }),
() => (this as any).$axios.$get(`${baseUrl}/user/${id}.json`),
Object.assign({ id, loading: true }, state.users[id])
)
}
}
export { s as state, actions, mutations }