Optimize future notes fetching

This commit is contained in:
Bojan Mojsilovic 2024-03-11 22:47:39 +01:00
parent a1ee76afd2
commit a194aaa04f

View File

@ -1,6 +1,6 @@
import { nip19 } from "nostr-tools";
import { createContext, createEffect, onCleanup, useContext } from "solid-js";
import { createStore, reconcile } from "solid-js/store";
import { createStore, reconcile, unwrap } from "solid-js/store";
import { APP_ID } from "../App";
import { Kind } from "../constants";
import { getEvents, getExploreFeed, getFeed, getFutureExploreFeed, getFutureFeed } from "../lib/feed";
@ -40,6 +40,9 @@ type HomeContextStore = {
notes: PrimalNote[],
page: FeedPage,
reposts: Record<string, string> | undefined,
scope: string,
timeframe: string,
latest_at: number,
},
sidebar: {
notes: PrimalNote[],
@ -88,6 +91,9 @@ const initialHomeData = {
mentions: {},
noteActions: {},
},
scope: '',
timeframe: '',
latest_at: 0,
},
sidebar: {
notes: [],
@ -203,6 +209,9 @@ export const HomeProvider = (props: { children: ContextChildren }) => {
mentions: {},
noteActions: {},
},
scope: '',
timeframe: '',
latest_at: 0,
}))
}
@ -227,6 +236,12 @@ export const HomeProvider = (props: { children: ContextChildren }) => {
const [scope, timeframe] = topic.split(';');
if (scope !== store.future.scope || timeframe !== store.future.timeframe) {
clearFuture();
updateStore('future', 'scope', () => scope);
updateStore('future', 'timeframe', () => timeframe);
}
let since = 0;
if (store.notes[0]) {
@ -235,7 +250,21 @@ export const HomeProvider = (props: { children: ContextChildren }) => {
store.notes[0].post.created_at;
}
clearFuture();
if (store.future.notes[0]) {
const lastFutureNote = unwrap(store.future.notes).sort((a, b) => b.post.created_at - a.post.created_at)[0];
since = lastFutureNote.repost ?
lastFutureNote.repost.note.created_at :
lastFutureNote.post.created_at;
}
updateStore('future', 'page', () =>({
messages: [],
users: {},
postStats: {},
mentions: {},
noteActions: {},
}))
if (scope && timeframe) {
if (timeframe !== 'latest') {
@ -383,13 +412,20 @@ export const HomeProvider = (props: { children: ContextChildren }) => {
store.notes[0]?.post?.noteId === messageId :
store.notes[0]?.repost?.note.noteId === messageId;
const scopeNotes = store[scope].notes;
const isaAlreadyIn = message.kind === Kind.Text &&
scopeNotes &&
scopeNotes.find(n => n.post.noteId === messageId);
let isAlreadyReposted = isRepostInCollection(store[scope].page.messages, message);
// const isAlreadyFetched = message.kind === Kind.Text ?
// store.future.notes[0]?.post?.noteId === messageId :
// store.future.notes[0]?.repost?.note.noteId === messageId;
if (isFirstNote || isAlreadyReposted) return;
if (isFirstNote || isaAlreadyIn || isAlreadyReposted) return;
updateStore(scope, 'page', 'messages',
(msgs) => [ ...msgs, { ...message }]
@ -490,6 +526,7 @@ export const HomeProvider = (props: { children: ContextChildren }) => {
const topic = (store.selectedFeed?.hex || '').split(';');
const sortingFunction = sortingPlan(topic[1]);
const newPosts = sortingFunction(convertToNotes(page));
saveNotes(newPosts, scope);