forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.ts
141 lines (123 loc) · 3.36 KB
/
feed.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
import {
changeSyncMapById,
createFilter,
createSyncMap,
deleteSyncMapById,
type Filter,
type FilterStore,
loadValue,
type SyncMapStore,
syncMapTemplate
} from '@logux/client'
import { nanoid } from 'nanoid'
import { atom, onMount } from 'nanostores'
import { client, getClient } from './client.ts'
import { createDownloadTask } from './download.ts'
import { type OptionalId, readonlyExport } from './lib/stores.ts'
import { type LoaderName, loaders } from './loader/index.ts'
import { deletePost, loadPosts, recalcPostsReading } from './post.ts'
import type { PostsList } from './posts-list.ts'
export type FeedValue = {
categoryId: string
id: string
lastOriginId?: string
lastPublishedAt?: number
loader: LoaderName
reading: 'fast' | 'slow'
title: string
url: string
}
export const Feed = syncMapTemplate<FeedValue>('feeds', {
offline: true,
remote: false
})
let $hasFeeds = atom<boolean | undefined>(false)
export const hasFeeds = readonlyExport($hasFeeds)
onMount($hasFeeds, () => {
let unbindFeeds: (() => void) | undefined
let unbindClient = client.subscribe(enabled => {
unbindFeeds?.()
unbindFeeds = undefined
if (enabled) {
unbindFeeds = getFeeds().subscribe(feeds => {
if (feeds.isLoading) {
$hasFeeds.set(undefined)
} else {
$hasFeeds.set(!feeds.isEmpty)
}
})
}
})
/* c8 ignore next 4 */
return () => {
unbindClient()
unbindFeeds?.()
}
})
export function getFeeds(
filter: Filter<FeedValue> = {}
): FilterStore<FeedValue> {
return createFilter(getClient(), Feed, filter)
}
export async function loadFeeds(
filter: Filter<FeedValue> = {}
): Promise<FeedValue[]> {
let value = await loadValue(getFeeds(filter))
return value.list
}
export async function addFeed(fields: OptionalId<FeedValue>): Promise<string> {
let id = fields.id ?? nanoid()
await createSyncMap(getClient(), Feed, { id, ...fields })
return id
}
export async function deleteFeed(feedId: string): Promise<void> {
let feed = Feed.cache[feedId]
if (feed) feed.deleted = true
let posts = await loadPosts({ feedId })
await Promise.all(posts.map(post => deletePost(post.id)))
return deleteSyncMapById(getClient(), Feed, feedId)
}
export function getFeed(feedId: string): SyncMapStore<FeedValue> {
return Feed(feedId, getClient())
}
export async function loadFeed(feedId: string): Promise<FeedValue | undefined> {
return loadValue(Feed(feedId, getClient()))
}
export async function changeFeed(
feedId: string,
changes: Partial<FeedValue>
): Promise<void> {
await changeSyncMapById(getClient(), Feed, feedId, changes)
await recalcPostsReading(feedId)
}
export function getFeedLatestPosts(
feed: FeedValue,
task = createDownloadTask()
): PostsList {
return loaders[feed.loader].getPosts(task, feed.url)
}
let testFeedId = 0
export function testFeed(feed: Partial<FeedValue> = {}): FeedValue {
testFeedId += 1
return {
categoryId: 'general',
id: `feed-${testFeedId}`,
lastOriginId: undefined,
lastPublishedAt: undefined,
loader: 'rss',
reading: 'fast',
title: `Test ${testFeedId}`,
url: `http://example.com/${testFeedId}`,
...feed
}
}
export const BROKEN_FEED: FeedValue = {
categoryId: 'general',
id: 'missed',
lastOriginId: undefined,
lastPublishedAt: undefined,
loader: 'atom',
reading: 'fast',
title: 'Missed',
url: ''
}