make api consistent with the JS implementation, add examples

pull/1860/head
Lucas Nogueira 8 months ago
parent 083c165543
commit ff9bddd95c
No known key found for this signature in database
GPG Key ID: 7C32FCA95C8C95D7

@ -1 +1 @@
if("__TAURI__"in window){var __TAURI_PLUGIN_STORE__=function(t){"use strict";var e,r;function a(t,e=!1){return window.__TAURI_INTERNALS__.transformCallback(t,e)}async function s(t,e={},r){return window.__TAURI_INTERNALS__.invoke(t,e,r)}"function"==typeof SuppressedError&&SuppressedError;class i{get rid(){return function(t,e,r,a){if("a"===r&&!a)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!a:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?a:"a"===r?a.call(t):a?a.value:e.get(t)}(this,e,"f")}constructor(t){e.set(this,void 0),function(t,e,r,a,s){if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");e.set(t,r)}(this,e,t)}async close(){return s("plugin:resources|close",{rid:this.rid})}}async function n(t,e,r){const i={kind:"Any"};return s("plugin:event|listen",{event:t,target:i,handler:a(e)}).then((e=>async()=>async function(t,e){await s("plugin:event|unlisten",{event:t,eventId:e})}(t,e)))}async function o(t,e){return await c.createOrExistingStore(t,e)}e=new WeakMap,function(t){t.WINDOW_RESIZED="tauri://resize",t.WINDOW_MOVED="tauri://move",t.WINDOW_CLOSE_REQUESTED="tauri://close-requested",t.WINDOW_DESTROYED="tauri://destroyed",t.WINDOW_FOCUS="tauri://focus",t.WINDOW_BLUR="tauri://blur",t.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",t.WINDOW_THEME_CHANGED="tauri://theme-changed",t.WINDOW_CREATED="tauri://window-created",t.WEBVIEW_CREATED="tauri://webview-created",t.DRAG_ENTER="tauri://drag-enter",t.DRAG_OVER="tauri://drag-over",t.DRAG_DROP="tauri://drag-drop",t.DRAG_LEAVE="tauri://drag-leave"}(r||(r={}));class c extends i{constructor(t){super(t)}static async createStore(t,e){const r=await s("plugin:store|create_store",{path:t,...e});return new c(r)}static async createOrLoad(t,e){const r=await s("plugin:store|create_or_load",{path:t,...e});return new c(r)}static async getStore(t){const e=await s("plugin:store|get_store",{path:t});return e?new c(e):void 0}async set(t,e){await s("plugin:store|set",{rid:this.rid,key:t,value:e})}async get(t){const[e,r]=await s("plugin:store|get",{rid:this.rid,key:t});return r?e:void 0}async has(t){return await s("plugin:store|has",{rid:this.rid,key:t})}async delete(t){return await s("plugin:store|delete",{rid:this.rid,key:t})}async clear(){await s("plugin:store|clear",{rid:this.rid})}async reset(){await s("plugin:store|reset",{rid:this.rid})}async keys(){return await s("plugin:store|keys",{rid:this.rid})}async values(){return await s("plugin:store|values",{rid:this.rid})}async entries(){return await s("plugin:store|entries",{rid:this.rid})}async length(){return await s("plugin:store|length",{rid:this.rid})}async load(){await s("plugin:store|load",{rid:this.rid})}async save(){await s("plugin:store|save",{rid:this.rid})}async onKeyChange(t,e){return await n("store://change",(r=>{r.payload.resourceId===this.rid&&r.payload.key===t&&e(r.payload.exists?r.payload.value:void 0)}))}async onChange(t){return await n("store://change",(e=>{e.payload.resourceId===this.rid&&t(e.payload.key,e.payload.exists?e.payload.value:void 0)}))}async close(){await s("plugin:store|close_store",{rid:this.rid})}}return t.LazyStore=class{get store(){return this._store||(this._store=o(this.path,this.options)),this._store}constructor(t,e){this.path=t,this.options=e}async init(){await this.store}async set(t,e){return(await this.store).set(t,e)}async get(t){return(await this.store).get(t)}async has(t){return(await this.store).has(t)}async delete(t){return(await this.store).delete(t)}async clear(){await(await this.store).clear()}async reset(){await(await this.store).reset()}async keys(){return(await this.store).keys()}async values(){return(await this.store).values()}async entries(){return(await this.store).entries()}async length(){return(await this.store).length()}async load(){await(await this.store).load()}async save(){await(await this.store).save()}async onKeyChange(t,e){return(await this.store).onKeyChange(t,e)}async onChange(t){return(await this.store).onChange(t)}async close(){this._store&&await(await this._store).close()}},t.Store=c,t.createOrExistingStore=o,t.createStore=async function(t,e){return await c.createStore(t,e)},t.getStore=async function(t){return await c.getStore(t)},t}({});Object.defineProperty(window.__TAURI__,"store",{value:__TAURI_PLUGIN_STORE__})}
if("__TAURI__"in window){var __TAURI_PLUGIN_STORE__=function(t){"use strict";var e,a;function r(t,e=!1){return window.__TAURI_INTERNALS__.transformCallback(t,e)}async function s(t,e={},a){return window.__TAURI_INTERNALS__.invoke(t,e,a)}"function"==typeof SuppressedError&&SuppressedError;class i{get rid(){return function(t,e,a,r){if("a"===a&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===a?r:"a"===a?r.call(t):r?r.value:e.get(t)}(this,e,"f")}constructor(t){e.set(this,void 0),function(t,e,a,r,s){if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");e.set(t,a)}(this,e,t)}async close(){return s("plugin:resources|close",{rid:this.rid})}}async function n(t,e,a){const i={kind:"Any"};return s("plugin:event|listen",{event:t,target:i,handler:r(e)}).then((e=>async()=>async function(t,e){await s("plugin:event|unlisten",{event:t,eventId:e})}(t,e)))}async function o(t,e){return await c.createOrLoad(t,e)}e=new WeakMap,function(t){t.WINDOW_RESIZED="tauri://resize",t.WINDOW_MOVED="tauri://move",t.WINDOW_CLOSE_REQUESTED="tauri://close-requested",t.WINDOW_DESTROYED="tauri://destroyed",t.WINDOW_FOCUS="tauri://focus",t.WINDOW_BLUR="tauri://blur",t.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",t.WINDOW_THEME_CHANGED="tauri://theme-changed",t.WINDOW_CREATED="tauri://window-created",t.WEBVIEW_CREATED="tauri://webview-created",t.DRAG_ENTER="tauri://drag-enter",t.DRAG_OVER="tauri://drag-over",t.DRAG_DROP="tauri://drag-drop",t.DRAG_LEAVE="tauri://drag-leave"}(a||(a={}));class c extends i{constructor(t){super(t)}static async create(t,e){const a=await s("plugin:store|create_store",{path:t,...e});return new c(a)}static async createOrLoad(t,e){const a=await s("plugin:store|create_or_load",{path:t,...e});return new c(a)}static async get(t){return await s("plugin:store|get_store",{path:t}).then((t=>t?new c(t):null))}async set(t,e){await s("plugin:store|set",{rid:this.rid,key:t,value:e})}async get(t){const[e,a]=await s("plugin:store|get",{rid:this.rid,key:t});return a?e:void 0}async has(t){return await s("plugin:store|has",{rid:this.rid,key:t})}async delete(t){return await s("plugin:store|delete",{rid:this.rid,key:t})}async clear(){await s("plugin:store|clear",{rid:this.rid})}async reset(){await s("plugin:store|reset",{rid:this.rid})}async keys(){return await s("plugin:store|keys",{rid:this.rid})}async values(){return await s("plugin:store|values",{rid:this.rid})}async entries(){return await s("plugin:store|entries",{rid:this.rid})}async length(){return await s("plugin:store|length",{rid:this.rid})}async load(){await s("plugin:store|load",{rid:this.rid})}async save(){await s("plugin:store|save",{rid:this.rid})}async onKeyChange(t,e){return await n("store://change",(a=>{a.payload.resourceId===this.rid&&a.payload.key===t&&e(a.payload.exists?a.payload.value:void 0)}))}async onChange(t){return await n("store://change",(e=>{e.payload.resourceId===this.rid&&t(e.payload.key,e.payload.exists?e.payload.value:void 0)}))}async close(){await s("plugin:store|close_store",{rid:this.rid})}}return t.LazyStore=class{get store(){return this._store||(this._store=o(this.path,this.options)),this._store}constructor(t,e){this.path=t,this.options=e}async init(){await this.store}async set(t,e){return(await this.store).set(t,e)}async get(t){return(await this.store).get(t)}async has(t){return(await this.store).has(t)}async delete(t){return(await this.store).delete(t)}async clear(){await(await this.store).clear()}async reset(){await(await this.store).reset()}async keys(){return(await this.store).keys()}async values(){return(await this.store).values()}async entries(){return(await this.store).entries()}async length(){return(await this.store).length()}async load(){await(await this.store).load()}async save(){await(await this.store).save()}async onKeyChange(t,e){return(await this.store).onKeyChange(t,e)}async onChange(t){return(await this.store).onChange(t)}async close(){this._store&&await(await this._store).close()}},t.Store=c,t.create=async function(t,e){return await c.create(t,e)},t.createOrLoad=o,t.getStore=async function(t){return await c.get(t)},t}({});Object.defineProperty(window.__TAURI__,"store",{value:__TAURI_PLUGIN_STORE__})}

@ -38,31 +38,31 @@ export type StoreOptions = {
*
* @throws If a store at that path already exists
*/
export async function createStore(
export async function create(
path: string,
options?: StoreOptions
): Promise<Store> {
return await Store.createStore(path, options)
return await Store.create(path, options)
}
/**
* Create a new Store or get the existing store with the path
* Create a new Store or load the existing store with the path
*
* @param path Path to save the store in `app_data_dir`
* @param options Store configuration options
*/
export async function createOrExistingStore(
export async function createOrLoad(
path: string,
options?: StoreOptions
): Promise<Store> {
return await Store.createOrExistingStore(path, options)
return await Store.createOrLoad(path, options)
}
/**
* @param path Path of the store in the rust side
* @param path Path of the store
*/
export async function getStore(path: string): Promise<Store | undefined> {
return await Store.getStore(path)
export async function getStore(path: string): Promise<Store | null> {
return await Store.get(path)
}
/**
@ -73,7 +73,7 @@ export class LazyStore implements IStore {
private get store(): Promise<Store> {
if (!this._store) {
this._store = createOrExistingStore(this.path, this.options)
this._store = createOrLoad(this.path, this.options)
}
return this._store
}
@ -172,15 +172,24 @@ export class Store extends Resource implements IStore {
}
/**
* Create a new store.
*
* If the store already exists, its data will be overwritten.
*
* If the store is already loaded you must use {@link getStore} instead.
*
* @example
* ```typescript
* import { Store } from '@tauri-apps/api/store';
* const store = await Store.create('store.json');
* ```
*
* @param path Path to save the store in `app_data_dir`
* @param options Store configuration options
*
* @throws If a store at that path already exists
*/
static async createStore(
path: string,
options?: StoreOptions
): Promise<Store> {
static async create(path: string, options?: StoreOptions): Promise<Store> {
const rid = await invoke<number>('plugin:store|create_store', {
path,
...options
@ -189,7 +198,18 @@ export class Store extends Resource implements IStore {
}
/**
* Create a new Store or get the existing store with the path
* Create a new Store or load the existing store with the path.
*
* If the store is already loaded you must use {@link getStore} instead.
*
* @example
* ```typescript
* import { Store } from '@tauri-apps/api/store';
* let store = await Store.get('store.json');
* if (!store) {
* store = await Store.createOrLoad('store.json');
* }
* ```
*
* @param path Path to save the store in `app_data_dir`
* @param options Store configuration options
@ -206,11 +226,26 @@ export class Store extends Resource implements IStore {
}
/**
* @param path Path of the store in the rust side
* Gets an already loaded store.
*
* If the store is not loaded, returns `null`. In this case,
* you must either {@link Store.create | create} it or {@link Store.createOrLoad createOrLoad} it.
*
* @example
* ```typescript
* import { Store } from '@tauri-apps/api/store';
* let store = await Store.get('store.json');
* if (!store) {
* store = await Store.createOrLoad('store.json');
* }
* ```
*
* @param path Path of the store.
*/
static async getStore(path: string): Promise<Store | undefined> {
const rid = await invoke<number | null>('plugin:store|get_store', { path })
return rid ? new Store(rid) : undefined
static async get(path: string): Promise<Store | null> {
return await invoke<number | null>('plugin:store|get_store', { path }).then(
(rid) => (rid ? new Store(rid) : null)
)
}
async set(key: string, value: unknown): Promise<void> {

@ -112,7 +112,7 @@ async fn create_store<R: Runtime>(
serialize_fn_name,
deserialize_fn_name,
)?;
let (_, rid) = builder.build_inner()?;
let (_, rid) = builder.create_inner()?;
Ok(rid)
}
@ -133,7 +133,7 @@ async fn create_or_load<R: Runtime>(
serialize_fn_name,
deserialize_fn_name,
)?;
let (_, rid) = builder.build_or_existing_inner()?;
let (_, rid) = builder.create_or_load_inner()?;
Ok(rid)
}
@ -242,23 +242,95 @@ async fn save<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
}
pub trait StoreExt<R: Runtime> {
/// Create a store or get an existing store with default settings at path
/// Create a store or load an existing store with default settings at the given path.
///
/// If the store is already loaded, its instance is automatically returned.
///
/// # Examples
///
/// ```
/// use tauri_plugin_store::StoreExt;
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = app.store("my-store")?;
/// Ok(())
/// });
/// ```
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
/// Create a store with default settings
/// Create a store with default settings.
///
/// If the store is already loaded you must check with [`Self::get_store`] or prefer [`Self::store`]
/// as it will return `Err(Error::AlreadyExists)`.
///
/// # Examples
///
/// ```
/// use tauri_plugin_store::StoreExt;
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = app.create_store("my-store")?;
/// Ok(())
/// });
/// ```
fn create_store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
/// Get a store builder
/// Get a store builder.
///
/// The builder can be used to configure the store.
/// To use the default settings see [`Self::store`].
///
/// # Examples
///
/// ```
/// use tauri_plugin_store::StoreExt;
/// use std::time::Duration;
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = app.store_builder("users.json").auto_save(Duration::from_secs(1)).create_or_load()?;
/// Ok(())
/// });
/// ```
fn store_builder(&self, path: impl AsRef<Path>) -> StoreBuilder<R>;
/// Get an existing store
/// Get a handle of an already loaded store.
///
/// If the store is not loaded or does not exist, it returns `None`.
/// In this case, you should initialize it with [`Self::store`].
///
/// # Examples
///
/// ```
/// use tauri_plugin_store::StoreExt;
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = if let Some(s) = app.get_store("store.json") {
/// s
/// } else {
/// app.store("store.json")?
/// };
/// Ok(())
/// });
/// ```
fn get_store(&self, path: impl AsRef<Path>) -> Option<Arc<Store<R>>>;
}
impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>> {
StoreBuilder::new(self.app_handle(), path).build_or_existing()
let path = path.as_ref();
if let Some(store) = self.get_store(path) {
return Ok(store);
}
StoreBuilder::new(self.app_handle(), path).create_or_load()
}
fn create_store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>> {
StoreBuilder::new(self.app_handle(), path).build()
StoreBuilder::new(self.app_handle(), path).create()
}
fn store_builder(&self, path: impl AsRef<Path>) -> StoreBuilder<R> {
@ -327,7 +399,7 @@ impl<R: Runtime> Builder<R> {
/// tauri_plugin_store::Builder::default()
/// .register_serialize_fn("no-pretty-json".to_owned(), no_pretty_json)
/// .build(),
/// )
/// );
/// ```
pub fn register_serialize_fn(mut self, name: String, serialize_fn: SerializeFn) -> Self {
self.serialize_fns.insert(name, serialize_fn);
@ -356,7 +428,7 @@ impl<R: Runtime> Builder<R> {
/// tauri_plugin_store::Builder::default()
/// .default_serialize_fn(no_pretty_json)
/// .build(),
/// )
/// );
/// ```
pub fn default_serialize_fn(mut self, serialize_fn: SerializeFn) -> Self {
self.default_serialize = serialize_fn;
@ -377,7 +449,7 @@ impl<R: Runtime> Builder<R> {
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").build()?;
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").create_or_load()?;
/// Ok(())
/// });
/// ```

@ -38,7 +38,6 @@ pub struct StoreBuilder<R: Runtime> {
serialize_fn: SerializeFn,
deserialize_fn: DeserializeFn,
auto_save: Option<Duration>,
load_on_build: bool,
}
impl<R: Runtime> StoreBuilder<R> {
@ -65,7 +64,6 @@ impl<R: Runtime> StoreBuilder<R> {
serialize_fn,
deserialize_fn,
auto_save: Some(Duration::from_millis(100)),
load_on_build: true,
}
}
@ -81,7 +79,7 @@ impl<R: Runtime> StoreBuilder<R> {
///
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// .defaults(defaults)
/// .build()?;
/// .create_or_load()?;
/// Ok(())
/// });
/// ```
@ -99,7 +97,7 @@ impl<R: Runtime> StoreBuilder<R> {
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// .default("foo".to_string(), "bar")
/// .build()?;
/// .create_or_load()?;
/// Ok(())
/// });
/// ```
@ -121,7 +119,7 @@ impl<R: Runtime> StoreBuilder<R> {
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .serialize(|cache| serde_json::to_vec(&cache).map_err(Into::into))
/// .build()?;
/// .create_or_load()?;
/// Ok(())
/// });
/// ```
@ -139,7 +137,7 @@ impl<R: Runtime> StoreBuilder<R> {
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .deserialize(|bytes| serde_json::from_slice(&bytes).map_err(Into::into))
/// .build()?;
/// .create_or_load()?;
/// Ok(())
/// });
/// ```
@ -157,7 +155,7 @@ impl<R: Runtime> StoreBuilder<R> {
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .auto_save(std::time::Duration::from_millis(100))
/// .build()?;
/// .create_or_load()?;
/// Ok(())
/// });
/// ```
@ -172,13 +170,7 @@ impl<R: Runtime> StoreBuilder<R> {
self
}
/// Skip loading the store on build
pub fn skip_initial_load(mut self) -> Self {
self.load_on_build = false;
self
}
pub(crate) fn build_inner(mut self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
pub(crate) fn build_inner(mut self, load: bool) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
let state = self.app.state::<StoreState>();
let mut stores = state.stores.lock().unwrap();
@ -193,7 +185,7 @@ impl<R: Runtime> StoreBuilder<R> {
self.serialize_fn,
self.deserialize_fn,
);
if self.load_on_build {
if load {
let _ = store_inner.load();
}
@ -210,17 +202,6 @@ impl<R: Runtime> StoreBuilder<R> {
Ok((store, rid))
}
pub(crate) fn build_or_existing_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
{
let state = self.app.state::<StoreState>();
let stores = state.stores.lock().unwrap();
if let Some(rid) = stores.get(&self.path) {
return Ok((self.app.resources_table().get(*rid).unwrap(), *rid));
}
}
self.build_inner()
}
/// Builds the [`Store`], also see [`build_or_existing`](Self::build_or_existing).
///
/// This loads the store from disk and put the store in the app's resource table,
@ -236,30 +217,38 @@ impl<R: Runtime> StoreBuilder<R> {
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").build()?;
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").create_or_load()?;
/// Ok(())
/// });
/// ```
pub fn build(self) -> crate::Result<Arc<Store<R>>> {
let (store, _) = self.build_inner()?;
pub fn create(self) -> crate::Result<Arc<Store<R>>> {
let (store, _) = self.create_inner()?;
Ok(store)
}
/// Get the existing store with the same path or builds a new [`Store`], also see [`build`](Self::build).
pub(crate) fn create_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
self.build_inner(false)
}
/// Get the existing store with the same path or creates a new [`Store`], also see [`create`](Self::create).
///
/// # Examples
/// ```
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").build_or_existing();
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").create_or_load();
/// Ok(())
/// });
/// ```
pub fn build_or_existing(self) -> crate::Result<Arc<Store<R>>> {
let (store, _) = self.build_or_existing_inner()?;
pub fn create_or_load(self) -> crate::Result<Arc<Store<R>>> {
let (store, _) = self.create_or_load_inner()?;
Ok(store)
}
pub(crate) fn create_or_load_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
self.build_inner(true)
}
}
enum AutoSaveMessage {

Loading…
Cancel
Save