mirror of
https://github.com/ershisan99/www.git
synced 2025-12-18 21:09:23 +00:00
fix player state
This commit is contained in:
@@ -30,26 +30,21 @@ export const playerStateRouter = createTRPCRouter({
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
.subscription(async function* ({ input, ctx, signal }) {
|
.subscription(async function* ({ input, ctx, signal }) {
|
||||||
console.log('subscription started for user:', input.userId)
|
|
||||||
|
|
||||||
try {
|
|
||||||
const iterator = createEventIterator<PlayerState>(
|
const iterator = createEventIterator<PlayerState>(
|
||||||
globalEmitter,
|
globalEmitter,
|
||||||
`state-change:${input.userId}`,
|
`state-change:${input.userId}`,
|
||||||
{ signal: signal }
|
{ signal: signal }
|
||||||
)
|
)
|
||||||
|
|
||||||
console.log('iterator created')
|
// get initial state
|
||||||
|
const initialState = await redis.get(PLAYER_STATE_KEY(input.userId))
|
||||||
for await (const [state] of iterator) {
|
if (initialState) {
|
||||||
console.log('emitting state:', state)
|
yield tracked('initial', JSON.parse(initialState) as PlayerState)
|
||||||
yield tracked(Date.now().toString(), state)
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
console.error('subscription error:', error)
|
// listen for updates
|
||||||
throw error
|
for await (const [state] of iterator) {
|
||||||
} finally {
|
yield tracked(Date.now().toString(), state)
|
||||||
console.log('subscription ended for user:', input.userId)
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user