create an mqtt_dummy that sends string every second
All checks were successful
Unit Tests / Run Tests (push) Successful in 14s
All checks were successful
Unit Tests / Run Tests (push) Successful in 14s
This commit is contained in:
parent
ed37e1c550
commit
af4f5a850c
6 changed files with 133 additions and 4 deletions
48
src/mqtt_dummy.gleam
Normal file
48
src/mqtt_dummy.gleam
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import gleam/erlang/process
|
||||
import gleam/list
|
||||
import gleam/otp/actor
|
||||
|
||||
pub type Subscriber =
|
||||
process.Subject(String)
|
||||
|
||||
pub type Message {
|
||||
Subscribe(subscriber: Subscriber)
|
||||
Proc
|
||||
}
|
||||
|
||||
pub type Subscribers =
|
||||
List(Subscriber)
|
||||
|
||||
fn handle_message(
|
||||
subscribers: Subscribers,
|
||||
message: Message,
|
||||
) -> actor.Next(Subscribers, Message) {
|
||||
case message {
|
||||
Subscribe(subscriber) -> {
|
||||
let new_subscribers = [subscriber, ..subscribers]
|
||||
actor.continue(new_subscribers)
|
||||
}
|
||||
Proc -> {
|
||||
list.each(subscribers, fn(subscriber) {
|
||||
process.send(subscriber, "Hello from dummy mqtt action")
|
||||
})
|
||||
actor.continue(subscribers)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe to receive ticks from the dummy actor
|
||||
pub fn subscribe(actor_ref: process.Subject(Message)) -> Subscriber {
|
||||
let mailbox = process.new_subject()
|
||||
process.send(actor_ref, Subscribe(mailbox))
|
||||
mailbox
|
||||
}
|
||||
|
||||
// Start the actor
|
||||
pub fn start() -> Result(process.Subject(Message), actor.StartError) {
|
||||
let assert Ok(started) =
|
||||
actor.new([])
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
Ok(started.data)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue