-
-
Save jasondown/72724fc6eb03d075d411095676881686 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[<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