@@ -75,19 +75,22 @@ Install the store plugin to get started.
75
75
<Tabs >
76
76
<TabItem label = " JavaScript" >
77
77
78
- ``` javascript
79
- import { Store } from ' @tauri-apps/plugin-store' ;
78
+ ``` typescript
79
+ import { createStore } from ' @tauri-apps/plugin-store' ;
80
80
// when using `"withGlobalTauri": true`, you may use
81
- // const { Store } = window.__TAURI__.store;
81
+ // const { createStore } = window.__TAURI__.store;
82
82
83
- // Store will be loaded automatically when used in JavaScript binding.
84
- const store = new Store (' store.bin' );
83
+ // create a new store or load the existing one
84
+ const store = await createStore (' store.bin' , {
85
+ // we can save automatically after each store modification
86
+ autoSave: true ,
87
+ });
85
88
86
89
// Set a value.
87
90
await store .set (' some-key' , { value: 5 });
88
91
89
92
// Get a value.
90
- const val = await store .get (' some-key' );
93
+ const val = await store .get <{ value : number }> (' some-key' );
91
94
console .log (val ); // { value: 5 }
92
95
93
96
// You can manually save the store after making changes.
@@ -100,32 +103,28 @@ await store.save();
100
103
101
104
``` rust title="src-tauri/src/lib.rs"
102
105
use tauri :: Wry ;
103
- use tauri_plugin_store :: {with_store, StoreCollection } ;
106
+ use tauri_plugin_store :: StoreExt ;
104
107
use serde_json :: json;
105
108
106
109
#[cfg_attr(mobile, tauri:: mobile_entry_point)]
107
110
pub fn run () {
108
111
tauri :: Builder :: default ()
109
112
. plugin (tauri_plugin_store :: Builder :: default (). build ())
110
113
. setup (| app | {
111
- let stores = app . handle (). try_state :: <StoreCollection <Wry >>(). ok_or (" Store not found" )? ;
112
- let path = PathBuf :: from (" store.bin" );
114
+ let store = app . handle (). store_builder (" store.bin" ). build ();
113
115
114
- with_store (app . handle (). clone (), stores , path , | store | {
115
- // Note that values must be serde_json::Value instances,
116
- // otherwise, they will not be compatible with the JavaScript bindings.
117
- store . insert (" some-key" . to_string (), json! ({ " value" : 5 }))? ;
116
+ // Note that values must be serde_json::Value instances,
117
+ // otherwise, they will not be compatible with the JavaScript bindings.
118
+ store . insert (" some-key" , json! ({ " value" : 5 }))? ;
118
119
119
- // Get a value from the store.
120
- let value = store . get (" some-key" ). expect (" Failed to get value from store" );
121
- println! (" {}" , value ); // {"value":5}
120
+ // Get a value from the store.
121
+ let value = store . get (" some-key" ). expect (" Failed to get value from store" );
122
+ println! (" {}" , value ); // {"value":5}
122
123
123
- // You can manually save the store after making changes.
124
- // Otherwise, it will save upon graceful exit as described above.
125
- store . save ()? ;
124
+ // You can manually save the store after making changes.
125
+ // Otherwise, it will save upon graceful exit as described above.
126
+ store . save ()? ;
126
127
127
- Ok (())
128
- });
129
128
130
129
Ok (())
131
130
})
0 commit comments