diff options
Diffstat (limited to 'u/trace/itrace.ts')
-rw-r--r-- | u/trace/itrace.ts | 68 |
1 files changed, 46 insertions, 22 deletions
diff --git a/u/trace/itrace.ts b/u/trace/itrace.ts index b483067..b9b750d 100644 --- a/u/trace/itrace.ts +++ b/u/trace/itrace.ts @@ -1,71 +1,95 @@ -import { Mapper, SideEffect } from "../fn/mod.ts"; +import type { Mapper, SideEffect } from "@emprespresso/pengueno"; -export interface ITrace<TracingW> { - addTrace: Mapper<TracingW, ITrace<TracingW>>; - trace: SideEffect<TracingW>; +// the "thing" every Trace writer must "trace()" +type BaseTraceWith = string; +export type ITraceWith<T> = BaseTraceWith | T; +export interface ITrace<TraceWith> { + addTrace: Mapper<ITraceWith<TraceWith>, ITrace<TraceWith>>; + trace: SideEffect<ITraceWith<TraceWith>>; } -export type ITraceableTuple<T, Trace> = [T, Trace]; -export type ITraceableMapper<T, Trace, U, W = ITraceable<T, Trace>> = ( +export type ITraceableTuple<T, TraceWith> = [T, BaseTraceWith | TraceWith]; +export type ITraceableMapper< + T, + U, + TraceWith, + W = ITraceable<T, TraceWith>, +> = ( w: W, ) => U; -export interface ITraceable<T, Trace> { +export interface ITraceable<T, Trace = BaseTraceWith> { readonly item: T; readonly trace: ITrace<Trace>; move<U>(u: U): ITraceable<U, Trace>; map: <U>( - mapper: ITraceableMapper<T, Trace, U>, + mapper: ITraceableMapper<T, U, Trace>, ) => ITraceable<U, Trace>; bimap: <U>( - mapper: ITraceableMapper<T, Trace, ITraceableTuple<U, Trace>>, + mapper: ITraceableMapper<T, ITraceableTuple<U, Trace>, Trace>, ) => ITraceable<U, Trace>; - peek: (peek: ITraceableMapper<T, Trace, void>) => ITraceable<T, Trace>; + peek: (peek: ITraceableMapper<T, void, Trace>) => ITraceable<T, Trace>; flatMap: <U>( - mapper: ITraceableMapper<T, Trace, ITraceable<U, Trace>>, + mapper: ITraceableMapper<T, ITraceable<U, Trace>, Trace>, ) => ITraceable<U, Trace>; flatMapAsync<U>( - mapper: ITraceableMapper<T, Trace, Promise<ITraceable<U, Trace>>>, + mapper: ITraceableMapper<T, Promise<ITraceable<U, Trace>>, Trace>, ): ITraceable<Promise<U>, Trace>; } -export class TraceableImpl<T, L> implements ITraceable<T, L> { +export class TraceableImpl<T, TraceWith> implements ITraceable<T, TraceWith> { protected constructor( readonly item: T, - readonly trace: ITrace<L>, + readonly trace: ITrace<TraceWith>, ) {} - public map<U>(mapper: ITraceableMapper<T, L, U>) { + public map<U>( + mapper: ITraceableMapper<T, U, TraceWith>, + ) { const result = mapper(this); return new TraceableImpl(result, this.trace); } public flatMap<U>( - mapper: ITraceableMapper<T, L, ITraceable<U, L>>, - ): ITraceable<U, L> { + mapper: ITraceableMapper< + T, + ITraceable<U, TraceWith>, + TraceWith + >, + ): ITraceable<U, TraceWith> { return mapper(this); } public flatMapAsync<U>( - mapper: ITraceableMapper<T, L, Promise<ITraceable<U, L>>>, - ): ITraceable<Promise<U>, L> { + mapper: ITraceableMapper< + T, + Promise<ITraceable<U, TraceWith>>, + TraceWith + >, + ): ITraceable<Promise<U>, TraceWith> { return new TraceableImpl( mapper(this).then(({ item }) => item), this.trace, ); } - public peek(peek: ITraceableMapper<T, L, void>) { + public peek(peek: ITraceableMapper<T, void, TraceWith>) { peek(this); return this; } - public move<Tt>(t: Tt): ITraceable<Tt, L> { + public move<Tt>(t: Tt): ITraceable<Tt, TraceWith> { return this.map(() => t); } - public bimap<U>(mapper: ITraceableMapper<T, L, ITraceableTuple<U, L>>) { + public bimap<U>( + mapper: ITraceableMapper< + T, + ITraceableTuple<U, TraceWith>, + TraceWith + >, + ) { const [item, trace] = mapper(this); return new TraceableImpl(item, this.trace.addTrace(trace)); } |