Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kuon
Created April 19, 2021 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuon/b81f6397f454f0254f7476563b1794c0 to your computer and use it in GitHub Desktop.
Save kuon/b81f6397f454f0254f7476563b1794c0 to your computer and use it in GitHub Desktop.
elm architecture in rust
use ten::prelude::*;
use ten::prelude::time::{Time, Instant};
#[derive(Debug)]
pub enum Msg {
Tick(Instant),
}
#[derive(Debug)]
pub struct Model {
tick_count: u64,
}
fn init(_: Nothing) -> (Model, Cmd<Msg>) {
(Model { tick_count: 0 }, Cmd::none())
}
fn update(msg: Msg, mut model: Model) -> (Model, Cmd<Msg>) {
match msg {
Msg::Tick(_) => {
model.tick_count += 1;
println!("ticks {}", model.tick_count);
let cmd = if model.tick_count >= 20 {
Cmd::batch([Process::exit(0)])
} else {
Cmd::none()
};
(model, cmd)
}
}
}
fn subscriptions(model: &Model) -> Sub<Msg> {
// Do ten fast ticks and then slow ticks
let interval = if model.tick_count < 10 { 100 } else { 1000 };
Time::every_ms(interval, Msg::Tick)
}
fn main() {
let app = Worker::application(init, update, subscriptions);
Runtime::run_program(app, Nothing);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment