You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a question about how to manage state within durable object (which is also a more general question about how these objects work).
Using the example from the README:
#[durable_object]pubstructChatroom{users:Vec<User>,messages:Vec<Message>,state:State,env:Env,// access `Env` across requests, use inside `fetch`}#[durable_object]implDurableObjectforChatroom{fnnew(state:State,env:Env) -> Self{Self{users:vec![],messages:vec![],state: state,
env,}}asyncfnfetch(&mutself,_req:Request) -> Result<Response>{// do some work when a worker makes a request to this DOResponse::ok(&format!("{} active users.",self.users.len()))}}
Imagine that I want to have fetch update the state of the object (for example by storing an incoming message). I see that I have mutable access to things like self.messages, and also self.state. Is it as simple as appending something to self.messages ? Or do I need to use the transactional storage API via self.state.storage() ?
If it is indeed as simple as updating self.messages, then how does that state actually get persisted?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi all,
I have a question about how to manage state within durable object (which is also a more general question about how these objects work).
Using the example from the README:
Imagine that I want to have
fetch
update the state of the object (for example by storing an incoming message). I see that I have mutable access to things likeself.messages
, and alsoself.state
. Is it as simple as appending something toself.messages
? Or do I need to use the transactional storage API viaself.state.storage()
?If it is indeed as simple as updating
self.messages
, then how does that state actually get persisted?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions