#lunatic #response #message #reply

lunatic-message-request

Message responses using regular lunatic processes

2 releases

0.1.1 Mar 7, 2023
0.1.0 Feb 27, 2023

#923 in WebAssembly

Apache-2.0/MIT

14KB
277 lines

Message responses using regular lunatic processes.

Regular lunatic processes don't typically support sending responses in messages. This library provides a type MessageRequest which adds support for replying back with a value.

Example

use lunatic::spawn_link;
use lunatic_message_request::{MessageRequest, ProcessRequest};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
enum Message {
    Inc,
    Dec,
    Count(MessageRequest<(), i32>),
}

let counter = spawn_link!(|mailbox: Mailbox<Message>| {
    let mut count = 0;

    loop {
        let msg = mailbox.receive();
        match msg {
            Message::Inc => count += 1,
            Message::Dec => count -= 1,
            Message::Count(req) => req.reply(count),
        }
    }
});

// Initial count should be 0
let count = counter.request(Message::Count, ());
assert_eq!(count, 0);

// Increment count
counter.send(Message::Inc);

// Count should now be 1
let count = counter.request(Message::Count, ());
assert_eq!(count, 1);

License

Licensed under either

at your option.

Dependencies

~1–1.7MB
~36K SLoC