Skip to content

Instantly share code, notes, and snippets.

@jasondown
Created December 23, 2021 18:49
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/11286701354cf53c9619e28d4281c452 to your computer and use it in GitHub Desktop.
Save jasondown/11286701354cf53c9619e28d4281c452 to your computer and use it in GitHub Desktop.
[<AutoOpen>]
module Customer =
type internal CustomerMessage =
| Order
| Done
type HungryCustomer (vendingMachine : VendingMachine, id : int, output : Printer) =
let agent = MailboxProcessor<CustomerMessage>.Start(fun inbox ->
let rec orderLoop () = async {
let! msg = inbox.Receive()
match msg with
| Order ->
output.Print (sprintf $"Customer {id} is still hungry and orders another")
vendingMachine.BuyItem id
return! orderLoop ()
| Done ->
output.Print (sprintf $"Customer {id} is getting full, so stops ordering")
return ()
}
orderLoop() )
member _.Order() = agent.Post Order
member _.Done() = agent.Post Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment