Add `withFacet` template

Simplifies the injection of `getCurrentFacet`.
This commit is contained in:
Emery Hemingway 2021-07-12 12:17:13 +02:00
parent 8c02bf807e
commit 4f7f7e7780
1 changed files with 18 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import std/[asyncdispatch, macros, options]
import preserves
import syndicate/[assertions, dataspaces, events, skeletons]
export preserves.`%`
export assertions.`?_`
export assertions.`?*`
export assertions.Observe
@ -178,6 +179,20 @@ template spawn*(name: string; spawnBody: untyped): untyped =
proc getCurrentFacet(): Facet {.inject, used.} = spawnFacet
spawnBody
template withFacet*(f: Facet; body: untyped): untyped =
## Execute a Syndicate ``body`` using the ``Facet`` at ``f``.
runnableExamples:
import preserves, preserves/records
type Foo = ref object
facet: Facet
i: int
proc incAndAssert(foo: Foo) =
inc(foo.i)
withFacet foo.facet:
react: assert: initRecord("Foo", %foo.i)
proc getCurrentFacet(): Facet {.inject, used.} = f
body
template syndicate*(ident, dataspaceBody: untyped): untyped =
proc `ident`*(facet: Facet) =
proc getCurrentFacet(): Facet {.inject, used.} = facet
@ -185,6 +200,8 @@ template syndicate*(ident, dataspaceBody: untyped): untyped =
when isMainModule:
asyncCheck bootModule("", `ident`)
template boot*(module: proc (facet: Facet) {.gcsafe.}) =
type BootProc* = proc (facet: Facet) {.gcsafe.}
template boot*(module: BootProc) =
mixin getCurrentFacet
module(getCurrentFacet())