Skip to content

Commit f792977

Browse files
committed
add SetTimeout
1 parent 3622f95 commit f792977

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/main.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ initI18n()
2929

3030
const watched = initView(state, elements)
3131

32+
const updateFeeds = () => {
33+
const promises = watched.feeds.map(feed =>
34+
loadFeed(feed.url)
35+
.then(xml => parseFeed(xml))
36+
.then(({ posts }) => {
37+
const existingLinks = watched.posts
38+
.filter(p => p.feedId === feed.id)
39+
.map(p => p.link)
40+
41+
const newPosts = posts.filter(p => !existingLinks.includes(p.link))
42+
43+
if (newPosts.length > 0) {
44+
watched.posts.push(...newPosts)
45+
}
46+
})
47+
.catch(() => {}),
48+
)
49+
50+
Promise.all(promises).finally(() => {
51+
setTimeout(updateFeeds, 5000)
52+
})
53+
}
54+
3255
elements.form.addEventListener('submit', (e) => {
3356
e.preventDefault()
3457

@@ -65,6 +88,10 @@ initI18n()
6588
watched.feeds.push(normalizedFeed)
6689
watched.posts.push(...normalizedPosts)
6790
watched.form.status = 'success'
91+
92+
if (watched.feeds.length === 1) {
93+
updateFeeds()
94+
}
6895
})
6996
.catch((err) => {
7097
// errors

src/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import generateId from './generateId'
22

3-
export const parseFeed = xmlString => {
3+
export const parseFeed = (xmlString) => {
44
const parser = new DOMParser()
55
const doc = parser.parseFromString(xmlString, 'text/xml')
66

0 commit comments

Comments
 (0)