Skip to content

Instantly share code, notes, and snippets.

@jasondown
Last active December 23, 2021 06:15
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/72724fc6eb03d075d411095676881686 to your computer and use it in GitHub Desktop.
Save jasondown/72724fc6eb03d075d411095676881686 to your computer and use it in GitHub Desktop.
[<AutoOpen>]
module Output =
type internal OutputMessage =
| Print of string
type Printer(output : (string -> unit)) =
let agent = MailboxProcessor<OutputMessage>.Start(fun inbox ->
let rec loop () = async {
let! message = inbox.Receive()
match message with
| Print msg ->
output msg;
return! loop() }
loop() )
member _.Print text = agent.Post(Print text)
//test
let consolePrinter = Printer(System.Console.Out.WriteLine)
consolePrinter.Print("Hello!")
consolePrinter.Print("Goodbye!")
// fsi:
// Hello!
// val it : unit = ()
// Goodbye!
// val it : unit = ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment