From c93292e2f7a8d3bf7c6fbadccafe1516d77b4ece Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Sun, 19 Jun 2016 12:56:37 -0300 Subject: Add withTimeout proc for futures --- lib/pure/asyncdispatch.nim | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib/pure') diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim index bb19f87ef..082fe40ff 100644 --- a/lib/pure/asyncdispatch.nim +++ b/lib/pure/asyncdispatch.nim @@ -1542,6 +1542,24 @@ proc sleepAsync*(ms: int): Future[void] = p.timers.push((epochTime() + (ms / 1000), retFuture)) return retFuture +proc withTimeout*[T](fut: Future[T], timeout: int): Future[bool] = + ## Returns a future which will complete once ``fut`` completes or after + ## ``timeout`` milliseconds has elapsed. + ## + ## If ``fut`` completes first the returned future will hold true, + ## otherwise, if ``timeout`` milliseconds has elapsed first, the returned + ## future will hold false. + + var retFuture = newFuture[bool]("asyncdispatch.`withTimeout`") + var timeoutFuture = sleepAsync(timeout) + fut.callback = + proc () = + if not retFuture.finished: retFuture.complete(true) + timeoutFuture.callback = + proc () = + if not retFuture.finished: retFuture.complete(false) + return retFuture + proc accept*(socket: AsyncFD, flags = {SocketFlag.SafeDisconn}): Future[AsyncFD] = ## Accepts a new connection. Returns a future containing the client socket -- cgit 1.4.1-2-gfad0 '/akspecs/aerc/commit/widgets/pgpinfo.go?id=bc593ac7cdb20621aba685987f2a92c9bd880358'>commit diff stats
path: root/widgets/pgpinfo.go
blob: febf29ad61972b4ad45bc691c2aeb9beb3dc3821 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79