From 590053ca7f78cbe62d9138a160f0e2572d4017ac Mon Sep 17 00:00:00 2001 From: hasundue Date: Thu, 21 Mar 2024 15:00:08 +0900 Subject: [PATCH] refactor(lib/streams): rename classes --- lib/streams.ts | 18 ++++++++---------- lib/streams_test.ts | 18 +++++++++--------- std/pools.ts | 4 ++-- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/lib/streams.ts b/lib/streams.ts index 9e71cfd..dd68263 100644 --- a/lib/streams.ts +++ b/lib/streams.ts @@ -1,13 +1,13 @@ /** * TransformStream which filters out duplicate values from a stream. */ -export class Distinctor +export class DistinctStream extends TransformStream { #seen: Set; - constructor(protected readonly fn: (value: R) => T) { + constructor(protected readonly selector: (value: R) => T) { super({ transform: (value, controller) => { - const key = fn(value); + const key = selector(value); if (!this.#seen.has(key)) { this.#seen.add(key); @@ -19,15 +19,13 @@ export class Distinctor } } -export type LogLevel = "error" | "warn" | "info" | "debug"; - -export interface ConsoleLoggerOptions { - level?: LogLevel; +export interface ConsoleLogStreamOptions { + level?: "error" | "warn" | "info" | "debug"; } -export class ConsoleLogger extends WritableStream { - readonly level: LogLevel; - constructor(options?: ConsoleLoggerOptions) { +export class ConsoleLogStream extends WritableStream { + readonly level: ConsoleLogStreamOptions["level"]; + constructor(options?: ConsoleLogStreamOptions) { const level = options?.level ?? "info"; super({ write(chunk) { diff --git a/lib/streams_test.ts b/lib/streams_test.ts index 5f9232f..70aac81 100644 --- a/lib/streams_test.ts +++ b/lib/streams_test.ts @@ -1,9 +1,9 @@ import { assert, assertArrayIncludes, assertEquals } from "@std/assert"; import { beforeEach, describe, it } from "@std/testing/bdd"; -import { ConsoleLogger, Distinctor } from "@lophus/lib/streams"; +import { ConsoleLogStream, DistinctStream } from "@lophus/lib/streams"; -describe("Distinctor", () => { - it("filters out duplicate values from a stream", async () => { +describe("DistinctStream", () => { + it("should filter out duplicate values from a stream", async () => { const stream = new ReadableStream({ start(controller) { controller.enqueue(1); @@ -14,14 +14,14 @@ describe("Distinctor", () => { }, }); const values = await Array.fromAsync( - stream.pipeThrough(new Distinctor((v) => v)), + stream.pipeThrough(new DistinctStream((v) => v)), ); assertEquals(values.length, 3); assertArrayIncludes(values, [1, 2, 3]); }); }); -describe("ConsoleLogger", () => { +describe("ConsoleLogStream", () => { let output = ""; beforeEach(() => { @@ -35,13 +35,13 @@ describe("ConsoleLogger", () => { }); it("should be a writable stream", () => { - const logger = new ConsoleLogger(); + const logger = new ConsoleLogStream(); assert(logger instanceof WritableStream); }); it("should log to console", async () => { - const logger = new ConsoleLogger(); - await logger.getWriter().write("ConsoleLogger"); - assertEquals(output, "ConsoleLogger"); + const logger = new ConsoleLogStream(); + await logger.getWriter().write("ConsoleLogStream"); + assertEquals(output, "ConsoleLogStream"); }); }); diff --git a/std/pools.ts b/std/pools.ts index 48226b3..cd267cc 100644 --- a/std/pools.ts +++ b/std/pools.ts @@ -1,5 +1,5 @@ import { mergeReadableStreams as merge } from "@std/streams"; -import { Distinctor } from "@lophus/lib/streams"; +import { DistinctStream } from "@lophus/lib/streams"; import type { ClientToRelayMessage, EventKind, @@ -47,7 +47,7 @@ export class RelayPool implements RelayLike { opts: Partial = {}, ): ReadableStream> { const subs = this.#relays_read.map((r) => r.subscribe(filter, opts)); - return merge(...subs).pipeThrough(new Distinctor((m) => m.id)); + return merge(...subs).pipeThrough(new DistinctStream((m) => m.id)); } async publish(