1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#[macro_export]
macro_rules! implement_event_emitter {
    ($duration:expr, $max_size:expr) => {
        static mut _EVENT_HUB: Option<ic_event_hub::event_hub::EventHub> = None;

        pub fn get_event_hub() -> &'static mut ic_event_hub::event_hub::EventHub {
            unsafe {
                if let Some(s) = &mut _EVENT_HUB {
                    s
                } else {
                    _EVENT_HUB = Some(ic_event_hub::event_hub::EventHub::new($duration, $max_size));
                    get_event_hub()
                }
            }
        }

        pub fn _take_event_hub_state() -> Option<ic_event_hub::event_hub::EventHub> {
            unsafe { _EVENT_HUB.take() }
        }

        pub fn _put_event_hub_state(state: Option<ic_event_hub::event_hub::EventHub>) {
            unsafe { _EVENT_HUB = state }
        }

        pub fn emit(
            event: impl ic_event_hub::types::IEvent,
        ) -> Result<(), ic_event_hub::types::EventHubError> {
            ic_event_hub::fns::emit_impl(event, get_event_hub())
        }

        pub fn send_events() {
            ic_event_hub::fns::send_events_impl(get_event_hub());
        }
    };
}

#[macro_export]
macro_rules! implement_subscribe {
    () => {
        #[ic_cdk_macros::update]
        fn subscribe(req: ic_event_hub::types::SubscribeRequest) {
            ic_event_hub::fns::subscribe_impl(req, get_event_hub());
        }
    };

    (guard = $guard:expr) => {
        #[ic_cdk_macros::update(guard = $guard)]
        fn subscribe(req: ic_event_hub::types::SubscribeRequest) {
            ic_event_hub::fns::subscribe_impl(req, get_event_hub());
        }
    };
}

#[macro_export]
macro_rules! implement_unsubscribe {
    () => {
        #[ic_cdk_macros::update]
        fn unsubscribe(req: ic_event_hub::types::UnsubscribeRequest) {
            ic_event_hub::fns::unsubscribe_impl(req, get_event_hub());
        }
    };

    (guard = $guard:expr) => {
        #[ic_cdk_macros::update(guard = $guard)]
        fn unsubscribe(req: ic_event_hub::types::UnsubscribeRequest) {
            ic_event_hub::fns::unsubscribe_impl(req, get_event_hub());
        }
    };
}