diff --git a/apps/server/src/wsServer/pushBus.test.ts b/apps/server/src/wsServer/pushBus.test.ts index 172944607b..a466fde8e6 100644 --- a/apps/server/src/wsServer/pushBus.test.ts +++ b/apps/server/src/wsServer/pushBus.test.ts @@ -1,25 +1,22 @@ -import type { WebSocket } from "ws"; import { it } from "@effect/vitest"; import { describe, expect } from "vitest"; import { Effect, Ref } from "effect"; import { WS_CHANNELS } from "@t3tools/contracts"; -import { makeServerPushBus } from "./pushBus"; +import { makeServerPushBus, type ServerPushClient } from "./pushBus"; -class MockWebSocket { - static readonly OPEN = 1; - - readonly OPEN = MockWebSocket.OPEN; - readyState = MockWebSocket.OPEN; +class MockPushClient implements ServerPushClient { readonly sent: string[] = []; private readonly waiters = new Set<() => void>(); - send(message: string) { - this.sent.push(message); - for (const waiter of this.waiters) { - waiter(); - } - } + readonly send: ServerPushClient["send"] = (message: string) => + Effect.sync(() => { + this.sent.push(message); + for (const waiter of this.waiters) { + waiter(); + } + return true; + }); waitForSentCount(count: number): Promise { if (this.sent.length >= count) { @@ -44,8 +41,8 @@ describe("makeServerPushBus", () => { it.live("waits for the welcome push before a new client joins broadcast delivery", () => Effect.scoped( Effect.gen(function* () { - const client = new MockWebSocket(); - const clients = yield* Ref.make(new Set()); + const client = new MockPushClient(); + const clients = yield* Ref.make(new Set()); const pushBus = yield* makeServerPushBus({ clients, logOutgoingPush: () => {}, @@ -57,7 +54,7 @@ describe("makeServerPushBus", () => { }); const delivered = yield* pushBus.publishClient( - client as unknown as WebSocket, + client, WS_CHANNELS.serverWelcome, { cwd: "/tmp/project", @@ -66,7 +63,7 @@ describe("makeServerPushBus", () => { ); expect(delivered).toBe(true); - yield* Ref.update(clients, (current) => current.add(client as unknown as WebSocket)); + yield* Ref.update(clients, (current) => current.add(client)); yield* pushBus.publishAll(WS_CHANNELS.serverConfigUpdated, { issues: [], diff --git a/apps/server/src/wsServer/pushBus.ts b/apps/server/src/wsServer/pushBus.ts index c2cd302f23..ba9bc9ad09 100644 --- a/apps/server/src/wsServer/pushBus.ts +++ b/apps/server/src/wsServer/pushBus.ts @@ -6,11 +6,14 @@ import { } from "@t3tools/contracts"; import { Deferred, Effect, Queue, Ref, Schema } from "effect"; import type { Scope } from "effect"; -import type { WebSocket } from "ws"; + +export interface ServerPushClient { + readonly send: (message: string) => Effect.Effect; +} type PushTarget = | { readonly kind: "all" } - | { readonly kind: "client"; readonly client: WebSocket }; + | { readonly kind: "client"; readonly client: ServerPushClient }; interface PushJob { readonly channel: C; @@ -25,14 +28,14 @@ export interface ServerPushBus { data: WsPushData, ) => Effect.Effect; readonly publishClient: ( - client: WebSocket, + client: ServerPushClient, channel: C, data: WsPushData, ) => Effect.Effect; } export const makeServerPushBus = (input: { - readonly clients: Ref.Ref>; + readonly clients: Ref.Ref>; readonly logOutgoingPush: (push: WsPushEnvelopeBase, recipients: number) => void; }): Effect.Effect => Effect.gen(function* () { @@ -57,19 +60,17 @@ export const makeServerPushBus = (input: { job.target.kind === "all" ? yield* Ref.get(input.clients) : new Set([job.target.client]); return yield* encodePush(push).pipe( - Effect.map((message) => { - let recipientCount = 0; - for (const client of recipients) { - if (client.readyState !== client.OPEN) { - continue; - } - client.send(message); - recipientCount += 1; - } - - input.logOutgoingPush(push, recipientCount); - return recipientCount > 0; - }), + Effect.flatMap((message) => + Effect.forEach(recipients, (client) => client.send(message), { + concurrency: "unbounded", + }).pipe( + Effect.map((deliveries) => { + const recipientCount = deliveries.filter(Boolean).length; + input.logOutgoingPush(push, recipientCount); + return recipientCount > 0; + }), + ), + ), ); });