diff options
author | Elizabeth Hunt <me@liz.coffee> | 2025-06-20 14:53:38 -0700 |
---|---|---|
committer | Elizabeth Hunt <me@liz.coffee> | 2025-06-20 14:53:38 -0700 |
commit | d4791f3d357634daf506fb8f91cc5332a794c421 (patch) | |
tree | 1bb01d2d4d8fa74d83bb6f99f2c8aa4146ca2d11 /u/trace/itrace.ts | |
parent | d7e8d31c94cd713a2f4cf799e20e993acc69e361 (diff) | |
download | ci-d4791f3d357634daf506fb8f91cc5332a794c421.tar.gz ci-d4791f3d357634daf506fb8f91cc5332a794c421.zip |
Move to nodejs
Diffstat (limited to 'u/trace/itrace.ts')
-rw-r--r-- | u/trace/itrace.ts | 120 |
1 files changed, 51 insertions, 69 deletions
diff --git a/u/trace/itrace.ts b/u/trace/itrace.ts index fcfbe32..8cf123a 100644 --- a/u/trace/itrace.ts +++ b/u/trace/itrace.ts @@ -1,90 +1,72 @@ -import type { Mapper, SideEffect, Supplier } from "@emprespresso/pengueno"; +import type { Mapper, SideEffect, Supplier } from '@emprespresso/pengueno'; // 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>>; + addTrace: Mapper<ITraceWith<TraceWith>, ITrace<TraceWith>>; + trace: SideEffect<ITraceWith<TraceWith>>; } export type ITraceableTuple<T, TraceWith> = [T, BaseTraceWith | TraceWith]; -export type ITraceableMapper<T, _T, TraceWith, W = ITraceable<T, TraceWith>> = ( - w: W, -) => _T; +export type ITraceableMapper<T, _T, TraceWith, W = ITraceable<T, TraceWith>> = (w: W) => _T; export interface ITraceable<T, Trace = BaseTraceWith> { - readonly trace: ITrace<Trace>; - get: Supplier<T>; - move: <_T>(t: _T) => ITraceable<_T, Trace>; - map: <_T>(mapper: ITraceableMapper<T, _T, Trace>) => ITraceable<_T, Trace>; - bimap: <_T>( - mapper: ITraceableMapper< - T, - ITraceableTuple<_T, Array<Trace> | Trace>, - Trace - >, - ) => ITraceable<_T, Trace>; - peek: (peek: ITraceableMapper<T, void, Trace>) => ITraceable<T, Trace>; - flatMap: <_T>( - mapper: ITraceableMapper<T, ITraceable<_T, Trace>, Trace>, - ) => ITraceable<_T, Trace>; - flatMapAsync<_T>( - mapper: ITraceableMapper<T, Promise<ITraceable<_T, Trace>>, Trace>, - ): ITraceable<Promise<_T>, Trace>; + readonly trace: ITrace<Trace>; + get: Supplier<T>; + move: <_T>(t: _T) => ITraceable<_T, Trace>; + map: <_T>(mapper: ITraceableMapper<T, _T, Trace>) => ITraceable<_T, Trace>; + bimap: <_T>(mapper: ITraceableMapper<T, ITraceableTuple<_T, Array<Trace> | Trace>, Trace>) => ITraceable<_T, Trace>; + peek: (peek: ITraceableMapper<T, void, Trace>) => ITraceable<T, Trace>; + flatMap: <_T>(mapper: ITraceableMapper<T, ITraceable<_T, Trace>, Trace>) => ITraceable<_T, Trace>; + flatMapAsync<_T>( + mapper: ITraceableMapper<T, Promise<ITraceable<_T, Trace>>, Trace>, + ): ITraceable<Promise<_T>, Trace>; } export class TraceableImpl<T, TraceWith> implements ITraceable<T, TraceWith> { - protected constructor( - private readonly item: T, - public readonly trace: ITrace<TraceWith>, - ) {} + protected constructor( + private readonly item: T, + public readonly trace: ITrace<TraceWith>, + ) {} - public map<_T>(mapper: ITraceableMapper<T, _T, TraceWith>) { - const result = mapper(this); - return new TraceableImpl(result, this.trace); - } + public map<_T>(mapper: ITraceableMapper<T, _T, TraceWith>) { + const result = mapper(this); + return new TraceableImpl(result, this.trace); + } - public flatMap<_T>( - mapper: ITraceableMapper<T, ITraceable<_T, TraceWith>, TraceWith>, - ): ITraceable<_T, TraceWith> { - return mapper(this); - } + public flatMap<_T>(mapper: ITraceableMapper<T, ITraceable<_T, TraceWith>, TraceWith>): ITraceable<_T, TraceWith> { + return mapper(this); + } - public flatMapAsync<_T>( - mapper: ITraceableMapper<T, Promise<ITraceable<_T, TraceWith>>, TraceWith>, - ): ITraceable<Promise<_T>, TraceWith> { - return new TraceableImpl( - mapper(this).then((t) => t.get()), - this.trace, - ); - } + public flatMapAsync<_T>( + mapper: ITraceableMapper<T, Promise<ITraceable<_T, TraceWith>>, TraceWith>, + ): ITraceable<Promise<_T>, TraceWith> { + return new TraceableImpl( + mapper(this).then((t) => t.get()), + this.trace, + ); + } - public peek(peek: ITraceableMapper<T, void, TraceWith>) { - peek(this); - return this; - } + public peek(peek: ITraceableMapper<T, void, TraceWith>) { + peek(this); + return this; + } - public move<_T>(t: _T): ITraceable<_T, TraceWith> { - return this.map(() => t); - } + public move<_T>(t: _T): ITraceable<_T, TraceWith> { + return this.map(() => t); + } - public bimap<_T>( - mapper: ITraceableMapper< - T, - ITraceableTuple<_T, Array<TraceWith> | TraceWith>, - TraceWith - >, - ) { - const [item, trace] = mapper(this); - const traces = Array.isArray(trace) ? trace : [trace]; - return new TraceableImpl( - item, - traces.reduce((trace, _trace) => trace.addTrace(_trace), this.trace), - ); - } + public bimap<_T>(mapper: ITraceableMapper<T, ITraceableTuple<_T, Array<TraceWith> | TraceWith>, TraceWith>) { + const [item, trace] = mapper(this); + 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; - } + public get() { + return this.item; + } } |