cleanup: switch to state-based post feeds

This commit is contained in:
James Xylight committed 2025-03-08 13:06:17 -08:00
1 parent 0236928bb9
commit 897a3c363c
10 files changed
+79 -85

No files matched your search

@@ -7,7 +7,6 @@ import type {
ListingType,
SortType,
} from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
export const shouldReload = (
cache: PostFeed | undefined,
@@ -30,14 +29,15 @@ export async function postFeed(args: {
init?: RequestInit | undefined,
) => Promise<Response>
}) {
const feed = get(postFeeds)[args.id]
const feed = postFeeds.value[args.id]
const posts = shouldReload(feed, args.url, instance.data)
? await client({ func: args.fetch }).getPosts(args.request)
: feed.data.posts
if (shouldReload(feed, args.url, instance.data) && browser)
postFeeds.updateFeed(args.id, {
postFeeds.value[args.id] = {
id: args.id,
data: {
...args.request,
posts: posts,
@@ -49,10 +49,10 @@ export async function postFeed(args: {
url: args.url,
lastSeen: 0,
instance: instance.data,
})
}
return (
get(postFeeds)[args.id]?.data ?? {
postFeeds.value[args.id]?.data ?? {
...args.request,
posts: posts,
cursor: {
@@ -67,28 +67,18 @@ export function getPostFeed(feeds: Map<PostFeedID, PostFeed>, id: PostFeedID) {
return feeds.get(id)
}
function createPostFeedsStore() {
// @ts-ignore
const { subscribe, update, set } = writable<Record<PostFeedID, PostFeed>>({})
class PostFeedState {
#value: Record<PostFeedID, PostFeed> = $state({}) as Record<
PostFeedID,
PostFeed
>
return {
subscribe,
addFeed: (id: PostFeedID, feed: PostFeed) =>
update((feeds) => ({ ...feeds, [id]: feed })),
updateFeed: (id: PostFeedID, feed: Partial<PostFeed>) =>
update((feeds) => {
return {
...feeds,
[id]: { ...feeds[id], ...feed } as PostFeed,
}
}),
removeFeed: (id: PostFeedID) =>
update((feeds) => {
const { [id]: _, ...rest } = feeds
return rest as Record<PostFeedID, PostFeed>
}),
clearAll: () => set({} as Record<PostFeedID, PostFeed>),
set,
get value() {
return this.#value
}
set value(value) {
this.#value = value
}
}
@@ -108,4 +98,4 @@ export interface PostFeed {
id: PostFeedID
}
export let postFeeds = createPostFeedsStore()
export let postFeeds = new PostFeedState()