chatmail / chatmail/async-imap
How would you make an idiomatic idle loop?
- Dominant language
- Rust
- Stars
- 147
- Forks
- 42
- Avg merge
- 8d 15h
- Merged PRs (30d)
- 1
Description
An email client will most of the time use an IDLE loop to get the new messages/deleted messages events in real time.
What would be the best way to do this with async-imap as the session is borrowed by idle?
I have stored a session in a struct:
```rust
pub struct ImapClient {
session: async_imap::Session>
}
```
That session is initialized with a `try_new` method.
Then I have a method that makes an idle wait. It returns the number of fetched messages.
```rust
pub async fn wait_for_new_messages(&mut self) -> Result {
self.session.select("INBOX").await.unwrap();
let mut idle = self.session.idle(); // boom: self.session moved due to this method call
idle.init().await?
// code for idle/fetch EXISTS
}
```
cf [this file](https://github.com/iroco-co/mailstorm/blob/feat/fetch-messages/src/imap_client.rs)
The caller is responsible to loop like:
```rust
loop {
match imap_client.wait_for_new_messages().await {
Ok(nb) => debug!("received {} messages", nb),
Err(err) => error!("{:?}", err)
}
}
```
I understand that the session moves to the idle Handler, to return it when idle is done.
How could I use the session instance with idle?
(@hpk42 I will push a doc PR with my findings, I just want to reach a working example)
Thank you for your answers.
What I tried:
- encapsulate idle handler in the struct ImapClient but when creating the session, same issue is happening with self
- encapsulate the `Client` and recreating a session each time but that means re-authenticate at each idle loop step thus having an unnecessary network overhead
- encapsulate the Client and making the idle loop inside the ImapClient. But same issue happens with the borrow in the loop
Contributor guide
No contributing guide indexed for this repository
Research direction
Read the linked imap_client.rs file and the wait_for_new_messages method, then inspect how Session::idle transfers and returns the session. Work toward a documented working example for an idle loop that avoids re-authentication, and validate it against the caller loop shown in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- networking
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100