1- use std:: cell:: RefCell ;
1+ use std:: cell:: Cell ;
2+ use std:: panic:: AssertUnwindSafe ;
23use tokio_stream:: { StreamExt , StreamMap } ;
34use worker:: {
4- durable_object, wasm_bindgen , wasm_bindgen_futures , DurableObject , Env , Error , Method , Request ,
5- Response , ResponseBuilder , Result , State , WebSocket , WebSocketIncomingMessage , WebSocketPair ,
5+ durable_object, js_sys , wasm_bindgen , DurableObject , Env , Error , Method , Request , Response ,
6+ ResponseBuilder , Result , State , WebSocket , WebSocketIncomingMessage , WebSocketPair ,
67 WebsocketEvent ,
78} ;
89
910use crate :: SomeSharedData ;
1011
1112#[ durable_object]
1213pub struct Counter {
13- count : RefCell < usize > ,
14- unstored_count : RefCell < usize > ,
14+ count : AssertUnwindSafe < Cell < usize > > ,
15+ unstored_count : AssertUnwindSafe < Cell < usize > > ,
1516 state : State ,
16- initialized : RefCell < bool > ,
17+ initialized : AssertUnwindSafe < Cell < bool > > ,
1718 env : Env ,
1819}
1920
2021impl DurableObject for Counter {
2122 fn new ( state : State , env : Env ) -> Self {
2223 Self {
23- count : RefCell :: new ( 0 ) ,
24- unstored_count : RefCell :: new ( 0 ) ,
25- initialized : RefCell :: new ( false ) ,
24+ count : AssertUnwindSafe ( Cell :: new ( 0 ) ) ,
25+ unstored_count : AssertUnwindSafe ( Cell :: new ( 0 ) ) ,
26+ initialized : AssertUnwindSafe ( Cell :: new ( false ) ) ,
2627 state,
2728 env,
2829 }
2930 }
3031
3132 async fn fetch ( & self , req : Request ) -> Result < Response > {
32- if !* self . initialized . borrow ( ) {
33- * self . initialized . borrow_mut ( ) = true ;
34- * self . count . borrow_mut ( ) = self . state . storage ( ) . get ( "count" ) . await ?. unwrap_or ( 0 ) ;
33+ if !self . initialized . get ( ) {
34+ self . initialized . set ( true ) ;
35+ self . count
36+ . set ( self . state . storage ( ) . get ( "count" ) . await ?. unwrap_or ( 0 ) ) ;
3537 }
3638
3739 if req. path ( ) . eq ( "/ws" ) {
@@ -49,15 +51,15 @@ impl DurableObject for Counter {
4951 . empty ( ) ) ;
5052 }
5153
52- * self . unstored_count . borrow_mut ( ) += 1 ;
53- * self . count . borrow_mut ( ) += 10 ;
54- let count = * self . count . borrow ( ) ;
54+ self . unstored_count . set ( self . unstored_count . get ( ) + 1 ) ;
55+ self . count . set ( self . count . get ( ) + 10 ) ;
56+ let count = self . count . get ( ) ;
5557 self . state . storage ( ) . put ( "count" , count) . await ?;
5658
5759 Response :: ok ( format ! (
5860 "[durable_object]: self.count: {}, self.unstored_count: {}, secret value: {}" ,
59- self . count. borrow ( ) ,
60- self . unstored_count. borrow ( ) ,
61+ self . count. get ( ) ,
62+ self . unstored_count. get ( ) ,
6163 self . env. secret( "SOME_SECRET" ) ?
6264 ) )
6365 }
@@ -134,7 +136,7 @@ pub async fn handle_websocket(req: Request, env: Env, _data: SomeSharedData) ->
134136 let do_ws = res. websocket ( ) . expect ( "server did not accept websocket" ) ;
135137 do_ws. accept ( ) ?;
136138
137- wasm_bindgen_futures :: spawn_local ( async move {
139+ js_sys :: futures :: spawn_local ( async move {
138140 let event_stream = server. events ( ) . expect ( "could not open stream" ) ;
139141 let do_event_stream = do_ws. events ( ) . expect ( "could not open stream" ) ;
140142
0 commit comments