src/sunk

Installation:

nimble install sunk

Types

CyclicOps = ref object
  status*: CStatus = running
PendingOps = ref object
  status*: PStatus = pending

Procs

proc `$`(cycle: CyclicOps): string {....raises: [], tags: [], forbids: [].}
Prints out a cyclic operation
proc `$`(pend: PendingOps): string {....raises: [], tags: [], forbids: [].}
Prints out a pending operation
proc after(ms: int or float; todo: proc ()): PendingOps
Executes actions passed as todo after ms milliseconds without blocking the main execution flow while waiting. An equivalent of javascript's setTimeout

Example:

discard after(2_500) do():
  echo "2.5 seconds passed !"

var pend: PendingOps = after(3_000) do():
  echo "This line will never be executed !"

# Let's cancel the second pennding process
# 1.5 seconds before its execution :
discard after(1_500) do(): cancel pend
proc cancel(pend: PendingOps) {....raises: [], tags: [], forbids: [].}
Cancels a pending operation
proc catch[T](fut: Future[T]; todo: proc ())

Example:

import std/[asyncdispatch, httpclient]
var
  client = newAsyncHttpClient()
  f = client.getContent("https://google.com")
f.catch do(): echo "Failed !"
proc catch[T](fut: Future[T]; todo: proc (error: ref Exception))

Example:

import std/[asyncdispatch, httpclient]
var
  client = newAsyncHttpClient()
  f = client.getContent("https://google.com")
f.catch do(what_s_wrong: ref Exception): echo what_s_wrong.msg
proc doAfter(todo: proc (); ms: int or float): PendingOps
Executes actions passed as todo after ms milliseconds without blocking the main execution flow while waiting

Example:

proc todo() = echo "2.5 seconds passed !"
discard todo.doAfter(2_500)
proc doEvery(todo: proc (); ms: int or float): CyclicOps
Executes actions passed as todo every ms milliseconds without blocking the main execution flow while waiting

Example:

proc todo() = echo "This line will be executed three times !"
var cycle = todo.doEvery(2_000)

# To stop the background process after 6.5 seconds:
discard after(6_500) do(): stop cycle
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

Example:

import threadpool, os

var hasFinished = false

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

proc notify() = echo "Finished !"

spawn longOps()
notify.doOnce(hasFinished)
proc every(ms: int or float; todo: proc ()): CyclicOps
Executes actions passed as todo every ms milliseconds without blocking the main execution flow while waiting. An equivalent of javascript's setInterval

Example:

var cycle: CyclicOps = every(2_000) do():
  echo "This line will be executed three times !"

# To stop the background process after 6.5 seconds:
discard after(6_500) do(): stop cycle
proc `finally`[T](fut: Future[T]; todo: proc ())

Example:

import std/[asyncdispatch, httpclient]
var
  client = newAsyncHttpClient()
  f = client.getContent("https://google.com")
f.finally do(): echo "Finished !"
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

Example:

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 !"
proc once(cond: var bool; todo: proc ()) {....raises: [Exception],
    tags: [TimeEffect, RootEffect], forbids: [].}
Checks cond in background every 5 milliseconds and executes actions passed as todo once it's true

Example:

import threadpool, os

var hasFinished = false

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

spawn longOps()
once(hasFinished) do(): echo "Finished !"
proc pause(cycle: CyclicOps) {....raises: [], tags: [], forbids: [].}
Pauses a cyclic operation
proc resume(cycle: CyclicOps) {....raises: [], tags: [], forbids: [].}
Resumes a cyclic operation
proc stop(cycle: CyclicOps) {....raises: [], tags: [], forbids: [].}
Stops a cyclic operation
proc then[T](fut: Future[T]; todo: proc ())

Example:

import std/[asyncdispatch, httpclient]
var
  client = newAsyncHttpClient()
  f = client.getContent("https://google.com")
f.then do(): echo "Finished with success !"
proc then[T](fut: Future[T]; todo: proc (value: T))

Example:

import std/[asyncdispatch, httpclient]
var
  client = newAsyncHttpClient()
  f = client.getContent("https://google.com")
f.then do(return_value: string): echo return_value