refactor ws push bus client abstraction
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
7b11cdd56f
commit
082876c397
2 files changed
+32
-34
No files matched your search
@@ -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<void> {
|
||||
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<WebSocket>());
|
||||
const client = new MockPushClient();
|
||||
const clients = yield* Ref.make(new Set<ServerPushClient>());
|
||||
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: [],
|
||||
|
||||
@@ -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<boolean>;
|
||||
}
|
||||
|
||||
type PushTarget =
|
||||
| { readonly kind: "all" }
|
||||
| { readonly kind: "client"; readonly client: WebSocket };
|
||||
| { readonly kind: "client"; readonly client: ServerPushClient };
|
||||
|
||||
interface PushJob<C extends WsPushChannel = WsPushChannel> {
|
||||
readonly channel: C;
|
||||
@@ -25,14 +28,14 @@ export interface ServerPushBus {
|
||||
data: WsPushData<C>,
|
||||
) => Effect.Effect<void>;
|
||||
readonly publishClient: <C extends WsPushChannel>(
|
||||
client: WebSocket,
|
||||
client: ServerPushClient,
|
||||
channel: C,
|
||||
data: WsPushData<C>,
|
||||
) => Effect.Effect<boolean>;
|
||||
}
|
||||
|
||||
export const makeServerPushBus = (input: {
|
||||
readonly clients: Ref.Ref<Set<WebSocket>>;
|
||||
readonly clients: Ref.Ref<Set<ServerPushClient>>;
|
||||
readonly logOutgoingPush: (push: WsPushEnvelopeBase, recipients: number) => void;
|
||||
}): Effect.Effect<ServerPushBus, never, Scope.Scope> =>
|
||||
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;
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user