Trait worker::durable::DurableObject [−][src]
pub trait DurableObject {
fn new(state: State, env: Env) -> Self;
fn fetch<'life0, 'async_trait>(
&'life0 mut self,
req: Request
) -> Pin<Box<dyn Future<Output = Result<Response>> + 'async_trait>>
where
'life0: 'async_trait,
Self: 'async_trait;
}
Expand description
Note: Implement this trait with a standard impl DurableObject for YourType
block, but in order to
integrate them with the Workers Runtime, you must also add the #[durable_object]
attribute
macro to both the impl block and the struct type definition.
Example
use worker::*;
#[durable_object]
pub struct Chatroom {
users: Vec<User>,
messages: Vec<Message>
state: State,
env: Env, // access `Env` across requests, use inside `fetch`
}
#[durable_object]
impl DurableObject for Chatroom {
fn new(state: State, env: Env) -> Self {
Self {
users: vec![],
messages: vec![],
state: state,
env,
}
}
async fn fetch(&mut self, _req: Request) -> Result<Response> {
// do some work when a worker makes a request to this DO
Response::ok(&format!("{} active users.", self.users.len()))
}
}