gqf2008 / Xtask

一种多任务调度内核
MIT License
72 stars 11 forks source link

PubSub模式消息总线 #2

Closed gqf2008 closed 2 years ago

gqf2008 commented 2 years ago

发布订阅模式消息总线

static BUS: Bus = Bus::new();

[derive(Debug, Clone, Copy)]

enum Event {
    Key(u8),
    Mouse(isize, isize),
}

BUS.subscribe("ev.key", |topic, ev| match ev {
    Event::Key(code) => {
        sprintln!("{} {:?}", topic, ev);
    }
    _ => {}
});
BUS.subscribe("ev.mouse", |topic, ev| match ev {
    Event::Mouse(x, y) => {
        sprintln!("{} {:?}", topic, ev);
    }
    _ => {}
});

TaskBuilder::new().name("events").spawn(move || loop {
    BUS.event("ev.key", Event::Key(8));
    xtask::sleep_ms(1000);
});
TaskBuilder::new().name("events").spawn(move || loop {
    BUS.event("ev.mouse", Event::Mouse(8378, 10036));
    xtask::sleep_ms(500);
});