Skip to content

Commit

Permalink
Replace next_update with update_stream
Browse files Browse the repository at this point in the history
  • Loading branch information
YouKnow-sys authored and Lonami committed Dec 20, 2024
1 parent 91f6d23 commit b942670
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 97 deletions.
5 changes: 4 additions & 1 deletion lib/grammers-client/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//! cargo run --example echo -- BOT_TOKEN
//! ```
use futures::StreamExt;
use futures_util::future::{select, Either};
use grammers_client::session::Session;
use grammers_client::{Client, Config, InitParams, Update};
Expand Down Expand Up @@ -69,6 +70,8 @@ async fn async_main() -> Result {
println!("Signed in!");
}

let mut update_stream = client.update_stream();

println!("Waiting for messages...");

// This code uses `select` on Ctrl+C to gracefully stop the client and have a chance to
Expand All @@ -80,7 +83,7 @@ async fn async_main() -> Result {
// so a manual `select` is used instead by pinning async blocks by hand.
loop {
let exit = pin!(async { tokio::signal::ctrl_c().await });
let upd = pin!(async { client.next_update().await });
let upd = pin!(async { update_stream.select_next_some().await });

let update = match select(exit, upd).await {
Either::Left(_) => break,
Expand Down
5 changes: 4 additions & 1 deletion lib/grammers-client/examples/inline-pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//! how much data a button's payload can contain, and to keep it simple, we're storing it inline
//! in decimal, so the numbers can't get too large).
use futures::StreamExt;
use futures_util::future::{select, Either};
use grammers_client::session::Session;
use grammers_client::{button, reply_markup, Client, Config, InputMessage, Update};
Expand Down Expand Up @@ -132,10 +133,12 @@ async fn async_main() -> Result {
println!("Signed in!");
}

let mut update_stream = client.update_stream();

println!("Waiting for messages...");
loop {
let exit = pin!(async { tokio::signal::ctrl_c().await });
let upd = pin!(async { client.next_update().await });
let upd = pin!(async { update_stream.select_next_some().await });

let update = match select(exit, upd).await {
Either::Left(_) => {
Expand Down
22 changes: 13 additions & 9 deletions lib/grammers-client/examples/reconnection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! this example demonstrate how to implement custom Reconnection Polies
use futures::TryStreamExt;
use grammers_client::session::Session;
use grammers_client::{Client, Config, InitParams, ReconnectionPolicy};
use std::ops::ControlFlow;
Expand Down Expand Up @@ -42,16 +43,19 @@ async fn async_main() -> Result {
/// happy listening to updates forever!!
use grammers_client::Update;

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

match update {
Update::NewMessage(message) if !message.outgoing() => {
message.respond(message.text()).await?;
client
.update_stream()
.try_for_each_concurrent(None, |update| async {
match update {
Update::NewMessage(message) if !message.outgoing() => {
message.respond(message.text()).await.map(|_| ())
}
_ => Ok(()),
}
_ => {}
}
}
})
.await?;

Ok(())
}

fn main() -> Result {
Expand Down
Loading

0 comments on commit b942670

Please sign in to comment.