Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Last active June 13, 2021 20:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save isaacabraham/7109018e483ae839b608 to your computer and use it in GitHub Desktop.
Save isaacabraham/7109018e483ae839b608 to your computer and use it in GitHub Desktop.
Local Mailbox Processor
// Our simple domain model
type Priority =
| Low
| Normal
| High
type Message = {
Text : string
Priority : Priority
}
type MessageCollection =
{ Count : int
Messages : Message list }
type Command =
| Record of Message // Add a new message to file
| Clear // Wipe out all messages
/// Create an Mailbox Processor for a specific actor.
let CreateActor (ActorKey name) =
MailboxProcessor.Start(fun mailbox ->
let messageStore = GetMessageStore name
let rec loop data =
async {
// Wait asynchronously until we get a message
let! message = mailbox.Receive()
// Process the message
match message with
| Clear ->
// Delete file and loop around.
messageStore.DeleteIfExists()
return! loop { Count = 0; Messages = [] }
| Record message ->
// Append the new message to file.
let updatedData =
{ data with
Count = data.Count + 1
Messages = data.Messages @ [ message ] }
messageStore.SetData(updatedData)
return! (loop updatedData)
}
// Start by loading the existing data, then loop indefinitely
let data = defaultArg (messageStore.GetData()) { Count = 0; Messages = [] }
loop data)
// Create an actor and post to it locally
let isaac = CreateActor "isaac"
isaac.Post(Record { Text = "Hello"; Priority = Priority.Low })
@Thorium
Copy link

Thorium commented Dec 11, 2014

Hi Isaac,
Nice...but what are "ActorKey" and "GetMessageStore"?

@bohdanszymanik
Copy link

Bit confusing without knowing what the messagestore is...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment