| @@ -30,4 +30,4 First release, intorduces the following | |||
|
|
30 | 30 | - `di` - dependency injection container |
|
|
31 | 31 | - `log` - log4 style logging system |
|
|
32 | 32 | - `text` - simple and fast text templating and formatting |
|
|
33 | - `Uuid` - uuid generation traits No newline at end of file | |
|
|
33 | - `Uuid` - uuid generation traits | |
| @@ -248,20 +248,18 export function delegate<T, K extends ke | |||
|
|
248 | 248 | } |
|
|
249 | 249 | |
|
|
250 | 250 | export function delay(timeMs: number, ct = Cancellation.none) { |
|
|
251 | ct.throwIfRequested(); | |
|
|
251 | 252 | return new Promise((resolve, reject) => { |
|
|
252 | if (ct.isRequested()) { | |
|
|
253 | ct.register(reject); | |
|
|
254 | } else { | |
|
|
255 | const h = ct.register(e => { | |
|
|
256 | clearTimeout(id); | |
|
|
257 | reject(e); | |
|
|
258 | // we don't nedd to unregister h, since ct is already disposed | |
|
|
259 |
|
|
|
|
260 | const id = setTimeout(() => { | |
|
|
261 | h.destroy(); | |
|
|
262 | resolve(); | |
|
|
263 | }, timeMs); | |
|
|
264 | } | |
|
|
253 | const h = ct.register(e => { | |
|
|
254 | clearTimeout(id); | |
|
|
255 | reject(e); | |
|
|
256 | // we don't nedd to unregister h, since ct is already disposed | |
|
|
257 | }); | |
|
|
258 | const id = setTimeout(() => { | |
|
|
259 | h.destroy(); | |
|
|
260 | resolve(); | |
|
|
261 | }, timeMs); | |
|
|
262 | ||
|
|
265 | 263 | }); |
|
|
266 | 264 | } |
|
|
267 | 265 | |
| @@ -1,7 +1,6 | |||
|
|
1 | 1 | import * as tape from "tape"; |
|
|
2 | 2 | import { Cancellation } from "@implab/core/Cancellation"; |
|
|
3 |
import { |
|
|
|
4 | import { delay } from "./TestTraits"; | |
|
|
3 | import { delay } from "@implab/core/safe"; | |
|
|
5 | 4 | |
|
|
6 | 5 | tape("standalone cancellation", async t => { |
|
|
7 | 6 | |
| @@ -1,8 +1,8 | |||
|
|
1 | 1 | import { TraceSource, DebugLevel } from "@implab/core/log/TraceSource"; |
|
|
2 | 2 | import * as tape from "tape"; |
|
|
3 | import { TapeWriter, delay } from "./TestTraits"; | |
|
|
4 | 3 | import { Observable } from "@implab/core/Observable"; |
|
|
5 | 4 | import { IObservable } from "@implab/core/interfaces"; |
|
|
5 | import { delay } from "@implab/core/safe"; | |
|
|
6 | 6 | |
|
|
7 | 7 | const trace = TraceSource.get("ObservableTests"); |
|
|
8 | 8 | |
| @@ -1,7 +1,6 | |||
|
|
1 | 1 | import tape = require("tape"); |
|
|
2 | import { delay } from "./TestTraits"; | |
|
|
3 | 2 | import { Cancellation } from "@implab/core/Cancellation"; |
|
|
4 | import { first, isPromise } from "@implab/core/safe"; | |
|
|
3 | import { first, isPromise, firstWhere, delay, nowait } from "@implab/core/safe"; | |
|
|
5 | 4 | |
|
|
6 | 5 | tape("await delay test", async t => { |
|
|
7 | 6 | // schedule delay |
| @@ -33,15 +32,10 tape("await delay test", async t => { | |||
|
|
33 | 32 | t.pass("the delay is cancelled"); |
|
|
34 | 33 | } |
|
|
35 | 34 | |
|
|
36 | let died = false; | |
|
|
37 | ||
|
|
38 | // try schedule delay after the cancellation is requested | |
|
|
39 | res = delay(0, ct).then(x => true, () => died = true); | |
|
|
40 | ||
|
|
41 | t.false(died, "The delay should be scheduled even if the cancellation is requested"); | |
|
|
42 | ||
|
|
43 | await res; | |
|
|
44 | t.true(died, "the delay should fail when cancelled"); | |
|
|
35 | t.throws(() => { | |
|
|
36 | // try schedule delay after the cancellation is requested | |
|
|
37 | nowait(delay(0, ct)); | |
|
|
38 | }, "Should throw if cancelled before start"); | |
|
|
45 | 39 | |
|
|
46 | 40 | t.end(); |
|
|
47 | 41 | }); |
| @@ -52,20 +46,39 tape("sequemce test", async t => { | |||
|
|
52 | 46 | |
|
|
53 | 47 | // synchronous tests |
|
|
54 | 48 | t.equals(first(sequence), "a", "Should return the first element"); |
|
|
49 | t.equals(firstWhere(sequence, x => x === "b"), "b", "Should get the second element"); | |
|
|
55 | 50 | |
|
|
56 | 51 | let v: string; |
|
|
57 | 52 | let e: Error; |
|
|
58 | 53 | first(sequence, x => v = x); |
|
|
59 | 54 | t.equal(v, "a", "The callback should be called for the first element"); |
|
|
55 | firstWhere(sequence, x => x === "b", x => v = x); | |
|
|
56 | t.equal(v, "b", "The callback should be called for the second element"); | |
|
|
60 | 57 | |
|
|
61 | 58 | t.throws(() => { |
|
|
62 | 59 | first(empty); |
|
|
63 | 60 | }, "Should throw when the sequence is empty"); |
|
|
64 | 61 | |
|
|
65 | 62 | t.throws(() => { |
|
|
63 | firstWhere(empty, x => x === "b"); | |
|
|
64 | }, "Should throw when the sequence is empty"); | |
|
|
65 | ||
|
|
66 | t.throws(() => { | |
|
|
66 | 67 | first(empty, x => v = x); |
|
|
67 | 68 | }, "Should throw when the sequence is empty"); |
|
|
68 | 69 | |
|
|
70 | t.throws(() => { | |
|
|
71 | firstWhere(empty, x => x === "b", x => v = x); | |
|
|
72 | }, "Should throw when the sequence is empty"); | |
|
|
73 | ||
|
|
74 | t.throws(() => { | |
|
|
75 | firstWhere(sequence, x => x === "z"); | |
|
|
76 | }, "Should throw when the element isn't found"); | |
|
|
77 | ||
|
|
78 | t.throws(() => { | |
|
|
79 | firstWhere(sequence, x => x === "z", x => v = x); | |
|
|
80 | }, "Should throw when the element isn't found"); | |
|
|
81 | ||
|
|
69 | 82 | first(empty, null, x => e = x); |
|
|
70 | 83 | t.true(e, "The errorback should be called for the empty sequence"); |
|
|
71 | 84 | |
| @@ -39,29 +39,6 export class TapeWriter implements IDest | |||
|
|
39 | 39 | } |
|
|
40 | 40 | } |
|
|
41 | 41 | |
|
|
42 | export async function delay(timeout: number, ct: ICancellation = Cancellation.none) { | |
|
|
43 | let un: IDestroyable; | |
|
|
44 | ||
|
|
45 | try { | |
|
|
46 | await new Promise((resolve, reject) => { | |
|
|
47 | if (ct.isRequested()) { | |
|
|
48 | un = ct.register(reject); | |
|
|
49 | } else { | |
|
|
50 | const ht = setTimeout(() => { | |
|
|
51 | resolve(); | |
|
|
52 | }, timeout); | |
|
|
53 | ||
|
|
54 | un = ct.register(e => { | |
|
|
55 | clearTimeout(ht); | |
|
|
56 | reject(e); | |
|
|
57 | }); | |
|
|
58 | } | |
|
|
59 | }); | |
|
|
60 | } finally { | |
|
|
61 | destroy(un); | |
|
|
62 | } | |
|
|
63 | } | |
|
|
64 | ||
|
|
65 | 42 | export function test(name: string, cb: (t: tape.Test) => any) { |
|
|
66 | 43 | tape(name, async t => { |
|
|
67 | 44 | const writer = new TapeWriter(t); |
General Comments 0
You need to be logged in to leave comments.
Login now
