Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/app/src/session/session-resolution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, test } from "bun:test"
import { createRoot } from "solid-js"
import { createSessionResolution } from "./session-resolution"

describe("session resolution", () => {
test("waits for a route session ID", () => {
createRoot((dispose) => {
let syncs = 0
const sessions = {
get: () => undefined,
sync: () => {
syncs++
return Promise.resolve()
},
}
const session = createSessionResolution(() => undefined, () => sessions)

expect(session()).toBeUndefined()
expect(syncs).toBe(0)
dispose()
})
})
})
12 changes: 9 additions & 3 deletions packages/app/src/session/session-resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ type Resolution<T> = { id: string; store: SessionStore<T> } & (
// session that simply has not resolved yet. Resolve failures rethrow on read so
// the enclosing SessionRouteErrorBoundary renders the scoped session error.
export function createSessionResolution<T>(
sessionID: () => string,
sessionID: () => string | undefined,
sessions: () => SessionStore<T>,
options?: { children?: boolean },
) {
const cached = createMemo(() => sessions().get(sessionID()))
const cached = createMemo(() => {
const id = sessionID()
if (!id) return
return sessions().get(id)
})
const [status, setStatus] = createSignal<Resolution<T>>()

createEffect(
on([sessionID, sessions] as const, ([id, store]) => {
if (!id) return
let stale = false
onCleanup(() => {
stale = true
Expand All @@ -60,10 +65,11 @@ export function createSessionResolution<T>(

return createMemo(() => {
const id = sessionID()
if (!id) return
const value = cached()
if (value) return value
const state = status()
if (state?.id !== id || state.store !== sessions()) return undefined
if (!state || state.id !== id || state.store !== sessions()) return undefined
if (state.state === "failed") throw state.failure
// A session missing after settlement was deleted, possibly by another client.
// Match the resolve error so the boundary shows the
Expand Down
Loading