Skip to content

Commit

Permalink
Remove needless Option in Client.next_update (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimentyy authored Aug 11, 2024
1 parent 75d2c7f commit b49bb9d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
6 changes: 3 additions & 3 deletions lib/grammers-client/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async fn async_main() -> Result {
// This code uses `select` on Ctrl+C to gracefully stop the client and have a chance to
// save the session. You could have fancier logic to save the session if you wanted to
// (or even save it on every update). Or you could also ignore Ctrl+C and just use
// `while let Some(updates) = client.next_updates().await?`.
// `let update = client.next_update().await?`.
//
// Using `tokio::select!` would be a lot cleaner but add a heavy dependency,
// so a manual `select` is used instead by pinning async blocks by hand.
Expand All @@ -87,8 +87,8 @@ async fn async_main() -> Result {
};

let update = match update {
None | Some(Ok(None)) => break,
Some(u) => u?.unwrap(),
None => break,
Some(u) => u?,
};

let handle = client.clone();
Expand Down
29 changes: 21 additions & 8 deletions lib/grammers-client/examples/inline-pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,30 @@ async fn async_main() -> Result {
}

println!("Waiting for messages...");
while let Some(update) = client.next_update().await? {
let handle = client.clone();
task::spawn(async move {
match handle_update(handle, update).await {
Ok(_) => {}
Err(e) => eprintln!("Error handling updates!: {e}"),
loop {
tokio::select! {

// A way to exit the loop
_ = tokio::signal::ctrl_c() => {
println!("Exiting...");
break
},

res = client.next_update() => {
let update = res?;

let handle = client.clone();
task::spawn(async move {
match handle_update(handle, update).await {
Ok(_) => {}
Err(e) => eprintln!("Error handling updates!: {e}"),
}
});
}
});
}
}

println!("Saving session file and exiting...");
println!("Saving session file...");
client.session().save_to_file(SESSION_FILE)?;
Ok(())
}
Expand Down
5 changes: 3 additions & 2 deletions lib/grammers-client/examples/reconnection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ async fn async_main() -> Result {
/// happy listening to updates forever!!
use grammers_client::Update;

while let Some(update) = client.next_update().await? {
loop {
let update = client.next_update().await?;

match update {
Update::NewMessage(message) if !message.outgoing() => {
message.respond(message.text()).await?;
}
_ => {}
}
}
Ok(())
}

fn main() -> Result {
Expand Down
7 changes: 4 additions & 3 deletions lib/grammers-client/src/client/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ impl Client {
/// # async fn f(client: grammers_client::Client) -> Result<(), Box<dyn std::error::Error>> {
/// use grammers_client::Update;
///
/// while let Some(update) = client.next_update().await? {
/// loop {
/// let update = client.next_update().await?;
/// // Echo incoming messages and ignore everything else
/// match update {
/// Update::NewMessage(mut message) if !message.outgoing() => {
Expand All @@ -48,12 +49,12 @@ impl Client {
/// # Ok(())
/// # }
/// ```
pub async fn next_update(&self) -> Result<Option<Update>, InvocationError> {
pub async fn next_update(&self) -> Result<Update, InvocationError> {
loop {
let (deadline, get_diff, get_channel_diff) = {
let state = &mut *self.0.state.write().unwrap();
if let Some(updates) = state.updates.pop_front() {
return Ok(Some(updates));
return Ok(updates);
}
(
state.message_box.check_deadlines(), // first, as it might trigger differences
Expand Down

0 comments on commit b49bb9d

Please sign in to comment.