-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchManager.js
106 lines (85 loc) · 2.87 KB
/
fetchManager.js
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
const workerSegregateList = new Worker('workerSegregateList.js')
const workerUpdateList = new Worker('workerUpdateList.js')
const urls = {
top: 'topstories.json',
new: 'newstories.json',
best: 'beststories.json',
ask: 'askstories.json',
show: 'showstories.json',
jobs: 'jobstories.json'
}
const url = 'https://hacker-news.firebaseio.com/v0/'
workerUpdateList.postMessage(['url', url, urls])
workerSegregateList.onmessage = handleMessages
workerUpdateList.onmessage = handleMessages
for (let story in urls) {
app.segregatedList[story] = []
app.stories[story] = []
}
fetch(`${url}${urls['top']}`).then(toJSON).then(getTopFifteen).catch(console.log)
for (let storyType in urls) {
fetch(`${url}${urls[storyType]}`).then(toJSON).then(list => { workerSegregateList.postMessage([storyType, list]) }).catch(console.log)
}
function toJSON (data) {
return data.json()
}
function getTopFifteen (list) {
fetchEachItem('top', 0, list.slice(0, 15))
}
function fetchEachItem (storyType, index, list) {
app.stories[storyType][index] = []
for (let id of list) {
app.stories[storyType][index].push(fetch(`${url}item/${id}.json`).then(toJSON))
}
}
function handleMessages (e) {
if (e.data[0] === 'list') {
app.segregatedList[e.data[1]][e.data[2]] = e.data[3]
} else if (e.data[0] === 'updatedList') {
console.log(e.data)
processUpdatedList(e.data)
}
}
function fetchItems (storyType, index, list = app.segregatedList) {
workerUpdateList.postMessage(['updateList', storyType, index, list[storyType][index]])
let stories = app.stories[storyType][index]
if (!stories) {
fetchEachItem(storyType, index, list[storyType][index])
}
}
function processUpdatedList ([_, storyType, index, oldList, newList]) {
updateMainList(storyType, index, newList)
fetchToUpdate(storyType, index, newList)
}
function updateMainList (storyType, index, newList) {
for (let i in newList) {
app.segregatedList[storyType][index].splice(i, 1, newList[i])
}
}
function fetchToUpdate (storyType, index, newList) {
for (let i in newList) {
app.stories[storyType][index].splice(i, 1, fetch(`${url}/item/${newList[i]}.json`).then(toJSON))
}
}
function getComments (storyType, index, storyId) {
let story = getStory(storyType, index, storyId)
story.then(fetchComments)
}
function getStory (storyType, index, storyId) {
let storyIndex = app.segregatedList[storyType][index].indexOf(storyId)
return app.stories[storyType][index][storyIndex]
}
function fetchComments (item) {
accumulateKids(item)
app.comments[item.id] = []
item.kids.forEach(id => {
app.comments[item.id].push(fetch(`${url}/item/${id}.json`).then(toJSON))
})
}
function accumulateKids (item) {
app.kidsList[item.id] = item.kids
}
function getReplies (commentId, parentId) {
let indexOfComment = app.kidsList[parentId].indexOf(commentId)
app.comments[parentId][indexOfComment].then(fetchComments)
}