wait_for()
Wait for the selected event to happen before proceeding.
Method Syntax
db.wait_for(event)
Arguments
| Argument | Type | Description |
|---|
event | WaitFor | The event to wait for before proceeding. |
WaitFor is a simple enum with two variants representing the event to wait for.
pub enum WaitFor {
Connection,
Database,
}
Example usage
The following test from the source code demonstrates the behaviour of the .wait_for_() method in a variety of situations.
use std::task::Poll;
use surrealdb::engine::remote::ws::{Client, Ws};
use surrealdb::opt::auth::Root;
use surrealdb::opt::WaitFor::{Connection, Database};
use surrealdb::Surreal;
use futures::poll;
use std::pin::pin;
#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db: Surreal<Client> = Surreal::init();
assert_eq!(poll!(pin!(db.wait_for(Connection))), Poll::Pending);
assert_eq!(poll!(pin!(db.wait_for(Database))), Poll::Pending);
db.connect::<Ws>("127.0.0.1:8000").await.unwrap();
assert_eq!(poll!(pin!(db.wait_for(Connection))), Poll::Ready(()));
assert_eq!(poll!(pin!(db.wait_for(Database))), Poll::Pending);
db.signin(Root {
username: "root".to_string(),
password: "secret".to_string(),
})
.await
.unwrap();
assert_eq!(poll!(pin!(db.wait_for(Connection))), Poll::Ready(()));
assert_eq!(poll!(pin!(db.wait_for(Database))), Poll::Pending);
db.use_ns("namespace").await.unwrap();
assert_eq!(poll!(pin!(db.wait_for(Connection))), Poll::Ready(()));
assert_eq!(poll!(pin!(db.wait_for(Database))), Poll::Pending);
db.use_db("database").await.unwrap();
assert_eq!(poll!(pin!(db.wait_for(Connection))), Poll::Ready(()));
assert_eq!(poll!(pin!(db.wait_for(Database))), Poll::Ready(()));
Ok(())
}
See also