Align sync as per the review
This commit is contained in:
@@ -28,6 +28,7 @@ node_modules/
|
|||||||
.pnpm-store/
|
.pnpm-store/
|
||||||
build/
|
build/
|
||||||
.svelte-kit/
|
.svelte-kit/
|
||||||
|
.next/
|
||||||
|
|
||||||
# Rust/Tauri
|
# Rust/Tauri
|
||||||
*target/
|
*target/
|
||||||
|
|||||||
@@ -156,24 +156,6 @@ import {readFeed} from "@lib/feeds"
|
|||||||
|
|
||||||
export const fromCsv = (s: string) => (s || "").split(",").filter(identity)
|
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 ROOM = "h"
|
||||||
|
|
||||||
export const PROTECTED = ["-"]
|
export const PROTECTED = ["-"]
|
||||||
|
|||||||
+22
-72
@@ -1,12 +1,12 @@
|
|||||||
import {page} from "$app/stores"
|
import {page} from "$app/stores"
|
||||||
import type {Unsubscriber} from "svelte/store"
|
import type {Unsubscriber} from "svelte/store"
|
||||||
import {derived, get} from "svelte/store"
|
import {get} from "svelte/store"
|
||||||
import {last, call, ifLet, assoc, chunk, sleep, identity, WEEK, ago} from "@welshman/lib"
|
import {last, call, ifLet, assoc, chunk, sleep, WEEK, ago} from "@welshman/lib"
|
||||||
import {PollResponse} from "nostr-tools/kinds"
|
import {PollResponse} from "nostr-tools/kinds"
|
||||||
|
import {merged} from "@welshman/store"
|
||||||
import {
|
import {
|
||||||
getListTags,
|
getListTags,
|
||||||
getRelayTagValues,
|
getRelayTagValues,
|
||||||
type List,
|
|
||||||
WRAP,
|
WRAP,
|
||||||
ROOM_META,
|
ROOM_META,
|
||||||
ROOM_DELETE,
|
ROOM_DELETE,
|
||||||
@@ -22,7 +22,7 @@ import {
|
|||||||
unionFilters,
|
unionFilters,
|
||||||
getTagValue,
|
getTagValue,
|
||||||
} from "@welshman/util"
|
} 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 {request, requestOne, Difference, DifferenceEvent} from "@welshman/net"
|
||||||
import {
|
import {
|
||||||
pubkey,
|
pubkey,
|
||||||
@@ -46,7 +46,6 @@ import {
|
|||||||
MESSAGE_KINDS,
|
MESSAGE_KINDS,
|
||||||
CONTENT_KINDS,
|
CONTENT_KINDS,
|
||||||
INDEXER_RELAYS,
|
INDEXER_RELAYS,
|
||||||
documentVisibility,
|
|
||||||
loadSettings,
|
loadSettings,
|
||||||
loadGroupList,
|
loadGroupList,
|
||||||
userSpaceUrls,
|
userSpaceUrls,
|
||||||
@@ -238,7 +237,7 @@ const syncUserData = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const syncRelayList = ($userRelayList: List | undefined) => {
|
const syncRelayList = ($userRelayList: PublishedList | undefined) => {
|
||||||
const pubkey = $userRelayList?.event?.pubkey
|
const pubkey = $userRelayList?.event?.pubkey
|
||||||
|
|
||||||
if (!pubkey) return
|
if (!pubkey) return
|
||||||
@@ -272,34 +271,17 @@ const syncUserData = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsubscribeGroupList = derived([userGroupList, documentVisibility], identity).subscribe(
|
const unsubscribeGroupList = merged([userGroupList]).subscribe(([$userGroupList]) => {
|
||||||
([$userGroupList, $visibility]) => {
|
syncGroupList($userGroupList)
|
||||||
if ($visibility === "hidden") {
|
})
|
||||||
unsubscribersByKey.forEach(call)
|
|
||||||
unsubscribersByKey.clear()
|
|
||||||
|
|
||||||
return
|
const unsubscribeRelayList = merged([userRelayList]).subscribe(([$userRelayList]) => {
|
||||||
}
|
syncRelayList($userRelayList)
|
||||||
|
})
|
||||||
|
|
||||||
syncGroupList($userGroupList)
|
const unsubscribeFollows = merged([userFollowList]).subscribe(() => {
|
||||||
},
|
syncFollowList()
|
||||||
)
|
})
|
||||||
|
|
||||||
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()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
unsubscribersByKey.forEach(call)
|
unsubscribersByKey.forEach(call)
|
||||||
@@ -364,22 +346,11 @@ const syncSpace = (url: string, rooms: string[]) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const syncSpaces = () => {
|
const syncSpaces = () => {
|
||||||
const store = derived([userGroupList, page, documentVisibility], identity)
|
const store = merged([userGroupList, page])
|
||||||
const unsubscribersByUrl = new Map<string, Unsubscriber>()
|
const unsubscribersByUrl = new Map<string, Unsubscriber>()
|
||||||
const roomsByUrl = new Map<string, string>()
|
const roomsByUrl = new Map<string, string>()
|
||||||
|
|
||||||
const unsubscribe = store.subscribe(([$userGroupList, $page, $visibility]) => {
|
const unsubscribe = store.subscribe(([$userGroupList, $page]) => {
|
||||||
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 urls = new Set(getSpaceUrlsFromGroupList($userGroupList))
|
const urls = new Set(getSpaceUrlsFromGroupList($userGroupList))
|
||||||
|
|
||||||
if ($page.params.relay) {
|
if ($page.params.relay) {
|
||||||
@@ -438,8 +409,6 @@ const syncDMs = () => {
|
|||||||
|
|
||||||
let currentPubkey: string | undefined
|
let currentPubkey: string | undefined
|
||||||
let currentShouldUnwrap = false
|
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 = () => {
|
const unsubscribeAll = () => {
|
||||||
for (const [url, unsubscribe] of unsubscribersByUrl.entries()) {
|
for (const [url, unsubscribe] of unsubscribersByUrl.entries()) {
|
||||||
@@ -457,12 +426,7 @@ const syncDMs = () => {
|
|||||||
loadRelayList($pubkey)
|
loadRelayList($pubkey)
|
||||||
.then(() => loadMessagingRelayList($pubkey))
|
.then(() => loadMessagingRelayList($pubkey))
|
||||||
.then($l => {
|
.then($l => {
|
||||||
if (
|
if ($l && currentPubkey === $pubkey && currentShouldUnwrap === $shouldUnwrap) {
|
||||||
$l &&
|
|
||||||
currentVisibility === "visible" &&
|
|
||||||
currentPubkey === $pubkey &&
|
|
||||||
currentShouldUnwrap === $shouldUnwrap
|
|
||||||
) {
|
|
||||||
subscribeAll($pubkey, getRelayTagValues(getListTags($l)))
|
subscribeAll($pubkey, getRelayTagValues(getListTags($l)))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -498,27 +462,13 @@ const syncDMs = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When pubkey or visibility changes, re-sync
|
const unsubscribePubkey = merged([pubkey, shouldUnwrap]).subscribe(([$pubkey, $shouldUnwrap]) => {
|
||||||
const unsubscribePubkey = derived([pubkey, shouldUnwrap, documentVisibility], identity).subscribe(
|
syncPubkey($pubkey, $shouldUnwrap)
|
||||||
([$pubkey, $shouldUnwrap, $visibility]) => {
|
})
|
||||||
currentVisibility = $visibility
|
|
||||||
|
|
||||||
if ($visibility === "hidden") {
|
|
||||||
unsubscribeAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
syncPubkey($pubkey, $shouldUnwrap)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
// When user messaging relays change, update synchronization
|
// When user messaging relays change, update synchronization
|
||||||
const unsubscribeList = derived([userMessagingRelayList, documentVisibility], identity).subscribe(
|
const unsubscribeList = merged([userMessagingRelayList]).subscribe(
|
||||||
([$userMessagingRelayList, $visibility]) => {
|
([$userMessagingRelayList]) => {
|
||||||
currentVisibility = $visibility
|
|
||||||
|
|
||||||
if ($visibility === "hidden") return
|
|
||||||
|
|
||||||
syncList($userMessagingRelayList)
|
syncList($userMessagingRelayList)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user