Trait worker::durable::DurableObject
source · pub trait DurableObject {
// Required methods
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 Self: 'async_trait,
'life0: 'async_trait;
// Provided methods
fn alarm<'life0, 'async_trait>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<Response>> + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn websocket_message<'life0, 'async_trait>(
&'life0 mut self,
ws: WebSocket,
message: WebSocketIncomingMessage
) -> Pin<Box<dyn Future<Output = Result<()>> + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn websocket_close<'life0, 'async_trait>(
&'life0 mut self,
ws: WebSocket,
code: usize,
reason: String,
was_clean: bool
) -> Pin<Box<dyn Future<Output = Result<()>> + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn websocket_error<'life0, 'async_trait>(
&'life0 mut self,
ws: WebSocket,
error: Error
) -> Pin<Box<dyn Future<Output = Result<()>> + 'async_trait>>
where Self: 'async_trait,
'life0: '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,
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()))
}
}
Required Methods§
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
Self: 'async_trait,
'life0: 'async_trait,
Provided Methods§
fn alarm<'life0, 'async_trait>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<Response>> + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn websocket_message<'life0, 'async_trait>(
&'life0 mut self,
ws: WebSocket,
message: WebSocketIncomingMessage
) -> Pin<Box<dyn Future<Output = Result<()>> + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn websocket_close<'life0, 'async_trait>(
&'life0 mut self,
ws: WebSocket,
code: usize,
reason: String,
was_clean: bool
) -> Pin<Box<dyn Future<Output = Result<()>> + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn websocket_error<'life0, 'async_trait>(
&'life0 mut self,
ws: WebSocket,
error: Error
) -> Pin<Box<dyn Future<Output = Result<()>> + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Object Safety§
This trait is not object safe.