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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::cmp::{max, min, Ordering};
use std::collections::{BTreeSet, BinaryHeap};
use candid::types::{Serializer, Type};
use candid::{decode_one, CandidType, Deserialize};
use ic_cdk::export::Principal;
use crate::EVENT_NAME_FIELD;
#[derive(Eq, PartialEq, PartialOrd, Ord, Hash, Clone, Debug, CandidType, Deserialize)]
pub struct EventField {
pub name: String,
pub value: Vec<u8>,
}
#[derive(Clone, Debug, CandidType, Deserialize)]
pub struct Event {
pub topics: BTreeSet<EventField>,
pub values: Vec<EventField>,
}
impl Event {
pub fn get_name(&self) -> String {
let encoded_name = self
.topics
.iter()
.find(|&field| field.name == EVENT_NAME_FIELD)
.cloned()
.unwrap()
.value;
decode_one::<String>(encoded_name.as_slice()).unwrap()
}
}
pub trait IEvent {
fn to_event(&self) -> Event;
fn from_event(event: Event) -> Self;
}
#[derive(Eq, PartialEq, PartialOrd, Ord, Hash, Clone, Debug, CandidType, Deserialize)]
pub struct EventFilter(pub BTreeSet<EventField>);
impl EventFilter {
pub fn empty() -> Self {
Self(BTreeSet::new())
}
}
pub trait IEventFilter {
fn to_event_filter(&self) -> EventFilter;
fn from_event_filter(filter: EventFilter) -> Self;
}
#[derive(CandidType, Deserialize)]
pub struct CallbackInfo {
pub filter: EventFilter,
pub method_name: String,
}
#[derive(CandidType, Deserialize)]
pub struct CallbackInfoExt {
pub filter: EventFilter,
pub endpoint: RemoteCallEndpoint,
}
#[derive(Debug)]
pub enum EventHubError {
EventHasNoActiveListeners,
EventIsTooBig,
}
#[derive(CandidType, Deserialize)]
pub struct EncodedEventBatch {
pub content: Vec<u8>,
pub events_count: usize,
pub timestamp: u64,
}
impl EncodedEventBatch {
pub fn new(content: &[u8], timestamp: u64) -> Self {
Self {
content: Vec::from(content),
events_count: 1,
timestamp,
}
}
pub fn add_event(&mut self, content: &[u8]) {
self.content.extend_from_slice(content);
self.events_count += 1;
}
}
#[derive(Eq, CandidType, Deserialize, Clone)]
pub struct TimestampedRemoteCallEndpoint {
pub timestamp: u64,
pub endpoint: RemoteCallEndpoint,
}
impl PartialEq for TimestampedRemoteCallEndpoint {
fn eq(&self, other: &Self) -> bool {
self.timestamp.eq(&other.timestamp) && self.endpoint.eq(&other.endpoint)
}
}
impl PartialOrd for TimestampedRemoteCallEndpoint {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.timestamp
.partial_cmp(&other.timestamp)
.map(|it| it.reverse())
}
fn lt(&self, other: &Self) -> bool {
self.timestamp.gt(&other.timestamp)
}
fn le(&self, other: &Self) -> bool {
self.timestamp.ge(&other.timestamp)
}
fn gt(&self, other: &Self) -> bool {
self.timestamp.lt(&other.timestamp)
}
fn ge(&self, other: &Self) -> bool {
self.timestamp.le(&other.timestamp)
}
}
impl Ord for TimestampedRemoteCallEndpoint {
fn cmp(&self, other: &Self) -> Ordering {
self.timestamp.cmp(&other.timestamp).reverse()
}
fn max(self, other: Self) -> Self
where
Self: Sized,
{
max(self, other)
}
fn min(self, other: Self) -> Self
where
Self: Sized,
{
min(self, other)
}
fn clamp(self, min: Self, max: Self) -> Self
where
Self: Sized,
{
if self.timestamp < max.timestamp {
max
} else if self.timestamp > min.timestamp {
min
} else {
self
}
}
}
#[derive(CandidType, Deserialize)]
pub struct SubscribeRequest {
pub callbacks: Vec<CallbackInfo>,
}
pub type UnsubscribeRequest = SubscribeRequest;
#[derive(CandidType, Deserialize)]
pub struct GetSubscribersRequest {
pub filters: Vec<EventFilter>,
}
#[derive(CandidType, Deserialize)]
pub struct GetSubscribersResponse {
pub subscribers: Vec<Vec<RemoteCallEndpoint>>,
}
#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug, CandidType, Deserialize)]
pub struct RemoteCallEndpoint {
pub canister_id: Principal,
pub method_name: String,
}
#[derive(Deserialize, Clone)]
pub struct EventQueue(pub BinaryHeap<TimestampedRemoteCallEndpoint>);
impl CandidType for EventQueue {
fn _ty() -> Type {
Type::Vec(Box::new(TimestampedRemoteCallEndpoint::ty()))
}
fn idl_serialize<S>(&self, serializer: S) -> Result<(), S::Error>
where
S: Serializer,
{
self.clone().0.into_sorted_vec().idl_serialize(serializer)
}
}