This commit is contained in:
parent
cd5b816e69
commit
fbcafef67c
1 changed files with 0 additions and 52 deletions
|
|
@ -1,52 +0,0 @@
|
||||||
import gleam/erlang/process
|
|
||||||
import gleam/list
|
|
||||||
import gleam/otp/actor
|
|
||||||
import sensors
|
|
||||||
|
|
||||||
pub type Subscriber =
|
|
||||||
process.Subject(sensors.SensorReading)
|
|
||||||
|
|
||||||
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,
|
|
||||||
sensors.SensorReading(sensors.Temperature, 26.6),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
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