Refactoring
All checks were successful
Lint / lint (push) Successful in 11s

This commit is contained in:
Julien Valverdé
2024-09-07 19:10:42 +02:00
parent 639285af82
commit 527b8a8f22
3 changed files with 48 additions and 63 deletions

View File

@@ -1,10 +1,18 @@
import type { AnyRouter } from "@trpc/server"
import type { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Config, Context, Effect, Layer } from "effect"
import type ws from "ws"
import { ExpressNodeHTTPServer } from "../Express"
import { ImportError } from "../ImportError"
import { TRPCUnknownContextCreator } from "./TRPCContextCreator"
import { TRPCAnyRouter } from "./TRPCRouter"
export class TRPCWebSocketServer extends Context.Tag("@thilalib/TRPC/TRPCWebSocketServer")<TRPCWebSocketServer, TRPCWebSocketServerService>() {}
export interface TRPCWebSocketServerService {
wss: ws.Server
handler: ReturnType<typeof applyWSSHandler>
}
const importWS = Effect.tryPromise({
@@ -17,57 +25,42 @@ const importTRPCServerWSAdapter = Effect.tryPromise({
catch: cause => new ImportError({ path: "@trpc/server/adapters/ws", cause }),
})
export const TRPCWebSocketServerLive = (
config: {
readonly host: Config.Config<string>
}
) => Layer.effect(TRPCWebSocketServer, Effect.gen(function*() {
const { WebSocketServer } = yield* importWS
const { applyWSSHandler } = yield* importTRPCServerWSAdapter
export const make = <
Tag,
TagShape extends AnyRouter,
>(
routerTag: Context.TagClass<Tag, "@thilalib/TRCP/TRPCRouter", TagShape>
) => {
class TRPCWebSocketServer extends Context.Tag("@thilalib/TRPC/TRPCWebSocketServer")<TRPCWebSocketServer, {
wss: ws.Server
handler: ReturnType<typeof applyWSSHandler<TagShape>>
}>() {}
const host = yield* config.host
const TRPCWebSocketServerLive = (
config: {
readonly host: Config.Config<string>
}
) => Layer.effect(TRPCWebSocketServer, Effect.gen(function*() {
const { WebSocketServer } = yield* importWS
const { applyWSSHandler } = yield* importTRPCServerWSAdapter
return yield* Effect.acquireRelease(
Effect.gen(function*() {
yield* Effect.logInfo(`WebSocket server started on ${ host }`)
const host = yield* config.host
const wss = new WebSocketServer({
server: yield* ExpressNodeHTTPServer.ExpressNodeHTTPServer,
host,
})
return yield* Effect.acquireRelease(
Effect.gen(function*() {
yield* Effect.logInfo(`WebSocket server started on ${ host }`)
const wss = new WebSocketServer({
server: yield* ExpressNodeHTTPServer.ExpressNodeHTTPServer,
host,
})
return {
return {
wss,
handler: applyWSSHandler({
wss,
handler: applyWSSHandler({
wss,
router: yield* routerTag,
createContext: (yield* TRPCUnknownContextCreator).createWebSocketContext,
}),
}
}),
router: yield* TRPCAnyRouter,
createContext: (yield* TRPCUnknownContextCreator).createWebSocketContext,
}),
}
}),
({ wss, handler }) => Effect.gen(function*() {
yield* Effect.logInfo(`WebSocket server on ${ host } is stopping. Waiting for existing connections to end...`)
({ wss, handler }) => Effect.gen(function*() {
yield* Effect.logInfo(`WebSocket server on ${ host } is stopping. Waiting for existing connections to end...`)
handler.broadcastReconnectNotification()
yield* Effect.async(resume => {
wss.close(() => resume(Effect.logInfo("WebSocket server closed")))
})
}),
)
}))
return { TRPCWebSocketServer, TRPCWebSocketServerLive }
}
handler.broadcastReconnectNotification()
yield* Effect.async(resume => {
wss.close(() => resume(Effect.logInfo("WebSocket server closed")))
})
}),
)
}))