Try cleanup threads

This commit is contained in:
2022-12-31 20:11:43 +00:00
parent 3947387619
commit 1abb2905f0
7 changed files with 191 additions and 29 deletions

View File

@ -71,7 +71,7 @@ export default function useEventPublisher() {
ev.Tags.push(new Tag(["e", thread.Reply.Id, "", "reply"], ev.Tags.length));
}
ev.Tags.push(new Tag(["p", replyTo.PubKey], ev.Tags.length));
for (let pk in thread.PubKeys) {
for (let pk of thread.PubKeys) {
ev.Tags.push(new Tag(["p", pk], ev.Tags.length));
}
} else {

View File

@ -1,7 +1,20 @@
import { useEffect, useMemo, useState } from "react";
import { useCallback, useEffect, useMemo, useReducer, useState } from "react";
import { System } from "..";
import { Subscriptions } from "../nostr/Subscriptions";
function notesReducer(state, ev) {
if (state.notes.some(a => a.id === ev.id)) {
return state;
}
return {
notes: [
...state.notes,
ev
]
}
}
/**
*
* @param {Subscriptions} sub
@ -9,7 +22,7 @@ import { Subscriptions } from "../nostr/Subscriptions";
* @returns
*/
export default function useSubscription(sub, opt) {
const [notes, setNotes] = useState([]);
const [state, dispatch] = useReducer(notesReducer, { notes: [] });
const options = {
leaveOpen: false,
@ -19,20 +32,12 @@ export default function useSubscription(sub, opt) {
useEffect(() => {
if (sub) {
sub.OnEvent = (e) => {
setNotes(n => {
if (Array.isArray(n) && !n.some(a => a.id === e.id)) {
return [
...n,
e
]
} else {
return n;
}
});
dispatch(e);
};
if (!options.leaveOpen) {
sub.OnEnd = (c) => {
sub.OnEvent = () => {};
c.RemoveSubscription(sub.Id);
if (sub.IsFinished()) {
System.RemoveSubscription(sub.Id);
@ -47,9 +52,5 @@ export default function useSubscription(sub, opt) {
}
}, [sub]);
useEffect(() => {
setNotes([]);
}, []);
return { notes, sub };
return state;
}

View File

@ -48,9 +48,7 @@ export default function useThreadFeed(id) {
const others = useSubscription(relatedThisSub, { leaveOpen: true });
return {
notes: [
...main.notes,
...others.notes
]
main: main.notes,
other: others.notes
};
}

View File

@ -5,6 +5,10 @@ import useSubscription from "./Subscription";
export default function useTimelineFeed(pubKeys) {
const sub = useMemo(() => {
if (pubKeys.length === 0) {
return null;
}
let sub = new Subscriptions();
sub.Id = "timeline";
sub.Authors = new Set(pubKeys);

View File

@ -5,7 +5,10 @@ import useThreadFeed from "../feed/ThreadFeed";
export default function EventPage() {
const params = useParams();
const id = params.id;
const { notes } = useThreadFeed(id);
return <Thread notes={notes} this={id}/>;
const { main, other } = useThreadFeed(id);
return <Thread notes={[
...main,
...other
].filter((v, i, a) => a.indexOf(b => b.id === v.id) === -1)} this={id} />;
}