Skip to content

Commit

Permalink
You can now choose the delay of once and doOnce
Browse files Browse the repository at this point in the history
  • Loading branch information
archnim committed May 7, 2023
1 parent b7f5cbd commit d270474
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
32 changes: 29 additions & 3 deletions src/sunk.nim
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ proc doEvery*(todo: proc (), ms: int or float): CyclicOps =

return cycle

proc once*(cond: var bool, ms: int or float, todo: proc ()) =
## Checks `cond` in background every `ms` milliseconds
## and executes actions passed as `todo` once it's true

runnableExamples:
import threadpool, os

var hasFinished = false

proc longOps() =
# let's fake long operations with sleep
sleep 5_000
hasFinished = true

spawn longOps()
once(hasFinished, 10) do(): echo "Finished !"

let cond = cond.addr

let p = proc () {.async.} =
while not cond[]:
await sleepAsync(ms)
todo()

discard p()

proc once*(cond: var bool, todo: proc ()) =
## Checks `cond` in background every 5 milliseconds
## and executes actions passed as `todo` once it's true
Expand All @@ -175,8 +201,8 @@ proc once*(cond: var bool, todo: proc ()) =

discard p()

proc doOnce*(todo: proc (), cond: var bool) =
## Checks `cond` in background every 5 milliseconds
proc doOnce*(todo: proc (), cond: var bool, ms: int or float = 5) =
## Checks `cond` in background every `ms` milliseconds
## and executes actions passed as `todo` once it's true

runnableExamples:
Expand All @@ -198,7 +224,7 @@ proc doOnce*(todo: proc (), cond: var bool) =

let p = proc () {.async.} =
while not cond[]:
await sleepAsync(5)
await sleepAsync(ms)
todo()

discard p()
Expand Down
2 changes: 1 addition & 1 deletion sunk.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.1.1"
version = "0.1.2"
author = "archnim"
description = "Few async tools for nim (then, catch, finally, and more)"
license = "MIT"
Expand Down

0 comments on commit d270474

Please sign in to comment.