Align sync as per the review

This commit is contained in:
nayan9617
2026-04-08 16:20:35 +05:30
committed by hodlbod
parent 35c27090c1
commit 2144bffc41
3 changed files with 23 additions and 90 deletions
+1
View File
@@ -28,6 +28,7 @@ node_modules/
.pnpm-store/
build/
.svelte-kit/
.next/
# Rust/Tauri
*target/
-18
View File
@@ -156,24 +156,6 @@ import {readFeed} from "@lib/feeds"
export const fromCsv = (s: string) => (s || "").split(",").filter(identity)
// Keep sync logic informed when the tab sleeps or wakes so subscriptions can be torn down cleanly.
export const documentVisibility = readable<DocumentVisibilityState>(
typeof document !== "undefined" ? document.visibilityState : "visible",
set => {
if (typeof document === "undefined") return
const onVisibilityChange = () => {
set(document.visibilityState)
}
document.addEventListener("visibilitychange", onVisibilityChange)
return () => {
document.removeEventListener("visibilitychange", onVisibilityChange)
}
},
)
export const ROOM = "h"
export const PROTECTED = ["-"]
+22 -72
View File
@@ -1,12 +1,12 @@
import {page} from "$app/stores"
import type {Unsubscriber} from "svelte/store"
import {derived, get} from "svelte/store"
import {last, call, ifLet, assoc, chunk, sleep, identity, WEEK, ago} from "@welshman/lib"
import {get} from "svelte/store"
import {last, call, ifLet, assoc, chunk, sleep, WEEK, ago} from "@welshman/lib"
import {PollResponse} from "nostr-tools/kinds"
import {merged} from "@welshman/store"
import {
getListTags,
getRelayTagValues,
type List,
WRAP,
ROOM_META,
ROOM_DELETE,
@@ -22,7 +22,7 @@ import {
unionFilters,
getTagValue,
} from "@welshman/util"
import type {Filter, TrustedEvent} from "@welshman/util"
import type {Filter, List, PublishedList, TrustedEvent} from "@welshman/util"
import {request, requestOne, Difference, DifferenceEvent} from "@welshman/net"
import {
pubkey,
@@ -46,7 +46,6 @@ import {
MESSAGE_KINDS,
CONTENT_KINDS,
INDEXER_RELAYS,
documentVisibility,
loadSettings,
loadGroupList,
userSpaceUrls,
@@ -238,7 +237,7 @@ const syncUserData = () => {
}
}
const syncRelayList = ($userRelayList: List | undefined) => {
const syncRelayList = ($userRelayList: PublishedList | undefined) => {
const pubkey = $userRelayList?.event?.pubkey
if (!pubkey) return
@@ -272,34 +271,17 @@ const syncUserData = () => {
}
}
const unsubscribeGroupList = derived([userGroupList, documentVisibility], identity).subscribe(
([$userGroupList, $visibility]) => {
if ($visibility === "hidden") {
unsubscribersByKey.forEach(call)
unsubscribersByKey.clear()
const unsubscribeGroupList = merged([userGroupList]).subscribe(([$userGroupList]) => {
syncGroupList($userGroupList)
})
return
}
const unsubscribeRelayList = merged([userRelayList]).subscribe(([$userRelayList]) => {
syncRelayList($userRelayList)
})
syncGroupList($userGroupList)
},
)
const unsubscribeRelayList = derived([userRelayList, documentVisibility], identity).subscribe(
([$userRelayList, $visibility]) => {
if ($visibility === "hidden") return
syncRelayList($userRelayList)
},
)
const unsubscribeFollows = derived([userFollowList, documentVisibility], identity).subscribe(
([$userFollowList, $visibility]) => {
if ($visibility === "hidden") return
syncFollowList()
},
)
const unsubscribeFollows = merged([userFollowList]).subscribe(() => {
syncFollowList()
})
return () => {
unsubscribersByKey.forEach(call)
@@ -364,22 +346,11 @@ const syncSpace = (url: string, rooms: string[]) => {
}
const syncSpaces = () => {
const store = derived([userGroupList, page, documentVisibility], identity)
const store = merged([userGroupList, page])
const unsubscribersByUrl = new Map<string, Unsubscriber>()
const roomsByUrl = new Map<string, string>()
const unsubscribe = store.subscribe(([$userGroupList, $page, $visibility]) => {
if ($visibility === "hidden") {
// Hidden tabs should drop every live space subscription so we restart from a clean slate on wake.
for (const unsubscribe of unsubscribersByUrl.values()) {
unsubscribe()
}
unsubscribersByUrl.clear()
roomsByUrl.clear()
return
}
const unsubscribe = store.subscribe(([$userGroupList, $page]) => {
const urls = new Set(getSpaceUrlsFromGroupList($userGroupList))
if ($page.params.relay) {
@@ -438,8 +409,6 @@ const syncDMs = () => {
let currentPubkey: string | undefined
let currentShouldUnwrap = false
// Late relay-list promises can resolve after a hide/show cycle, so keep the last visible state here.
let currentVisibility: DocumentVisibilityState = "visible"
const unsubscribeAll = () => {
for (const [url, unsubscribe] of unsubscribersByUrl.entries()) {
@@ -457,12 +426,7 @@ const syncDMs = () => {
loadRelayList($pubkey)
.then(() => loadMessagingRelayList($pubkey))
.then($l => {
if (
$l &&
currentVisibility === "visible" &&
currentPubkey === $pubkey &&
currentShouldUnwrap === $shouldUnwrap
) {
if ($l && currentPubkey === $pubkey && currentShouldUnwrap === $shouldUnwrap) {
subscribeAll($pubkey, getRelayTagValues(getListTags($l)))
}
})
@@ -498,27 +462,13 @@ const syncDMs = () => {
}
}
// When pubkey or visibility changes, re-sync
const unsubscribePubkey = derived([pubkey, shouldUnwrap, documentVisibility], identity).subscribe(
([$pubkey, $shouldUnwrap, $visibility]) => {
currentVisibility = $visibility
if ($visibility === "hidden") {
unsubscribeAll()
return
}
syncPubkey($pubkey, $shouldUnwrap)
},
)
const unsubscribePubkey = merged([pubkey, shouldUnwrap]).subscribe(([$pubkey, $shouldUnwrap]) => {
syncPubkey($pubkey, $shouldUnwrap)
})
// When user messaging relays change, update synchronization
const unsubscribeList = derived([userMessagingRelayList, documentVisibility], identity).subscribe(
([$userMessagingRelayList, $visibility]) => {
currentVisibility = $visibility
if ($visibility === "hidden") return
const unsubscribeList = merged([userMessagingRelayList]).subscribe(
([$userMessagingRelayList]) => {
syncList($userMessagingRelayList)
},
)