Skip to content

Commit 5ef6cac

Browse files
authored
Merge pull request #4760 from Officeyutong/update-wasm-peer-store
peer-store, wasm: use `path` as database name in wasm peer-store implementation
2 parents bb492c8 + b428fbc commit 5ef6cac

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

network/src/peer_store/browser.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ pub async fn get_db<P: AsRef<Path>>(path: P) -> &'static Storage {
5959
DB.get_or_init(|| Storage::new(path)).await
6060
}
6161

62+
const STORE_NAME: &str = "main-store";
63+
6264
#[derive(Clone)]
6365
pub struct Storage {
6466
chan: tokio::sync::mpsc::Sender<Request>,
@@ -67,15 +69,14 @@ pub struct Storage {
6769
impl Storage {
6870
pub async fn new<P: AsRef<Path>>(path: P) -> Self {
6971
let factory = Factory::new().unwrap();
70-
let mut open_request = factory.open("network", Some(1)).unwrap();
71-
let store_name = path.as_ref().to_str().unwrap().to_owned();
72-
let store_name_clone = store_name.clone();
72+
let database_name = path.as_ref().to_str().unwrap().to_owned();
73+
let mut open_request = factory.open(&database_name, Some(1)).unwrap();
7374
open_request.on_upgrade_needed(move |event| {
7475
let database = event.database().unwrap();
7576
let store_params = ObjectStoreParams::new();
7677

7778
let store = database
78-
.create_object_store(&store_name_clone, store_params)
79+
.create_object_store(STORE_NAME, store_params)
7980
.unwrap();
8081
let mut index_params = IndexParams::new();
8182
index_params.unique(true);
@@ -92,9 +93,9 @@ impl Storage {
9293
match request.cmd {
9394
CommandRequest::Read { key } => {
9495
let tran = db
95-
.transaction(&[&store_name], TransactionMode::ReadOnly)
96+
.transaction(&[STORE_NAME], TransactionMode::ReadOnly)
9697
.unwrap();
97-
let store = tran.object_store(&store_name).unwrap();
98+
let store = tran.object_store(STORE_NAME).unwrap();
9899
let key = serde_wasm_bindgen::to_value(&key).unwrap();
99100
let value = store
100101
.get(key)
@@ -107,9 +108,9 @@ impl Storage {
107108
}
108109
CommandRequest::Put { kv } => {
109110
let tran = db
110-
.transaction(&[&store_name], TransactionMode::ReadWrite)
111+
.transaction(&[STORE_NAME], TransactionMode::ReadWrite)
111112
.unwrap();
112-
let store = tran.object_store(&store_name).unwrap();
113+
let store = tran.object_store(STORE_NAME).unwrap();
113114

114115
let key = serde_wasm_bindgen::to_value(&kv.key).unwrap();
115116
let value = serde_wasm_bindgen::to_value(&kv).unwrap();

0 commit comments

Comments
 (0)