summaryrefslogtreecommitdiff
path: root/utils/trace.ts
diff options
context:
space:
mode:
Diffstat (limited to 'utils/trace.ts')
-rw-r--r--utils/trace.ts130
1 files changed, 0 insertions, 130 deletions
diff --git a/utils/trace.ts b/utils/trace.ts
deleted file mode 100644
index 1a5e51d..0000000
--- a/utils/trace.ts
+++ /dev/null
@@ -1,130 +0,0 @@
-export interface Logger {
- log: (...args: unknown[]) => void;
- debug: (...args: unknown[]) => void;
- warn: (...args: unknown[]) => void;
- error: (...args: unknown[]) => void;
-}
-
-type Supplier<T> = () => T;
-type TraceSupplier = Supplier<string>;
-export interface ITraceableLogger<L extends ITraceableLogger<L>>
- extends Logger {
- addTracer: (traceSupplier: TraceSupplier) => L;
-}
-
-export type ITraceableTuple<T> = [T, TraceSupplier];
-export type ITraceableMapper<T, L extends ITraceableLogger<L>, U> = (
- t: ITraceable<T, L>,
-) => U;
-export interface ITraceable<T, L extends ITraceableLogger<L>> {
- item: T;
- logger: L;
-
- map: <U>(mapper: ITraceableMapper<T, L, U>) => ITraceable<U, L>;
- bimap: <U>(
- mapper: ITraceableMapper<T, L, ITraceableTuple<U>>,
- ) => ITraceable<U, L>;
- peek: (peek: ITraceableMapper<T, L, void>) => ITraceable<T, L>;
- flatMap: <U>(
- mapper: ITraceableMapper<T, L, ITraceable<U, L>>,
- ) => ITraceable<U, L>;
- flatMapAsync<U>(
- mapper: ITraceableMapper<T, L, Promise<ITraceable<U, L>>>,
- ): ITraceable<Promise<U>, L>;
- move<Tt>(t: Tt): ITraceable<Tt, L>;
-}
-
-export class TraceableLogger implements ITraceableLogger<TraceableLogger> {
- private readonly logger: Logger = console;
- constructor(
- private readonly traces = [() => `[${new Date().toISOString()}]`],
- ) {
- }
-
- public debug(...args: unknown[]) {
- this.logger.debug("[DEBUG]", ...this.getPrefix(), args);
- }
-
- public log(...args: unknown[]) {
- this.logger.log("[INFO]", ...this.getPrefix(), args);
- }
-
- public warn(...args: unknown[]) {
- this.logger.warn("[WARN]", ...this.getPrefix(), args);
- }
-
- public error(...args: unknown[]) {
- this.logger.error("[ERROR]", ...this.getPrefix(), args);
- }
-
- public addTracer(traceSupplier: TraceSupplier) {
- return new TraceableLogger(this.traces.concat(traceSupplier));
- }
-
- private getPrefix() {
- return this.traces.map((tracer) => tracer());
- }
-}
-
-class TraceableImpl<
- T,
- L extends ITraceableLogger<L>,
-> implements ITraceable<T, L> {
- protected constructor(readonly item: T, readonly logger: L) {}
-
- public map<U>(mapper: ITraceableMapper<T, L, U>) {
- const result = mapper(this);
- return new TraceableImpl(result, this.logger);
- }
-
- public flatMap<U>(
- mapper: ITraceableMapper<T, L, ITraceable<U, L>>,
- ): ITraceable<U, L> {
- return mapper(this);
- }
-
- public flatMapAsync<U>(
- mapper: ITraceableMapper<T, L, Promise<ITraceable<U, L>>>,
- ): ITraceable<Promise<U>, L> {
- return new TraceableImpl(
- mapper(this).then(({ item }) => item),
- this.logger,
- );
- }
-
- public peek(peek: ITraceableMapper<T, L, void>) {
- peek(this);
- return this;
- }
-
- public move<Tt>(t: Tt) {
- return this.map(() => t);
- }
-
- public bimap<U>(mapper: ITraceableMapper<T, L, ITraceableTuple<U>>) {
- const [item, trace] = mapper(this);
- return new TraceableImpl(item, this.logger.addTracer(trace));
- }
-
- static promiseify<T, L extends ITraceableLogger<L>, U>(
- mapper: ITraceableMapper<T, L, U>,
- ): ITraceableMapper<Promise<T>, L, Promise<U>> {
- return (traceablePromise) =>
- traceablePromise.flatMapAsync(async (t) => {
- const item = await t.item;
- return t.map(() => item).map(mapper);
- }).item;
- }
-}
-
-export class Traceable<T> extends TraceableImpl<T, TraceableLogger> {
- static withClassTrace<C extends object, T>(
- c: C,
- ): ITraceableMapper<T, TraceableLogger, ITraceableTuple<T>> {
- return (t) => [t.item, () => c.constructor.name];
- }
-
- static from<T>(t: T) {
- return new Traceable(t, new TraceableLogger());
- }
-}