Skip to content

Commit c39c2bb

Browse files
committed
feat: reduce store created
1 parent 608ab37 commit c39c2bb

File tree

5 files changed

+47
-86
lines changed

5 files changed

+47
-86
lines changed

crates/restsend/src/storage/indexeddb.rs

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::{borrow::Borrow, cell::RefCell, io::Cursor, rc::Rc, time::Duration, vec
88
use wasm_bindgen::prelude::*;
99
use wasm_bindgen_futures::JsFuture;
1010
use 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

1515
const LAST_DB_VERSION: u32 = 1;
@@ -67,6 +67,7 @@ where
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()),

js/restsend_wasm.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,10 +856,10 @@ export interface InitOutput {
856856
readonly __wbindgen_export_4: WebAssembly.Table;
857857
readonly __wbindgen_export_5: WebAssembly.Table;
858858
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
859-
readonly closure635_externref_shim: (a: number, b: number, c: any) => void;
859+
readonly closure636_externref_shim: (a: number, b: number, c: any) => void;
860860
readonly _dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hb8244aa3255c06dc: (a: number, b: number) => void;
861-
readonly closure778_externref_shim: (a: number, b: number, c: any) => void;
862-
readonly closure819_externref_shim: (a: number, b: number, c: any, d: any) => void;
861+
readonly closure779_externref_shim: (a: number, b: number, c: any) => void;
862+
readonly closure820_externref_shim: (a: number, b: number, c: any, d: any) => void;
863863
readonly __wbindgen_start: () => void;
864864
}
865865

js/restsend_wasm.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -286,19 +286,19 @@ export function logout(endpoint, token) {
286286
}
287287

288288
function __wbg_adapter_54(arg0, arg1, arg2) {
289-
wasm.closure635_externref_shim(arg0, arg1, arg2);
289+
wasm.closure636_externref_shim(arg0, arg1, arg2);
290290
}
291291

292292
function __wbg_adapter_61(arg0, arg1) {
293293
wasm._dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hb8244aa3255c06dc(arg0, arg1);
294294
}
295295

296296
function __wbg_adapter_66(arg0, arg1, arg2) {
297-
wasm.closure778_externref_shim(arg0, arg1, arg2);
297+
wasm.closure779_externref_shim(arg0, arg1, arg2);
298298
}
299299

