import { IObservable, ICancellation, IDestroyable } from "@implab/core-amd/interfaces"; import { Cancellation } from "@implab/core-amd/Cancellation"; import { TraceEvent, LogLevel, WarnLevel, DebugLevel, TraceSource } from "@implab/core-amd/log/TraceSource"; import { argumentNotNull, destroy } from "@implab/core-amd/safe"; import { TapWriter } from "./Tap"; export class TapeWriter implements IDestroyable { private readonly _t: TapWriter; private readonly _subscriptions = new Array(); private _destroyed = false; constructor(t: TapWriter) { argumentNotNull(t, "test"); this._t = t; } writeEvents(source: IObservable, ct: ICancellation = Cancellation.none) { if (!this._destroyed) { const subscription = source.on(this.writeEvent.bind(this)); if (ct.isSupported()) { ct.register(subscription.destroy.bind(subscription)); } this._subscriptions.push(subscription); } } writeEvent(next: TraceEvent) { if (next.level >= DebugLevel) { this._t.comment(`DEBUG ${next.source.id} ${next}`); } else if (next.level >= LogLevel) { this._t.comment(`LOG ${next.source.id} ${next}`); } else if (next.level >= WarnLevel) { this._t.comment(`WARN ${next.source.id} ${next}`); } else { this._t.comment(`ERROR ${next.source.id} ${next}`); } } destroy() { if (this._destroyed) return; this._destroyed = true; this._subscriptions.forEach(destroy); } } type TestCallback = (ok: (msg: string) => void, fail: (msg: string) => void, trace: TraceSource) => void; type AsyncTestCallback = (trace: TraceSource) => PromiseLike; export function test(name: string, cb: TestCallback ): void; export function test(name: string, cb: AsyncTestCallback): void; export function test(name: string, cb: TestCallback | AsyncTestCallback) { } export function run() { }