TestTraits.ts
62 lines
| 1.8 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r16 | import { IObservable, ICancellation, IDestroyable } from "../../build/dist/interfaces"; | |
| import * as TraceEvent from '../../build/dist/log/TraceEvent'; | |||
| import { Cancellation } from "../../build/dist/Cancellation"; | |||
| import * as TraceSource from "../../build/dist/log/TraceSource"; | |||
|
|
r15 | import * as tape from 'tape'; | |
|
|
r16 | import { argumentNotNull } from "../../build/dist/safe"; | |
|
|
r15 | ||
| export class TapeWriter implements IDestroyable { | |||
| readonly _tape: tape.Test | |||
| _subscriptions = new Array<IDestroyable>(); | |||
| constructor(tape: tape.Test) { | |||
| argumentNotNull(tape, "tape"); | |||
| this._tape = tape; | |||
| } | |||
| writeEvents(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) { | |||
| let 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 >= TraceSource.LogLevel) { | |||
| this._tape.comment("LOG " + next.arg); | |||
|
|
r18 | } else if (next.level >= TraceSource.WarnLevel) { | |
|
|
r15 | this._tape.comment("WARN " + next.arg); | |
| } else { | |||
| this._tape.comment("ERROR " + next.arg); | |||
| } | |||
| } | |||
| destroy() { | |||
| this._subscriptions.forEach(x => x.destroy()); | |||
| } | |||
|
|
r18 | } | |
| export async function delay(timeout: number, ct: ICancellation = Cancellation.none) { | |||
| let un: IDestroyable; | |||
| try { | |||
| await new Promise((resolve, reject) => { | |||
| if (ct.isRequested()) { | |||
| un = ct.register(reject); | |||
| } else { | |||
| let ht = setTimeout(() => { | |||
| resolve(); | |||
| }, timeout); | |||
| un = ct.register(e => { | |||
| clearTimeout(ht); | |||
| reject(e); | |||
| }); | |||
| } | |||
| }); | |||
| } finally { | |||
| if(un) | |||
| un.destroy(); | |||
| }; | |||
|
|
r15 | } |
