Rustでnodeっぽくsentinterval & emit event

実装メモです

*Cargo.toml*

```toml
[dependencies]
tokio = { version = "0.2", features = ["full"] }
futures = "0.3"
eventbus = "0.5.1"
lazy_static = "1.4.0"
```

*main.rs*

```rust
use tokio::time;
use std::time::Duration;
use eventbus::{Event, EventBus, register_hook, post_event};
use tokio::signal;
use lazy_static::lazy_static;
use std::sync::{Arc, Mutex};

lazy_static! {
    static ref EVENT_BUS : Arc<Mutex<EventBus>> = Arc::new(Mutex::new(EventBus::new()));
}

struct TickEvent {
    subscriber: TickSubscriber,
}

impl Event for TickEvent {
}

struct TickPublisher {
    event: TickEvent,
}

impl TickPublisher {
    fn new() -> Self {
        let event = TickEvent {
            subscriber: TickSubscriber::new(),
        };
        Self {
            event,
        }
    }

    #[allow(deprecated)]
    fn register_add_handler(&self, priority: Option<i32>) {
        let priority = match priority {
            Some(inner) => inner,
            None => 1,
        };
        let event_bus = EVENT_BUS.lock().unwrap();
        register_hook!(&event_bus, priority, TickEvent, Self::add_handler);
    }

    #[allow(deprecated)]
    fn register_nonehandler(&self, priority: Option<i32>) {
        let priority = match priority {
            Some(inner) => inner,
            None => 1,
        };
        let event_bus = EVENT_BUS.lock().unwrap();
        // let event_bus = EventBus::new();
        register_hook!(&event_bus, priority, TickEvent, Self::none_handler);
    }

    #[allow(deprecated)]
    async fn ticks(&mut self) {
        let mut interval = time::interval(Duration::from_secs(1));
        let mut count = 0;
        loop {
            print!("let's wait 1 sec");
            interval.tick().await;
            print!("...{}...", count);
            println!("wait...completed!");
            count += 1;
            let event_bus = EVENT_BUS.lock().unwrap();
            // let event_bus = EventBus::new();
            post_event!(&event_bus, &mut self.event, TickEvent);
        }
    }
    fn add_handler(e: &mut TickEvent) {
        /* adds 1 */
        println!("add_handler called");
        e.subscriber.add();
    }
    fn none_handler(e: &mut TickEvent) {
        println!("none_handler called");
        e.subscriber.none();
    }
}

struct TickSubscriber {
    count: i32,
}

impl TickSubscriber {
    fn new() -> Self {
        Self {
            count: 0,
        }
    }
    pub fn add(&mut self) {
        self.count += 1;
        println!("subscriber count: {}", self.count);
    }
    pub fn none(&mut self) {
        println!("stay: {}", self.count);
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let rt = tokio::runtime::Runtime::new().unwrap();

    let task1 = async {
        let mut ticker = TickPublisher::new();
        ticker.register_add_handler(Some(1));
        ticker.ticks().await;
    };
    rt.spawn(task1);

    /* task2 */
    let task2 = async {
        let mut ticker = TickPublisher::new();
        ticker.register_nonehandler(Some(2));
        ticker.ticks().await;
    };
    rt.spawn(task2);

    signal::ctrl_c().await?;
    println!("ctrl-c received!");
    Ok(())
}
```