Skip to content

Commit

Permalink
remove unnecessary TSR queue
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Jan 24, 2025
1 parent a992b2c commit 768b6c3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 95 deletions.
2 changes: 0 additions & 2 deletions packages/start-client/src/ssr-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export interface StartSsrGlobal {
>
cleanScripts: () => void
dehydrated?: any
queue: Array<() => boolean>
runQueue: () => void
initMatch: (match: SsrMatch) => void
resolvePromise: (opts: {
matchId: string
Expand Down
154 changes: 61 additions & 93 deletions packages/start-server/src/tsrScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,114 +4,82 @@ import type { StartSsrGlobal } from '@tanstack/start-client'
const __TSR_SSR__: StartSsrGlobal = {
matches: [],
streamedValues: {},
queue: [],
runQueue: () => {
let changed = false as boolean
__TSR_SSR__.queue = __TSR_SSR__.queue.filter((fn) => {
if (fn()) {
changed = true
return false
}
return true
})
if (changed) {
__TSR_SSR__.runQueue()
}
},
initMatch: (match) => {
__TSR_SSR__.queue.push(() => {
__TSR_SSR__.matches.push(match)
__TSR_SSR__.matches.push(match)

Object.entries(match.extracted).forEach(([_id, ex]) => {
if (ex.type === 'stream') {
let controller
ex.value = new ReadableStream({
start(c) {
controller = {
enqueue: (chunk: unknown) => {
try {
c.enqueue(chunk)
} catch {}
},
close: () => {
try {
c.close()
} catch {}
},
}
},
})
ex.value.controller = controller
} else {
let resolve: ControllablePromise['reject'] | undefined
let reject: ControllablePromise['reject'] | undefined
Object.entries(match.extracted).forEach(([_id, ex]) => {
if (ex.type === 'stream') {
let controller
ex.value = new ReadableStream({
start(c) {
controller = {
enqueue: (chunk: unknown) => {
try {
c.enqueue(chunk)
} catch {}
},
close: () => {
try {
c.close()
} catch {}
},
}
},
})
ex.value.controller = controller
} else {
let resolve: ControllablePromise['reject'] | undefined
let reject: ControllablePromise['reject'] | undefined

ex.value = new Promise((_resolve, _reject) => {
reject = _reject
resolve = _resolve
}) as ControllablePromise
ex.value.reject = reject!
ex.value.resolve = resolve!
}
})

return true
ex.value = new Promise((_resolve, _reject) => {
reject = _reject
resolve = _resolve
}) as ControllablePromise
ex.value.reject = reject!
ex.value.resolve = resolve!
}
})

__TSR_SSR__.runQueue()
return true
},
resolvePromise: ({ matchId, id, promiseState }) => {
__TSR_SSR__.queue.push(() => {
const match = __TSR_SSR__.matches.find((m) => m.id === matchId)
if (match) {
const ex = match.extracted[id]
if (
ex &&
ex.type === 'promise' &&
ex.value &&
promiseState.status === 'success'
) {
ex.value.resolve(promiseState.data)
return true
}
const match = __TSR_SSR__.matches.find((m) => m.id === matchId)
if (match) {
const ex = match.extracted[id]
if (
ex &&
ex.type === 'promise' &&
ex.value &&
promiseState.status === 'success'
) {
ex.value.resolve(promiseState.data)
return true
}
return false
})

__TSR_SSR__.runQueue()
}
return false
},
injectChunk: ({ matchId, id, chunk }) => {
__TSR_SSR__.queue.push(() => {
const match = __TSR_SSR__.matches.find((m) => m.id === matchId)
const match = __TSR_SSR__.matches.find((m) => m.id === matchId)

if (match) {
const ex = match.extracted[id]
if (ex && ex.type === 'stream' && ex.value?.controller) {
ex.value.controller.enqueue(
new TextEncoder().encode(chunk.toString()),
)
return true
}
if (match) {
const ex = match.extracted[id]
if (ex && ex.type === 'stream' && ex.value?.controller) {
ex.value.controller.enqueue(new TextEncoder().encode(chunk.toString()))
return true
}
return false
})

__TSR_SSR__.runQueue()
}
return false
},
closeStream: ({ matchId, id }) => {
__TSR_SSR__.queue.push(() => {
const match = __TSR_SSR__.matches.find((m) => m.id === matchId)
if (match) {
const ex = match.extracted[id]
if (ex && ex.type === 'stream' && ex.value?.controller) {
ex.value.controller.close()
return true
}
const match = __TSR_SSR__.matches.find((m) => m.id === matchId)
if (match) {
const ex = match.extracted[id]
if (ex && ex.type === 'stream' && ex.value?.controller) {
ex.value.controller.close()
return true
}
return false
})

__TSR_SSR__.runQueue()
}
return false
},
cleanScripts: () => {
document.querySelectorAll('.tsr-once').forEach((el) => {
Expand Down

0 comments on commit 768b6c3

Please sign in to comment.