diff --git a/history.md b/history.md --- a/history.md +++ b/history.md @@ -30,4 +30,4 @@ First release, intorduces the following - `di` - dependency injection container - `log` - log4 style logging system - `text` - simple and fast text templating and formatting -- `Uuid` - uuid generation traits \ No newline at end of file +- `Uuid` - uuid generation traits diff --git a/src/main/ts/safe.ts b/src/main/ts/safe.ts --- a/src/main/ts/safe.ts +++ b/src/main/ts/safe.ts @@ -248,20 +248,18 @@ export function delegate { - if (ct.isRequested()) { - ct.register(reject); - } else { - const h = ct.register(e => { - clearTimeout(id); - reject(e); - // we don't nedd to unregister h, since ct is already disposed - }); - const id = setTimeout(() => { - h.destroy(); - resolve(); - }, timeMs); - } + const h = ct.register(e => { + clearTimeout(id); + reject(e); + // we don't nedd to unregister h, since ct is already disposed + }); + const id = setTimeout(() => { + h.destroy(); + resolve(); + }, timeMs); + }); } diff --git a/src/test/ts/CancellationTests.ts b/src/test/ts/CancellationTests.ts --- a/src/test/ts/CancellationTests.ts +++ b/src/test/ts/CancellationTests.ts @@ -1,7 +1,6 @@ import * as tape from "tape"; import { Cancellation } from "@implab/core/Cancellation"; -import { ICancellation } from "@implab/core/interfaces"; -import { delay } from "./TestTraits"; +import { delay } from "@implab/core/safe"; tape("standalone cancellation", async t => { diff --git a/src/test/ts/ObservableTests.ts b/src/test/ts/ObservableTests.ts --- a/src/test/ts/ObservableTests.ts +++ b/src/test/ts/ObservableTests.ts @@ -1,8 +1,8 @@ import { TraceSource, DebugLevel } from "@implab/core/log/TraceSource"; import * as tape from "tape"; -import { TapeWriter, delay } from "./TestTraits"; import { Observable } from "@implab/core/Observable"; import { IObservable } from "@implab/core/interfaces"; +import { delay } from "@implab/core/safe"; const trace = TraceSource.get("ObservableTests"); diff --git a/src/test/ts/SafeTests.ts b/src/test/ts/SafeTests.ts --- a/src/test/ts/SafeTests.ts +++ b/src/test/ts/SafeTests.ts @@ -1,7 +1,6 @@ import tape = require("tape"); -import { delay } from "./TestTraits"; import { Cancellation } from "@implab/core/Cancellation"; -import { first, isPromise } from "@implab/core/safe"; +import { first, isPromise, firstWhere, delay, nowait } from "@implab/core/safe"; tape("await delay test", async t => { // schedule delay @@ -33,15 +32,10 @@ tape("await delay test", async t => { t.pass("the delay is cancelled"); } - let died = false; - - // try schedule delay after the cancellation is requested - res = delay(0, ct).then(x => true, () => died = true); - - t.false(died, "The delay should be scheduled even if the cancellation is requested"); - - await res; - t.true(died, "the delay should fail when cancelled"); + t.throws(() => { + // try schedule delay after the cancellation is requested + nowait(delay(0, ct)); + }, "Should throw if cancelled before start"); t.end(); }); @@ -52,20 +46,39 @@ tape("sequemce test", async t => { // synchronous tests t.equals(first(sequence), "a", "Should return the first element"); + t.equals(firstWhere(sequence, x => x === "b"), "b", "Should get the second element"); let v: string; let e: Error; first(sequence, x => v = x); t.equal(v, "a", "The callback should be called for the first element"); + firstWhere(sequence, x => x === "b", x => v = x); + t.equal(v, "b", "The callback should be called for the second element"); t.throws(() => { first(empty); }, "Should throw when the sequence is empty"); t.throws(() => { + firstWhere(empty, x => x === "b"); + }, "Should throw when the sequence is empty"); + + t.throws(() => { first(empty, x => v = x); }, "Should throw when the sequence is empty"); + t.throws(() => { + firstWhere(empty, x => x === "b", x => v = x); + }, "Should throw when the sequence is empty"); + + t.throws(() => { + firstWhere(sequence, x => x === "z"); + }, "Should throw when the element isn't found"); + + t.throws(() => { + firstWhere(sequence, x => x === "z", x => v = x); + }, "Should throw when the element isn't found"); + first(empty, null, x => e = x); t.true(e, "The errorback should be called for the empty sequence"); diff --git a/src/test/ts/TestTraits.ts b/src/test/ts/TestTraits.ts --- a/src/test/ts/TestTraits.ts +++ b/src/test/ts/TestTraits.ts @@ -39,29 +39,6 @@ export class TapeWriter implements IDest } } -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 { - const ht = setTimeout(() => { - resolve(); - }, timeout); - - un = ct.register(e => { - clearTimeout(ht); - reject(e); - }); - } - }); - } finally { - destroy(un); - } -} - export function test(name: string, cb: (t: tape.Test) => any) { tape(name, async t => { const writer = new TapeWriter(t);