300-
function __wbg_adapter_445(arg0, arg1, arg2, arg3) {
301-
wasm.closure819_externref_shim(arg0, arg1, arg2, arg3);
300+
function __wbg_adapter_443(arg0, arg1, arg2, arg3) {
301+
wasm.closure820_externref_shim(arg0, arg1, arg2, arg3);
302302
}
303303

304304
const __wbindgen_enum_BinaryType = ["blob", "arraybuffer"];
@@ -1874,9 +1874,6 @@ function __wbg_get_imports() {
18741874
imports.wbg.__wbg_close_5ce03e29be453811 = function() { return handleError(function (arg0) {
18751875
arg0.close();
18761876
}, arguments) };
1877-
imports.wbg.__wbg_commit_ee33ba79d75a9134 = function() { return handleError(function (arg0) {
1878-
arg0.commit();
1879-
}, arguments) };
18801877
imports.wbg.__wbg_continue_c46c11d3dbe1b030 = function() { return handleError(function (arg0) {
18811878
arg0.continue();
18821879
}, arguments) };
@@ -2214,7 +2211,7 @@ function __wbg_get_imports() {
22142211
const a = state0.a;
22152212
state0.a = 0;
22162213
try {
2217-
return __wbg_adapter_445(a, state0.b, arg0, arg1);
2214+
return __wbg_adapter_443(a, state0.b, arg0, arg1);
22182215
} finally {
22192216
state0.a = a;
22202217
}
@@ -2572,28 +2569,28 @@ function __wbg_get_imports() {
25722569
const ret = false;
25732570
return ret;
25742571
};
2575-
imports.wbg.__wbindgen_closure_wrapper1512 = function(arg0, arg1, arg2) {
2576-
const ret = makeMutClosure(arg0, arg1, 636, __wbg_adapter_54);
2572+
imports.wbg.__wbindgen_closure_wrapper1515 = function(arg0, arg1, arg2) {
2573+
const ret = makeMutClosure(arg0, arg1, 637, __wbg_adapter_54);
25772574
return ret;
25782575
};
2579-
imports.wbg.__wbindgen_closure_wrapper1516 = function(arg0, arg1, arg2) {
2580-
const ret = makeMutClosure(arg0, arg1, 636, __wbg_adapter_54);
2576+
imports.wbg.__wbindgen_closure_wrapper1519 = function(arg0, arg1, arg2) {
2577+
const ret = makeMutClosure(arg0, arg1, 637, __wbg_adapter_54);
25812578
return ret;
25822579
};
2583-
imports.wbg.__wbindgen_closure_wrapper1517 = function(arg0, arg1, arg2) {
2584-
const ret = makeMutClosure(arg0, arg1, 636, __wbg_adapter_54);
2580+
imports.wbg.__wbindgen_closure_wrapper1520 = function(arg0, arg1, arg2) {
2581+
const ret = makeMutClosure(arg0, arg1, 637, __wbg_adapter_54);
25852582
return ret;
25862583
};
2587-
imports.wbg.__wbindgen_closure_wrapper1518 = function(arg0, arg1, arg2) {
2588-
const ret = makeMutClosure(arg0, arg1, 636, __wbg_adapter_61);
2584+
imports.wbg.__wbindgen_closure_wrapper1521 = function(arg0, arg1, arg2) {
2585+
const ret = makeMutClosure(arg0, arg1, 637, __wbg_adapter_61);
25892586
return ret;
25902587
};
2591-
imports.wbg.__wbindgen_closure_wrapper1522 = function(arg0, arg1, arg2) {
2592-
const ret = makeMutClosure(arg0, arg1, 636, __wbg_adapter_54);
2588+
imports.wbg.__wbindgen_closure_wrapper1525 = function(arg0, arg1, arg2) {
2589+
const ret = makeMutClosure(arg0, arg1, 637, __wbg_adapter_54);
25932590
return ret;
25942591
};
2595-
imports.wbg.__wbindgen_closure_wrapper2238 = function(arg0, arg1, arg2) {
2596-
const ret = makeMutClosure(arg0, arg1, 779, __wbg_adapter_66);
2592+
imports.wbg.__wbindgen_closure_wrapper2241 = function(arg0, arg1, arg2) {
2593+
const ret = makeMutClosure(arg0, arg1, 780, __wbg_adapter_66);
25972594
return ret;
25982595
};
25992596
imports.wbg.__wbindgen_debug_string = function(arg0, arg1) {

js/restsend_wasm_bg.wasm

-626 Bytes
Binary file not shown.

js/restsend_wasm_bg.wasm.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export const __externref_table_alloc: () => number;
104104
export const __wbindgen_export_4: WebAssembly.Table;
105105
export const __wbindgen_export_5: WebAssembly.Table;
106106
export const __wbindgen_free: (a: number, b: number, c: number) => void;
107-
export const closure635_externref_shim: (a: number, b: number, c: any) => void;
107+
export const closure636_externref_shim: (a: number, b: number, c: any) => void;
108108
export const _dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hb8244aa3255c06dc: (a: number, b: number) => void;
109-
export const closure778_externref_shim: (a: number, b: number, c: any) => void;
110-
export const closure819_externref_shim: (a: number, b: number, c: any, d: any) => void;
109+
export const closure779_externref_shim: (a: number, b: number, c: any) => void;
110+
export const closure820_externref_shim: (a: number, b: number, c: any, d: any) => void;
111111
export const __wbindgen_start: () => void;

0 commit comments

Comments
 (0)