@@ -8,8 +8,8 @@ use std::{borrow::Borrow, cell::RefCell, io::Cursor, rc::Rc, time::Duration, vec
88use wasm_bindgen:: prelude:: * ;
99use wasm_bindgen_futures:: JsFuture ;
1010use web_sys:: {
11- DomException , IdbDatabase , IdbIndexParameters , IdbKeyRange , IdbObjectStoreParameters ,
12- IdbOpenDbRequest , IdbRequest , IdbTransactionMode ,
11+ DomException , IdbDatabase , IdbIndexParameters , IdbKeyRange , IdbObjectStore ,
12+ IdbObjectStoreParameters , IdbOpenDbRequest , IdbRequest , IdbTransactionMode ,
1313} ;
1414
1515const LAST_DB_VERSION : u32 = 1 ;
6767 table_name : String ,
6868 db : IdbDatabase ,
6969 _phantom : std:: marker:: PhantomData < T > ,
70+ store : IdbObjectStore ,
7071}
7172
7273#[ allow( dead_code) ]
@@ -174,10 +175,15 @@ impl<T: StoreModel + 'static> IndexeddbTable<T> {
174175 open_req_ref. set_onsuccess ( None ) ;
175176 open_req_ref. set_onerror ( None ) ;
176177
178+ let store = db_result
179+ . transaction_with_str_and_mode ( & table_name, IdbTransactionMode :: Readwrite )
180+ . and_then ( |tx| tx. object_store ( & table_name) ) ?;
181+
177182 Ok ( Box :: new ( IndexeddbTable {
178183 table_name : table_name. to_string ( ) ,
179184 db : db_result,
180185 _phantom : std:: marker:: PhantomData ,
186+ store,
181187 } ) )
182188 }
183189}
@@ -204,13 +210,7 @@ impl<T: StoreModel + 'static> IndexeddbTable<T> {
204210 None => js_sys:: Number :: POSITIVE_INFINITY ,
205211 } ;
206212
207- let store = self
208- . db
209- . transaction_with_str_and_mode ( & self . table_name , IdbTransactionMode :: Readonly )
210- . and_then ( |tx| tx. object_store ( & self . table_name ) )
211- . ok ( ) ?;
212-
213- let index = store. index ( "partition+sortkey" ) . ok ( ) ?;
213+ let index = self . store . index ( "partition+sortkey" ) . ok ( ) ?;
214214 let query_range: IdbKeyRange = web_sys:: IdbKeyRange :: bound (
215215 & js_sys:: Array :: of2 ( & partition. into ( ) , & js_sys:: Number :: NEGATIVE_INFINITY . into ( ) )
216216 . into ( ) ,
@@ -302,13 +302,7 @@ impl<T: StoreModel + 'static> IndexeddbTable<T> {
302302 None => js_sys:: Number :: POSITIVE_INFINITY ,
303303 } ;
304304
305- let store = self
306- . db
307- . transaction_with_str_and_mode ( & self . table_name , IdbTransactionMode :: Readonly )
308- . and_then ( |tx| tx. object_store ( & self . table_name ) )
309- . ok ( ) ?;
310-
311- let index = store. index ( "partition+sortkey" ) . ok ( ) ?;
305+ let index = self . store . index ( "partition+sortkey" ) . ok ( ) ?;
312306 let query_range: IdbKeyRange = web_sys:: IdbKeyRange :: bound (
313307 & js_sys:: Array :: of2 ( & partition. into ( ) , & js_sys:: Number :: NEGATIVE_INFINITY . into ( ) )
314308 . into ( ) ,
@@ -414,16 +408,10 @@ impl<T: StoreModel + 'static> IndexeddbTable<T> {
414408 }
415409
416410 async fn get ( & self , partition : & str , key : & str ) -> Option < T > {
417- let store = self
418- . db
419- . transaction_with_str_and_mode ( & self . table_name , IdbTransactionMode :: Readonly )
420- . and_then ( |tx| tx. object_store ( & self . table_name ) )
421- . ok ( ) ?;
422-
423411 let query_keys = js_sys:: Array :: new ( ) ;
424412 query_keys. push ( & partition. into ( ) ) ;
425413 query_keys. push ( & key. into ( ) ) ;
426- let get_req = store. get ( & query_keys) . ok ( ) ?;
414+ let get_req = self . store . get ( & query_keys) . ok ( ) ?;
427415 let get_req_ref = get_req. clone ( ) ;
428416
429417 let ( done_tx, mut done_rx) = tokio:: sync:: mpsc:: unbounded_channel ( ) ;
@@ -457,46 +445,35 @@ impl<T: StoreModel + 'static> IndexeddbTable<T> {
457445 }
458446
459447 async fn batch_update ( & self , items : & Vec < super :: ValueItem < T > > ) -> crate :: Result < ( ) > {
460- let tx = self
461- . db
462- . transaction_with_str_and_mode ( & self . table_name , IdbTransactionMode :: Readwrite ) ?;
463- let store = tx. object_store ( & self . table_name ) ?;
464-
465448 for item in items {
466- let query_keys = js_sys:: Array :: new ( ) ;
467- query_keys. push ( & item. partition . to_string ( ) . into ( ) ) ;
468- query_keys. push ( & item. key . to_string ( ) . into ( ) ) ;
469449 match item. value . as_ref ( ) {
470450 None => {
471- store. delete ( & query_keys) . ok ( ) ;
451+ let query_keys = js_sys:: Array :: new ( ) ;
452+ query_keys. push ( & item. partition . to_string ( ) . into ( ) ) ;
453+ query_keys. push ( & item. key . to_string ( ) . into ( ) ) ;
454+ self . store . delete ( & query_keys) . ok ( ) ;
472455 }
473456 Some ( v) => {
474- let key = item. key . to_string ( ) ;
475457 let value = StoreValue {
476458 sortkey : v. sort_key ( ) as f64 ,
477459 partition : item. partition . to_string ( ) ,
478- key : key. clone ( ) ,
460+ key : item . key . to_string ( ) ,
479461 value : v. to_string ( ) ,
480462 } ;
481463 let item = serde_wasm_bindgen:: to_value ( & value)
482464 . map_err ( |e| ClientError :: Storage ( e. to_string ( ) ) ) ?;
483- store. put ( & item) . ok ( ) ;
465+ self . store . put ( & item) . ok ( ) ;
484466 }
485467 }
486468 }
487- #[ allow( deprecated) ]
488- tx. commit ( ) . map_err ( Into :: into)
469+ Ok ( ( ) )
489470 }
490471
491472 async fn set ( & self , partition : & str , key : & str , value : Option < & T > ) -> crate :: Result < ( ) > {
492473 let value = match value {
493474 None => return self . remove ( partition, key) . await ,
494475 Some ( v) => v,
495476 } ;
496- let tx = self
497- . db
498- . transaction_with_str_and_mode ( & self . table_name , IdbTransactionMode :: Readwrite ) ?;
499- let store = tx. object_store ( & self . table_name ) ?;
500477
501478 let item = StoreValue {
502479 sortkey : value. sort_key ( ) as f64 ,
@@ -507,24 +484,18 @@ impl<T: StoreModel + 'static> IndexeddbTable<T> {
507484
508485 let item =
509486 serde_wasm_bindgen:: to_value ( & item) . map_err ( |e| ClientError :: Storage ( e. to_string ( ) ) ) ?;
510- store. put ( & item) ?;
511- #[ allow( deprecated) ]
512- tx. commit ( ) . map_err ( Into :: into)
487+ self . store . put ( & item) ?;
488+ Ok ( ( ) )
513489 }
514490
515491 async fn remove ( & self , partition : & str , key : & str ) -> crate :: Result < ( ) > {
516- let tx = self
517- . db
518- . transaction_with_str_and_mode ( & self . table_name , IdbTransactionMode :: Readwrite ) ?;
519- let store = tx. object_store ( & self . table_name ) ?;
520-
521492 let cursor_req = if !key. is_empty ( ) {
522493 let query_keys = js_sys:: Array :: new ( ) ;
523494 query_keys. push ( & partition. into ( ) ) ;
524495 query_keys. push ( & key. into ( ) ) ;
525- store. delete ( & query_keys. into ( ) )
496+ self . store . delete ( & query_keys. into ( ) )
526497 } else {
527- let index = store. index ( "partition+sortkey" ) ?;
498+ let index = self . store . index ( "partition+sortkey" ) ?;
528499 let query_range: IdbKeyRange = web_sys:: IdbKeyRange :: bound (
529500 & js_sys:: Array :: of2 ( & partition. into ( ) , & js_sys:: Number :: NEGATIVE_INFINITY . into ( ) ) ,
530501 & js_sys:: Array :: of2 ( & partition. into ( ) , & js_sys:: Number :: POSITIVE_INFINITY . into ( ) ) ,
@@ -583,18 +554,11 @@ impl<T: StoreModel + 'static> IndexeddbTable<T> {
583554 let _ = done_rx. recv ( ) . await ;
584555 cursor_req_ref. set_onerror ( None ) ;
585556 cursor_req_ref. set_onsuccess ( None ) ;
586- #[ allow( deprecated) ]
587- tx. commit ( ) . map_err ( Into :: into)
557+ Ok ( ( ) )
588558 }
589559
590560 async fn last ( & self , partition : & str ) -> Option < T > {
591- let store = self
592- . db
593- . transaction_with_str_and_mode ( & self . table_name , IdbTransactionMode :: Readonly )
594- . and_then ( |tx| tx. object_store ( & self . table_name ) )
595- . ok ( ) ?;
596-
597- let index = store. index ( "partition+sortkey" ) . ok ( ) ?;
561+ let index = self . store . index ( "partition+sortkey" ) . ok ( ) ?;
598562 let query_range: IdbKeyRange = web_sys:: IdbKeyRange :: bound (
599563 & js_sys:: Array :: of2 ( & partition. into ( ) , & js_sys:: Number :: NEGATIVE_INFINITY . into ( ) ) ,
600564 & js_sys:: Array :: of2 ( & partition. into ( ) , & js_sys:: Number :: POSITIVE_INFINITY . into ( ) ) ,
0 commit comments