Skip to content

Commit 153f1f0

Browse files
committed
remove unnecessary cell
1 parent c6a79d3 commit 153f1f0

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

worker-sandbox/src/shared_counter.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use worker::*;
55
#[shared_durable_object]
66
pub struct SharedCounter {
77
count: RefCell<usize>,
8-
state: RefCell<State>,
8+
state: State,
99
initialized: RefCell<bool>,
1010
env: Env,
1111
}
@@ -16,24 +16,22 @@ impl SharedDurableObject for SharedCounter {
1616
Self {
1717
count: RefCell::new(0),
1818
initialized: RefCell::new(false),
19-
state: RefCell::new(state),
19+
state,
2020
env,
2121
}
2222
}
2323

2424
async fn fetch(&self, req: Request) -> Result<Response> {
2525
if !*self.initialized.borrow() {
2626
*self.initialized.borrow_mut() = true;
27-
let storage = self.state.borrow().storage();
28-
let count = storage.get("count").await.unwrap_or(0);
29-
*self.count.borrow_mut() = count;
27+
*self.count.borrow_mut() = self.state.storage().get("count").await.unwrap_or(0);
3028
}
3129

3230
if req.path().eq("/ws") {
3331
let pair = WebSocketPair::new()?;
3432
let server = pair.server;
3533
// accept websocket with hibernation api
36-
self.state.borrow().accept_web_socket(&server);
34+
self.state.accept_web_socket(&server);
3735
server
3836
.serialize_attachment("hello")
3937
.expect("failed to serialize attachment");
@@ -48,9 +46,10 @@ impl SharedDurableObject for SharedCounter {
4846
TimeoutFuture::new(1_000).await;
4947

5048
*self.count.borrow_mut() += 15;
51-
let mut storage = self.state.borrow().storage();
52-
let count = *self.count.borrow();
53-
storage.put("count", count).await?;
49+
self.state
50+
.storage()
51+
.put("count", *self.count.borrow())
52+
.await?;
5453

5554
Response::ok(format!(
5655
"[durable_object]: self.count: {}, secret value: {}",
@@ -72,10 +71,9 @@ impl SharedDurableObject for SharedCounter {
7271
TimeoutFuture::new(1_000).await;
7372

7473
// get and increment storage by 15
75-
let mut storage = self.state.borrow().storage();
76-
let mut count = storage.get("count").await.unwrap_or(0);
74+
let mut count = self.state.storage().get("count").await.unwrap_or(0);
7775
count += 15;
78-
storage.put("count", count).await?;
76+
self.state.storage().put("count", count).await?;
7977
// send value to client
8078
ws.send_with_str(format!("{}", count))
8179
.expect("failed to send value to client");

0 commit comments

Comments
 (0)