New "addCallback" proc for processing Futures in Turns

This commit is contained in:
Emery Hemingway 2022-09-04 11:50:46 -05:00
parent 9b0437e922
commit 96d3bbb500
3 changed files with 19 additions and 5 deletions

View File

@ -32,9 +32,9 @@ import ./syndicate/protocols/dataspace
from ./syndicate/relays import connectStdio, connectUnix
export Actor, Assertion, Facet, Handle, Ref, Symbol, Turn, TurnAction,
`$`, `?`, analyse, asyncCheck, bootDataspace, connectStdio, connectUnix,
drop, facet, future, grab, message, newDataspace, publish, retract,
replace, run, stop, unembed
`$`, `?`, addCallback, analyse, asyncCheck, bootDataspace, connectStdio,
connectUnix, drop, facet, future, grab, message, newDataspace, publish,
retract, replace, run, stop, unembed
type
Observe* = dataspace.Observe[Ref]

View File

@ -421,8 +421,9 @@ proc terminate(facet; e: ref Exception) =
facet.actor.terminate(turn, e)
proc asyncCheck*(turn; fut: FutureBase) =
## Sets a callback on `fut` which propagates exceptions to the facet of `turn`.
let facet = turn.facet
fut.addCallback do ():
fut.addCallback do:
if fut.failed: terminate(facet, fut.error)
template tryFacet(facet; body: untyped) =
@ -452,6 +453,19 @@ proc run*(`ref`: Ref; action: TurnAction) =
## Convenience proc to run a `TurnAction` in the scope of a `Ref`.
run(`ref`.relay, action)
proc addCallback*(fut: FutureBase; turn: var Turn; act: TurnAction) =
## Add a callback to a `Future` that will be called at a later `Turn` with the same context as the current.
let facet = turn.facet
if fut.finished:
enqueue(turn, facet, act)
else:
fut.addCallback do:
run(facet, act)
proc addCallback*[T](fut: Future[T]; turn: var Turn; cb: proc (t: var Turn; f: Future[T]) {.closure, gcsafe.}) =
## Add a callback to a `Future` that will be called at a later `Turn` with the same context as the current.
addCallback(fut, turn) do (turn: var Turn): cb(turn, fut)
proc stop*(turn: var Turn, facet: Facet) =
enqueue(turn, facet.parent.get) do (turn: var Turn):
facet.terminate(turn, true)

View File

@ -1,6 +1,6 @@
# Package
version = "20220831"
version = "20220904"
author = "Emery Hemingway"
description = "Syndicated actors for conversational concurrency"
license = "Unlicense"