diff options
Diffstat (limited to 'u/trace/itrace.ts')
-rw-r--r-- | u/trace/itrace.ts | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/u/trace/itrace.ts b/u/trace/itrace.ts index b9b750d..620fff0 100644 --- a/u/trace/itrace.ts +++ b/u/trace/itrace.ts @@ -1,4 +1,4 @@ -import type { Mapper, SideEffect } from "@emprespresso/pengueno"; +import type { Mapper, SideEffect, Supplier } from "@emprespresso/pengueno"; // the "thing" every Trace writer must "trace()" type BaseTraceWith = string; @@ -19,15 +19,18 @@ export type ITraceableMapper< ) => U; export interface ITraceable<T, Trace = BaseTraceWith> { - readonly item: T; readonly trace: ITrace<Trace>; - + get: Supplier<T>; move<U>(u: U): ITraceable<U, Trace>; map: <U>( mapper: ITraceableMapper<T, U, Trace>, ) => ITraceable<U, Trace>; bimap: <U>( - mapper: ITraceableMapper<T, ITraceableTuple<U, Trace>, Trace>, + mapper: ITraceableMapper< + T, + ITraceableTuple<U, Array<Trace> | Trace>, + Trace + >, ) => ITraceable<U, Trace>; peek: (peek: ITraceableMapper<T, void, Trace>) => ITraceable<T, Trace>; flatMap: <U>( @@ -40,8 +43,8 @@ export interface ITraceable<T, Trace = BaseTraceWith> { export class TraceableImpl<T, TraceWith> implements ITraceable<T, TraceWith> { protected constructor( - readonly item: T, - readonly trace: ITrace<TraceWith>, + private readonly item: T, + public readonly trace: ITrace<TraceWith>, ) {} public map<U>( @@ -69,7 +72,7 @@ export class TraceableImpl<T, TraceWith> implements ITraceable<T, TraceWith> { >, ): ITraceable<Promise<U>, TraceWith> { return new TraceableImpl( - mapper(this).then(({ item }) => item), + mapper(this).then((t) => t.get()), this.trace, ); } @@ -86,11 +89,19 @@ export class TraceableImpl<T, TraceWith> implements ITraceable<T, TraceWith> { public bimap<U>( mapper: ITraceableMapper< T, - ITraceableTuple<U, TraceWith>, + ITraceableTuple<U, Array<TraceWith> | TraceWith>, TraceWith >, ) { const [item, trace] = mapper(this); - return new TraceableImpl(item, this.trace.addTrace(trace)); + const traces = Array.isArray(trace) ? trace : [trace]; + return new TraceableImpl( + item, + traces.reduce((trace, _trace) => trace.addTrace(_trace), this.trace), + ); + } + + public get() { + return this.item; } } |