Skip to content

Instantly share code, notes, and snippets.

@jasondown
Last active December 23, 2021 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasondown/a769d0cf50c229e1d5e6d01f9f60386b to your computer and use it in GitHub Desktop.
Save jasondown/a769d0cf50c229e1d5e6d01f9f60386b to your computer and use it in GitHub Desktop.
Simple output message agent
type OutputMessage =
| Print of string
let agent output = MailboxProcessor<OutputMessage>.Start(fun inbox ->
let rec loop () = async {
let! message = inbox.Receive()
match message with
| Print msg ->
output msg
return! loop() }
loop() )
//test
let consoleAgent = agent System.Console.Out.WriteLine
consoleAgent.Post(Print "hello agent")
consoleAgent.Post(Print "hello again")
// fsi:
// hello agent
// val it : unit = ()
// hello again
// val it : unit = ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment