From e49fda41176d025a671802be76c219d66167276f Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Mon, 12 May 2025 23:05:27 -0700 Subject: snapshot --- utils/trace.ts | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) (limited to 'utils/trace.ts') diff --git a/utils/trace.ts b/utils/trace.ts index eb4ac2f..373f37e 100644 --- a/utils/trace.ts +++ b/utils/trace.ts @@ -25,12 +25,15 @@ export interface ITraceable> { mapper: ITraceableMapper>, ) => ITraceable; peek: (peek: ITraceableMapper) => ITraceable; - flatMap: (mapper: ITraceableMapper>) => ITraceable; - flatMapAsync(mapper: ITraceableMapper>>): ITraceable, L>; + flatMap: ( + mapper: ITraceableMapper>, + ) => ITraceable; + flatMapAsync( + mapper: ITraceableMapper>>, + ): ITraceable, L>; } -export class TraceableLogger - implements ITraceableLogger { +export class TraceableLogger implements ITraceableLogger { private readonly logger: Logger = console; constructor( private readonly traces = [() => `[${new Date().toISOString()}]`], @@ -62,23 +65,30 @@ export class TraceableLogger } } -export class TraceableImpl< +class TraceableImpl< T, L extends ITraceableLogger, > implements ITraceable { - private constructor(readonly item: T, readonly logger: L) {} + protected constructor(readonly item: T, readonly logger: L) {} public map(mapper: ITraceableMapper) { const result = mapper(this); return new TraceableImpl(result, this.logger); } - public flatMap(mapper: ITraceableMapper>): ITraceable { + public flatMap( + mapper: ITraceableMapper>, + ): ITraceable { return mapper(this); } - public flatMapAsync(mapper: ITraceableMapper>>): ITraceable, L> { - return new TraceableImpl(mapper(this).then(({ item }) => item), this.logger); + public flatMapAsync( + mapper: ITraceableMapper>>, + ): ITraceable, L> { + return new TraceableImpl( + mapper(this).then(({ item }) => item), + this.logger, + ); } public peek(peek: ITraceableMapper) { @@ -95,20 +105,21 @@ export class TraceableImpl< mapper: ITraceableMapper, ): ITraceableMapper, L, Promise> { return (traceablePromise) => - traceablePromise.flatMapAsync(async (t) => { - const item = await t.item; - return t.map(() => item).map(mapper); - }).item; + traceablePromise.flatMapAsync(async (t) => { + const item = await t.item; + return t.map(() => item).map(mapper); + }).item; } +} - static withClassTrace>(c: C): ITraceableMapper> { +export class Traceable extends TraceableImpl { + static withClassTrace( + c: C, + ): ITraceableMapper> { return (t) => [t.item, () => c.constructor.name]; } static from(t: T) { - return new TraceableImpl(t, new TraceableLogger()); + return new Traceable(t, new TraceableLogger()); } } - -export interface Traceable = TraceableLogger> extends ITraceable { -} -- cgit v1.2.3-70-g09d2