-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathrefresh.ts
More file actions
183 lines (160 loc) · 4.72 KB
/
refresh.ts
File metadata and controls
183 lines (160 loc) · 4.72 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { loadValue } from '@logux/client'
import { atom, computed, map } from 'nanostores'
import { getEnvironment } from './environment.ts'
import {
changeFeed,
type FeedValue,
getFeed,
getFeedLatestPosts,
getFeeds
} from './feed.ts'
import { loadFilters } from './filter.ts'
import { createQueue } from './lib/queue.ts'
import { increaseKey } from './lib/stores.ts'
import { addPost, type OriginPost, processOriginPost } from './post.ts'
import type { PostsList } from './posts-list.ts'
export const DEFAULT_REFRESH_STATISTICS = {
errorFeeds: 0,
errorRequests: 0,
foundFast: 0,
foundSlow: 0,
initializing: false,
processedFeeds: 0,
totalFeeds: 0
}
export type RefreshStatistics = typeof DEFAULT_REFRESH_STATISTICS
export const refreshStatistics = map({ ...DEFAULT_REFRESH_STATISTICS })
export type RefreshError = { error: Error; feed: FeedValue }
export const refreshErrors = atom<RefreshError[]>([])
export type refreshStatusValue =
| 'done'
| 'error'
| 'refreshing'
| 'refreshingError'
| 'start'
export const refreshStatus = atom<refreshStatusValue>('start')
export const isRefreshing = computed(refreshStatus, icon => {
return icon.startsWith('refreshing')
})
let doneTimeout: NodeJS.Timeout | undefined
export const refreshProgress = computed(refreshStatistics, stats => {
if (stats.initializing || stats.totalFeeds === 0) {
return 0
} else {
return Math.floor((stats.processedFeeds / stats.totalFeeds) * 100) / 100
}
})
let queue = createQueue<FeedValue>([])
function wasAlreadyAdded(feed: FeedValue, origin: OriginPost): boolean {
if (origin.publishedAt && feed.lastPublishedAt) {
return origin.publishedAt <= feed.lastPublishedAt
} else {
return origin.originId === feed.lastOriginId
}
}
async function addPosts(feed: FeedValue, posts: OriginPost[]): Promise<void> {
let first = posts[0]
if (!first) return
if (first.publishedAt) {
posts.sort((a, b) => {
return (b.publishedAt ?? 0) - (a.publishedAt ?? 0)
})
first = posts[0]!
}
if (wasAlreadyAdded(feed, first)) return
let filters = await loadFilters({ feedId: feed.id })
for (let origin of posts) {
if (wasAlreadyAdded(feed, origin)) {
break
}
let reading = filters(origin) ?? feed.reading
if (reading !== 'delete') {
await addPost(processOriginPost(origin, feed.id, reading))
if (reading === 'fast') {
increaseKey(refreshStatistics, 'foundFast')
} else {
increaseKey(refreshStatistics, 'foundSlow')
}
}
}
await changeFeed(feed.id, {
lastOriginId: first.originId,
lastPublishedAt: first.publishedAt
})
}
async function checkForNextPage(
feed: FeedValue,
pages: PostsList
): Promise<void> {
let enough = pages.get().list.some(i => wasAlreadyAdded(feed, i))
if (!enough && pages.get().hasNext) {
queue.add(feed, async () => {
await pages.next()
let error = pages.get().error
if (error) throw error
await checkForNextPage(feed, pages)
})
} else {
if (!getFeed(feed.id).deleted) {
await addPosts(feed, pages.get().list)
}
await changeFeed(feed.id, {
refreshedAt: Math.round(Date.now() / 1000)
})
increaseKey(refreshStatistics, 'processedFeeds')
}
}
export async function refreshPosts(): Promise<void> {
if (isRefreshing.get()) return
if (doneTimeout) {
clearTimeout(doneTimeout)
doneTimeout = undefined
}
refreshStatus.set('refreshing')
refreshErrors.set([])
refreshStatistics.set({ ...DEFAULT_REFRESH_STATISTICS, initializing: true })
let feeds = await loadValue(getFeeds())
refreshStatistics.set({
...refreshStatistics.get(),
initializing: false,
totalFeeds: feeds.list.length
})
queue = createQueue(feeds.list)
await queue.start(
6,
feed => {
return async task => {
let pages = getFeedLatestPosts(feed, task)
if (pages.get().isLoading) await pages.loading
let error = pages.get().error
if (error) throw error
await checkForNextPage(feed, pages)
}
},
{
onRequestError() {
increaseKey(refreshStatistics, 'errorRequests')
},
onTaskFail(feed, error) {
getEnvironment().warn(error)
refreshErrors.set([...refreshErrors.get(), { error, feed }])
refreshStatus.set('refreshingError')
increaseKey(refreshStatistics, 'errorFeeds')
increaseKey(refreshStatistics, 'processedFeeds')
}
}
)
if (refreshStatus.get() === 'refreshingError') {
refreshStatus.set('error')
} else {
refreshStatus.set('done')
doneTimeout = setTimeout(() => {
refreshStatus.set('start')
}, 1000)
}
}
export function stopRefreshing(): void {
if (!isRefreshing.get()) return
refreshStatus.set('start')
queue.stop()
}