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
/*
 * Copyright Stalwart Labs Ltd. See the COPYING
 * file at the top-level directory of this distribution.
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * file at the top-level directory of this distribution.
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
 * option. This file may not be copied, modified, or distributed
 * except according to those terms.
 */

#[cfg(feature = "async")]
use futures_util::StreamExt;
#[cfg(feature = "async")]
use jmap_client::{client::Client, TypeState};

#[cfg(feature = "async")]
async fn event_source() {
    // Connect to the JMAP server using Basic authentication
    let client = Client::new()
        .credentials(("john@example.org", "secret"))
        .connect("https://jmap.example.org")
        .await
        .unwrap();

    // Open EventSource connection
    let mut stream = client
        .event_source(
            [
                TypeState::Email,
                TypeState::EmailDelivery,
                TypeState::Mailbox,
                TypeState::EmailSubmission,
                TypeState::Identity,
            ]
            .into(),
            false,
            60.into(),
            None,
        )
        .await
        .unwrap();

    // Consume events
    while let Some(event) = stream.next().await {
        let changes = event.unwrap();
        println!("-> Change id: {:?}", changes.id());
        for account_id in changes.changed_accounts() {
            println!(" Account {} has changes:", account_id);
            if let Some(account_changes) = changes.changes(account_id) {
                for (type_state, state_id) in account_changes {
                    println!("   Type {:?} has a new state {}.", type_state, state_id);
                }
            }
        }
    }
}

fn main() {
    #[cfg(feature = "async")]
    let _c = event_source();
}