mirror of
https://github.com/tauri-apps/tauri-plugin-store.git
synced 2026-01-31 00:35:18 +01:00
Rename dev branch to v1 and next branch to v2
Committed via a GitHub action: https://github.com/tauri-apps/plugins-workspace/actions/runs/4869194441 Co-authored-by: FabianLars <FabianLars@users.noreply.github.com>
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules
|
||||
17
Cargo.toml
Normal file
17
Cargo.toml
Normal file
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "tauri-plugin-store"
|
||||
version = "0.0.0"
|
||||
description = "Simple, persistent key-value store."
|
||||
authors.workspace = true
|
||||
license.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
tauri.workspace = true
|
||||
log.workspace = true
|
||||
thiserror.workspace = true
|
||||
20
LICENSE.spdx
Normal file
20
LICENSE.spdx
Normal file
@@ -0,0 +1,20 @@
|
||||
SPDXVersion: SPDX-2.1
|
||||
DataLicense: CC0-1.0
|
||||
PackageName: tauri
|
||||
DataFormat: SPDXRef-1
|
||||
PackageSupplier: Organization: The Tauri Programme in the Commons Conservancy
|
||||
PackageHomePage: https://tauri.app
|
||||
PackageLicenseDeclared: Apache-2.0
|
||||
PackageLicenseDeclared: MIT
|
||||
PackageCopyrightText: 2019-2022, The Tauri Programme in the Commons Conservancy
|
||||
PackageSummary: <text>Tauri is a rust project that enables developers to make secure
|
||||
and small desktop applications using a web frontend.
|
||||
</text>
|
||||
PackageComment: <text>The package includes the following libraries; see
|
||||
Relationship information.
|
||||
</text>
|
||||
Created: 2019-05-20T09:00:00Z
|
||||
PackageDownloadLocation: git://github.com/tauri-apps/tauri
|
||||
PackageDownloadLocation: git+https://github.com/tauri-apps/tauri.git
|
||||
PackageDownloadLocation: git+ssh://github.com/tauri-apps/tauri.git
|
||||
Creator: Person: Daniel Thompson-Yvetot
|
||||
177
LICENSE_APACHE-2.0
Normal file
177
LICENSE_APACHE-2.0
Normal file
@@ -0,0 +1,177 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
21
LICENSE_MIT
Normal file
21
LICENSE_MIT
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 - Present Tauri Apps Contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
114
README.md
Normal file
114
README.md
Normal file
@@ -0,0 +1,114 @@
|
||||

|
||||
|
||||
Simple, persistent key-value store.
|
||||
|
||||
## Install
|
||||
|
||||
_This plugin requires a Rust version of at least **1.64**_
|
||||
|
||||
There are three general methods of installation that we can recommend.
|
||||
|
||||
1. Use crates.io and npm (easiest, and requires you to trust that our publishing pipeline worked)
|
||||
2. Pull sources directly from Github using git tags / revision hashes (most secure)
|
||||
3. Git submodule install this repo in your tauri project and then use file protocol to ingest the source (most secure, but inconvenient to use)
|
||||
|
||||
Install the Core plugin by adding the following to your `Cargo.toml` file:
|
||||
|
||||
`src-tauri/Cargo.toml`
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
|
||||
```
|
||||
|
||||
You can install the JavaScript Guest bindings using your preferred JavaScript package manager:
|
||||
|
||||
> Note: Since most JavaScript package managers are unable to install packages from git monorepos we provide read-only mirrors of each plugin. This makes installation option 2 more ergonomic to use.
|
||||
|
||||
```sh
|
||||
pnpm add https://github.com/tauri-apps/tauri-plugin-store
|
||||
# or
|
||||
npm add https://github.com/tauri-apps/tauri-plugin-store
|
||||
# or
|
||||
yarn add https://github.com/tauri-apps/tauri-plugin-store
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
First you need to register the core plugin with Tauri:
|
||||
|
||||
`src-tauri/src/main.rs`
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_store::Builder::default().build())
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
```
|
||||
|
||||
Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
|
||||
|
||||
```javascript
|
||||
import { Store } from "tauri-plugin-store-api";
|
||||
|
||||
const store = new Store(".settings.dat");
|
||||
|
||||
await store.set("some-key", { value: 5 });
|
||||
|
||||
const val = await store.get("some-key");
|
||||
assert(val, { value: 5 });
|
||||
|
||||
await store.save(); // this manually saves the store, otherwise the store is only saved when your app is closed
|
||||
```
|
||||
|
||||
### Persisting values
|
||||
|
||||
Values added to the store are not persisted between application loads unless:
|
||||
|
||||
1. The application is closed gracefully (plugin automatically saves)
|
||||
2. The store is manually saved (using `store.save()`)
|
||||
|
||||
## Usage from Rust
|
||||
|
||||
You can also access Stores from Rust, you can create new stores:
|
||||
|
||||
```rust
|
||||
use tauri_plugin_store::StoreBuilder;
|
||||
use serde_json::json;
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_store::Builder::default().build())
|
||||
.setup(|app| {
|
||||
let mut store = StoreBuilder::new(app.handle(), "path/to/store.bin".parse()?).build();
|
||||
|
||||
store.insert("a".to_string(), json!("b")) // note that values must be serd_json::Value to be compatible with JS
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
```
|
||||
|
||||
As you may have noticed, the Store crated above isn't accessible to the frontend. To interoperate with stores created by JS use the exported `with_store` method:
|
||||
|
||||
```rust
|
||||
use tauri::Wry;
|
||||
use tauri_plugin_store::with_store;
|
||||
|
||||
let stores = app.state::<StoreCollection<Wry>>();
|
||||
let path = PathBuf::from("path/to/the/storefile");
|
||||
|
||||
with_store(app_handle, stores, path, |store| store.insert("a".to_string(), json!("b")))
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
PRs accepted. Please make sure to read the Contributing Guide before making a pull request.
|
||||
|
||||
## License
|
||||
|
||||
Code: (c) 2015 - Present - The Tauri Programme within The Commons Conservancy.
|
||||
|
||||
MIT or MIT/Apache 2.0 where applicable.
|
||||
BIN
banner.png
Normal file
BIN
banner.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
105
dist-js/index.d.ts
vendored
Normal file
105
dist-js/index.d.ts
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
import { UnlistenFn } from "@tauri-apps/api/event";
|
||||
/**
|
||||
* A key-value store persisted by the backend layer.
|
||||
*/
|
||||
export declare class Store {
|
||||
path: string;
|
||||
constructor(path: string);
|
||||
/**
|
||||
* Inserts a key-value pair into the store.
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @returns
|
||||
*/
|
||||
set(key: string, value: unknown): Promise<void>;
|
||||
/**
|
||||
* Returns the value for the given `key` or `null` the key does not exist.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
get<T>(key: string): Promise<T | null>;
|
||||
/**
|
||||
* Returns `true` if the given `key` exists in the store.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
has(key: string): Promise<boolean>;
|
||||
/**
|
||||
* Removes a key-value pair from the store.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
delete(key: string): Promise<boolean>;
|
||||
/**
|
||||
* Clears the store, removing all key-value pairs.
|
||||
*
|
||||
* Note: To clear the storage and reset it to it's `default` value, use `reset` instead.
|
||||
* @returns
|
||||
*/
|
||||
clear(): Promise<void>;
|
||||
/**
|
||||
* Resets the store to it's `default` value.
|
||||
*
|
||||
* If no default value has been set, this method behaves identical to `clear`.
|
||||
* @returns
|
||||
*/
|
||||
reset(): Promise<void>;
|
||||
/**
|
||||
* Returns a list of all key in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
keys(): Promise<string[]>;
|
||||
/**
|
||||
* Returns a list of all values in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
values<T>(): Promise<T[]>;
|
||||
/**
|
||||
* Returns a list of all entries in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
entries<T>(): Promise<Array<[key: string, value: T]>>;
|
||||
/**
|
||||
* Returns the number of key-value pairs in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
length(): Promise<number>;
|
||||
/**
|
||||
* Attempts to load the on-disk state at the stores `path` into memory.
|
||||
*
|
||||
* This method is useful if the on-disk state was edited by the user and you want to synchronize the changes.
|
||||
*
|
||||
* Note: This method does not emit change events.
|
||||
* @returns
|
||||
*/
|
||||
load(): Promise<void>;
|
||||
/**
|
||||
* Saves the store to disk at the stores `path`.
|
||||
*
|
||||
* As the store is only persisted to disk before the apps exit, changes might be lost in a crash.
|
||||
* This method lets you persist the store to disk whenever you deem necessary.
|
||||
* @returns
|
||||
*/
|
||||
save(): Promise<void>;
|
||||
/**
|
||||
* Listen to changes on a store key.
|
||||
* @param key
|
||||
* @param cb
|
||||
* @returns A promise resolving to a function to unlisten to the event.
|
||||
*/
|
||||
onKeyChange<T>(key: string, cb: (value: T | null) => void): Promise<UnlistenFn>;
|
||||
/**
|
||||
* Listen to changes on the store.
|
||||
* @param cb
|
||||
* @returns A promise resolving to a function to unlisten to the event.
|
||||
*/
|
||||
onChange<T>(cb: (key: string, value: T | null) => void): Promise<UnlistenFn>;
|
||||
}
|
||||
182
dist-js/index.min.js
vendored
Normal file
182
dist-js/index.min.js
vendored
Normal file
@@ -0,0 +1,182 @@
|
||||
var d=Object.defineProperty;var e=(c,a)=>{for(var b in a)d(c,b,{get:a[b],enumerable:!0});};
|
||||
|
||||
var f={};e(f,{convertFileSrc:()=>w,invoke:()=>c$1,transformCallback:()=>s$1});function u$1(){return window.crypto.getRandomValues(new Uint32Array(1))[0]}function s$1(e,r=!1){let n=u$1(),t=`_${n}`;return Object.defineProperty(window,t,{value:o=>(r&&Reflect.deleteProperty(window,t),e==null?void 0:e(o)),writable:!1,configurable:!0}),n}async function c$1(e,r={}){return new Promise((n,t)=>{let o=s$1(i=>{n(i),Reflect.deleteProperty(window,`_${a}`);},!0),a=s$1(i=>{t(i),Reflect.deleteProperty(window,`_${o}`);},!0);window.__TAURI_IPC__({cmd:e,callback:o,error:a,...r});})}function w(e,r="asset"){let n=encodeURIComponent(e);return navigator.userAgent.includes("Windows")?`https://${r}.localhost/${n}`:`${r}://localhost/${n}`}
|
||||
|
||||
async function a(i){return c$1("tauri",i)}
|
||||
|
||||
var W={};e(W,{TauriEvent:()=>c,emit:()=>D,listen:()=>E,once:()=>_});async function s(n,t){return a({__tauriModule:"Event",message:{cmd:"unlisten",event:n,eventId:t}})}async function m(n,t,i){await a({__tauriModule:"Event",message:{cmd:"emit",event:n,windowLabel:t,payload:i}});}async function o(n,t,i){return a({__tauriModule:"Event",message:{cmd:"listen",event:n,windowLabel:t,handler:s$1(i)}}).then(r=>async()=>s(n,r))}async function u(n,t,i){return o(n,t,r=>{i(r),s(n,r.id).catch(()=>{});})}var c=(e=>(e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_CREATED="tauri://window-created",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WINDOW_FILE_DROP="tauri://file-drop",e.WINDOW_FILE_DROP_HOVER="tauri://file-drop-hover",e.WINDOW_FILE_DROP_CANCELLED="tauri://file-drop-cancelled",e.MENU="tauri://menu",e.CHECK_UPDATE="tauri://update",e.UPDATE_AVAILABLE="tauri://update-available",e.INSTALL_UPDATE="tauri://update-install",e.STATUS_UPDATE="tauri://update-status",e.DOWNLOAD_PROGRESS="tauri://update-download-progress",e))(c||{});async function E(n,t){return o(n,null,t)}async function _(n,t){return u(n,null,t)}async function D(n,t){return m(n,void 0,t)}
|
||||
|
||||
// Copyright 2021 Tauri Programme within The Commons Conservancy
|
||||
/**
|
||||
* A key-value store persisted by the backend layer.
|
||||
*/
|
||||
class Store {
|
||||
constructor(path) {
|
||||
this.path = path;
|
||||
}
|
||||
/**
|
||||
* Inserts a key-value pair into the store.
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @returns
|
||||
*/
|
||||
async set(key, value) {
|
||||
return await c$1("plugin:store|set", {
|
||||
path: this.path,
|
||||
key,
|
||||
value,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns the value for the given `key` or `null` the key does not exist.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
async get(key) {
|
||||
return await c$1("plugin:store|get", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns `true` if the given `key` exists in the store.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
async has(key) {
|
||||
return await c$1("plugin:store|has", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Removes a key-value pair from the store.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
async delete(key) {
|
||||
return await c$1("plugin:store|delete", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Clears the store, removing all key-value pairs.
|
||||
*
|
||||
* Note: To clear the storage and reset it to it's `default` value, use `reset` instead.
|
||||
* @returns
|
||||
*/
|
||||
async clear() {
|
||||
return await c$1("plugin:store|clear", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Resets the store to it's `default` value.
|
||||
*
|
||||
* If no default value has been set, this method behaves identical to `clear`.
|
||||
* @returns
|
||||
*/
|
||||
async reset() {
|
||||
return await c$1("plugin:store|reset", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns a list of all key in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async keys() {
|
||||
return await c$1("plugin:store|keys", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns a list of all values in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async values() {
|
||||
return await c$1("plugin:store|values", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns a list of all entries in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async entries() {
|
||||
return await c$1("plugin:store|entries", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns the number of key-value pairs in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async length() {
|
||||
return await c$1("plugin:store|length", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Attempts to load the on-disk state at the stores `path` into memory.
|
||||
*
|
||||
* This method is useful if the on-disk state was edited by the user and you want to synchronize the changes.
|
||||
*
|
||||
* Note: This method does not emit change events.
|
||||
* @returns
|
||||
*/
|
||||
async load() {
|
||||
return await c$1("plugin:store|load", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Saves the store to disk at the stores `path`.
|
||||
*
|
||||
* As the store is only persisted to disk before the apps exit, changes might be lost in a crash.
|
||||
* This method lets you persist the store to disk whenever you deem necessary.
|
||||
* @returns
|
||||
*/
|
||||
async save() {
|
||||
return await c$1("plugin:store|save", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Listen to changes on a store key.
|
||||
* @param key
|
||||
* @param cb
|
||||
* @returns A promise resolving to a function to unlisten to the event.
|
||||
*/
|
||||
async onKeyChange(key, cb) {
|
||||
return await E("store://change", (event) => {
|
||||
if (event.payload.path === this.path && event.payload.key === key) {
|
||||
cb(event.payload.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Listen to changes on the store.
|
||||
* @param cb
|
||||
* @returns A promise resolving to a function to unlisten to the event.
|
||||
*/
|
||||
async onChange(cb) {
|
||||
return await E("store://change", (event) => {
|
||||
if (event.payload.path === this.path) {
|
||||
cb(event.payload.key, event.payload.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export { Store };
|
||||
//# sourceMappingURL=index.min.js.map
|
||||
1
dist-js/index.min.js.map
Normal file
1
dist-js/index.min.js.map
Normal file
File diff suppressed because one or more lines are too long
177
dist-js/index.mjs
Normal file
177
dist-js/index.mjs
Normal file
@@ -0,0 +1,177 @@
|
||||
import { invoke } from '@tauri-apps/api/tauri';
|
||||
import { listen } from '@tauri-apps/api/event';
|
||||
|
||||
// Copyright 2021 Tauri Programme within The Commons Conservancy
|
||||
/**
|
||||
* A key-value store persisted by the backend layer.
|
||||
*/
|
||||
class Store {
|
||||
constructor(path) {
|
||||
this.path = path;
|
||||
}
|
||||
/**
|
||||
* Inserts a key-value pair into the store.
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @returns
|
||||
*/
|
||||
async set(key, value) {
|
||||
return await invoke("plugin:store|set", {
|
||||
path: this.path,
|
||||
key,
|
||||
value,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns the value for the given `key` or `null` the key does not exist.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
async get(key) {
|
||||
return await invoke("plugin:store|get", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns `true` if the given `key` exists in the store.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
async has(key) {
|
||||
return await invoke("plugin:store|has", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Removes a key-value pair from the store.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
async delete(key) {
|
||||
return await invoke("plugin:store|delete", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Clears the store, removing all key-value pairs.
|
||||
*
|
||||
* Note: To clear the storage and reset it to it's `default` value, use `reset` instead.
|
||||
* @returns
|
||||
*/
|
||||
async clear() {
|
||||
return await invoke("plugin:store|clear", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Resets the store to it's `default` value.
|
||||
*
|
||||
* If no default value has been set, this method behaves identical to `clear`.
|
||||
* @returns
|
||||
*/
|
||||
async reset() {
|
||||
return await invoke("plugin:store|reset", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns a list of all key in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async keys() {
|
||||
return await invoke("plugin:store|keys", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns a list of all values in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async values() {
|
||||
return await invoke("plugin:store|values", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns a list of all entries in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async entries() {
|
||||
return await invoke("plugin:store|entries", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns the number of key-value pairs in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async length() {
|
||||
return await invoke("plugin:store|length", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Attempts to load the on-disk state at the stores `path` into memory.
|
||||
*
|
||||
* This method is useful if the on-disk state was edited by the user and you want to synchronize the changes.
|
||||
*
|
||||
* Note: This method does not emit change events.
|
||||
* @returns
|
||||
*/
|
||||
async load() {
|
||||
return await invoke("plugin:store|load", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Saves the store to disk at the stores `path`.
|
||||
*
|
||||
* As the store is only persisted to disk before the apps exit, changes might be lost in a crash.
|
||||
* This method lets you persist the store to disk whenever you deem necessary.
|
||||
* @returns
|
||||
*/
|
||||
async save() {
|
||||
return await invoke("plugin:store|save", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Listen to changes on a store key.
|
||||
* @param key
|
||||
* @param cb
|
||||
* @returns A promise resolving to a function to unlisten to the event.
|
||||
*/
|
||||
async onKeyChange(key, cb) {
|
||||
return await listen("store://change", (event) => {
|
||||
if (event.payload.path === this.path && event.payload.key === key) {
|
||||
cb(event.payload.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Listen to changes on the store.
|
||||
* @param cb
|
||||
* @returns A promise resolving to a function to unlisten to the event.
|
||||
*/
|
||||
async onChange(cb) {
|
||||
return await listen("store://change", (event) => {
|
||||
if (event.payload.path === this.path) {
|
||||
cb(event.payload.key, event.payload.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export { Store };
|
||||
//# sourceMappingURL=index.mjs.map
|
||||
1
dist-js/index.mjs.map
Normal file
1
dist-js/index.mjs.map
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.mjs","sources":["../guest-js/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAAA;AAaA;;AAEG;MACU,KAAK,CAAA;AAEhB,IAAA,WAAA,CAAY,IAAY,EAAA;AACtB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;KAClB;AAED;;;;;;AAMG;AACH,IAAA,MAAM,GAAG,CAAC,GAAW,EAAE,KAAc,EAAA;AACnC,QAAA,OAAO,MAAM,MAAM,CAAC,kBAAkB,EAAE;YACtC,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,GAAG;YACH,KAAK;AACN,SAAA,CAAC,CAAC;KACJ;AAED;;;;;AAKG;IACH,MAAM,GAAG,CAAI,GAAW,EAAA;AACtB,QAAA,OAAO,MAAM,MAAM,CAAC,kBAAkB,EAAE;YACtC,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,GAAG;AACJ,SAAA,CAAC,CAAC;KACJ;AAED;;;;;AAKG;IACH,MAAM,GAAG,CAAC,GAAW,EAAA;AACnB,QAAA,OAAO,MAAM,MAAM,CAAC,kBAAkB,EAAE;YACtC,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,GAAG;AACJ,SAAA,CAAC,CAAC;KACJ;AAED;;;;;AAKG;IACH,MAAM,MAAM,CAAC,GAAW,EAAA;AACtB,QAAA,OAAO,MAAM,MAAM,CAAC,qBAAqB,EAAE;YACzC,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,GAAG;AACJ,SAAA,CAAC,CAAC;KACJ;AAED;;;;;AAKG;AACH,IAAA,MAAM,KAAK,GAAA;AACT,QAAA,OAAO,MAAM,MAAM,CAAC,oBAAoB,EAAE;YACxC,IAAI,EAAE,IAAI,CAAC,IAAI;AAChB,SAAA,CAAC,CAAC;KACJ;AAED;;;;;AAKG;AACH,IAAA,MAAM,KAAK,GAAA;AACT,QAAA,OAAO,MAAM,MAAM,CAAC,oBAAoB,EAAE;YACxC,IAAI,EAAE,IAAI,CAAC,IAAI;AAChB,SAAA,CAAC,CAAC;KACJ;AAED;;;;AAIG;AACH,IAAA,MAAM,IAAI,GAAA;AACR,QAAA,OAAO,MAAM,MAAM,CAAC,mBAAmB,EAAE;YACvC,IAAI,EAAE,IAAI,CAAC,IAAI;AAChB,SAAA,CAAC,CAAC;KACJ;AAED;;;;AAIG;AACH,IAAA,MAAM,MAAM,GAAA;AACV,QAAA,OAAO,MAAM,MAAM,CAAC,qBAAqB,EAAE;YACzC,IAAI,EAAE,IAAI,CAAC,IAAI;AAChB,SAAA,CAAC,CAAC;KACJ;AAED;;;;AAIG;AACH,IAAA,MAAM,OAAO,GAAA;AACX,QAAA,OAAO,MAAM,MAAM,CAAC,sBAAsB,EAAE;YAC1C,IAAI,EAAE,IAAI,CAAC,IAAI;AAChB,SAAA,CAAC,CAAC;KACJ;AAED;;;;AAIG;AACH,IAAA,MAAM,MAAM,GAAA;AACV,QAAA,OAAO,MAAM,MAAM,CAAC,qBAAqB,EAAE;YACzC,IAAI,EAAE,IAAI,CAAC,IAAI;AAChB,SAAA,CAAC,CAAC;KACJ;AAED;;;;;;;AAOG;AACH,IAAA,MAAM,IAAI,GAAA;AACR,QAAA,OAAO,MAAM,MAAM,CAAC,mBAAmB,EAAE;YACvC,IAAI,EAAE,IAAI,CAAC,IAAI;AAChB,SAAA,CAAC,CAAC;KACJ;AAED;;;;;;AAMG;AACH,IAAA,MAAM,IAAI,GAAA;AACR,QAAA,OAAO,MAAM,MAAM,CAAC,mBAAmB,EAAE;YACvC,IAAI,EAAE,IAAI,CAAC,IAAI;AAChB,SAAA,CAAC,CAAC;KACJ;AAED;;;;;AAKG;AACH,IAAA,MAAM,WAAW,CACf,GAAW,EACX,EAA6B,EAAA;QAE7B,OAAO,MAAM,MAAM,CAAmB,gBAAgB,EAAE,CAAC,KAAK,KAAI;AAChE,YAAA,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,KAAK,GAAG,EAAE;AACjE,gBAAA,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AACzB,aAAA;AACH,SAAC,CAAC,CAAC;KACJ;AAED;;;;AAIG;IACH,MAAM,QAAQ,CACZ,EAA0C,EAAA;QAE1C,OAAO,MAAM,MAAM,CAAmB,gBAAgB,EAAE,CAAC,KAAK,KAAI;YAChE,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,EAAE;AACpC,gBAAA,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC5C,aAAA;AACH,SAAC,CAAC,CAAC;KACJ;AACF;;;;"}
|
||||
203
guest-js/index.ts
Normal file
203
guest-js/index.ts
Normal file
@@ -0,0 +1,203 @@
|
||||
// Copyright 2021 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
import { listen, UnlistenFn } from "@tauri-apps/api/event";
|
||||
|
||||
interface ChangePayload<T> {
|
||||
path: string;
|
||||
key: string;
|
||||
value: T | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A key-value store persisted by the backend layer.
|
||||
*/
|
||||
export class Store {
|
||||
path: string;
|
||||
constructor(path: string) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a key-value pair into the store.
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @returns
|
||||
*/
|
||||
async set(key: string, value: unknown): Promise<void> {
|
||||
return await invoke("plugin:store|set", {
|
||||
path: this.path,
|
||||
key,
|
||||
value,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value for the given `key` or `null` the key does not exist.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
async get<T>(key: string): Promise<T | null> {
|
||||
return await invoke("plugin:store|get", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the given `key` exists in the store.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
async has(key: string): Promise<boolean> {
|
||||
return await invoke("plugin:store|has", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a key-value pair from the store.
|
||||
*
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
async delete(key: string): Promise<boolean> {
|
||||
return await invoke("plugin:store|delete", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the store, removing all key-value pairs.
|
||||
*
|
||||
* Note: To clear the storage and reset it to it's `default` value, use `reset` instead.
|
||||
* @returns
|
||||
*/
|
||||
async clear(): Promise<void> {
|
||||
return await invoke("plugin:store|clear", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the store to it's `default` value.
|
||||
*
|
||||
* If no default value has been set, this method behaves identical to `clear`.
|
||||
* @returns
|
||||
*/
|
||||
async reset(): Promise<void> {
|
||||
return await invoke("plugin:store|reset", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all key in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async keys(): Promise<string[]> {
|
||||
return await invoke("plugin:store|keys", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all values in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async values<T>(): Promise<T[]> {
|
||||
return await invoke("plugin:store|values", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all entries in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async entries<T>(): Promise<Array<[key: string, value: T]>> {
|
||||
return await invoke("plugin:store|entries", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of key-value pairs in the store.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
async length(): Promise<number> {
|
||||
return await invoke("plugin:store|length", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to load the on-disk state at the stores `path` into memory.
|
||||
*
|
||||
* This method is useful if the on-disk state was edited by the user and you want to synchronize the changes.
|
||||
*
|
||||
* Note: This method does not emit change events.
|
||||
* @returns
|
||||
*/
|
||||
async load(): Promise<void> {
|
||||
return await invoke("plugin:store|load", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the store to disk at the stores `path`.
|
||||
*
|
||||
* As the store is only persisted to disk before the apps exit, changes might be lost in a crash.
|
||||
* This method lets you persist the store to disk whenever you deem necessary.
|
||||
* @returns
|
||||
*/
|
||||
async save(): Promise<void> {
|
||||
return await invoke("plugin:store|save", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen to changes on a store key.
|
||||
* @param key
|
||||
* @param cb
|
||||
* @returns A promise resolving to a function to unlisten to the event.
|
||||
*/
|
||||
async onKeyChange<T>(
|
||||
key: string,
|
||||
cb: (value: T | null) => void
|
||||
): Promise<UnlistenFn> {
|
||||
return await listen<ChangePayload<T>>("store://change", (event) => {
|
||||
if (event.payload.path === this.path && event.payload.key === key) {
|
||||
cb(event.payload.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen to changes on the store.
|
||||
* @param cb
|
||||
* @returns A promise resolving to a function to unlisten to the event.
|
||||
*/
|
||||
async onChange<T>(
|
||||
cb: (key: string, value: T | null) => void
|
||||
): Promise<UnlistenFn> {
|
||||
return await listen<ChangePayload<T>>("store://change", (event) => {
|
||||
if (event.payload.path === this.path) {
|
||||
cb(event.payload.key, event.payload.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
33
package.json
Normal file
33
package.json
Normal file
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "tauri-plugin-store-api",
|
||||
"version": "0.0.0",
|
||||
"description": "Simple, persistent key-value store.",
|
||||
"license": "MIT or APACHE-2.0",
|
||||
"authors": [
|
||||
"Tauri Programme within The Commons Conservancy"
|
||||
],
|
||||
"type": "module",
|
||||
"browser": "dist-js/index.min.js",
|
||||
"module": "dist-js/index.mjs",
|
||||
"types": "dist-js/index.d.ts",
|
||||
"exports": {
|
||||
"import": "./dist-js/index.mjs",
|
||||
"types": "./dist-js/index.d.ts",
|
||||
"browser": "./dist-js/index.min.js"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "rollup -c"
|
||||
},
|
||||
"files": [
|
||||
"dist-js",
|
||||
"!dist-js/**/*.map",
|
||||
"README.md",
|
||||
"LICENSE"
|
||||
],
|
||||
"devDependencies": {
|
||||
"tslib": "^2.5.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tauri-apps/api": "^1.2.0"
|
||||
}
|
||||
}
|
||||
11
rollup.config.mjs
Normal file
11
rollup.config.mjs
Normal file
@@ -0,0 +1,11 @@
|
||||
import { readFileSync } from "fs";
|
||||
|
||||
import { createConfig } from "../../shared/rollup.config.mjs";
|
||||
|
||||
export default createConfig({
|
||||
input: "guest-js/index.ts",
|
||||
pkg: JSON.parse(
|
||||
readFileSync(new URL("./package.json", import.meta.url), "utf8")
|
||||
),
|
||||
external: [/^@tauri-apps\/api/],
|
||||
});
|
||||
37
src/error.rs
Normal file
37
src/error.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright 2021 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::{Serialize, Serializer};
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// The error types.
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[non_exhaustive]
|
||||
pub enum Error {
|
||||
#[error("Failed to serialize store. {0}")]
|
||||
Serialize(Box<dyn std::error::Error + Send + Sync>),
|
||||
#[error("Failed to deserialize store. {0}")]
|
||||
Deserialize(Box<dyn std::error::Error + Send + Sync>),
|
||||
/// JSON error.
|
||||
#[error(transparent)]
|
||||
Json(#[from] serde_json::Error),
|
||||
/// IO error.
|
||||
#[error(transparent)]
|
||||
Io(#[from] std::io::Error),
|
||||
/// Store not found
|
||||
#[error("Store \"{0}\" not found")]
|
||||
NotFound(PathBuf),
|
||||
/// Some Tauri API failed
|
||||
#[error(transparent)]
|
||||
Tauri(#[from] tauri::Error),
|
||||
}
|
||||
|
||||
impl Serialize for Error {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.to_string().as_ref())
|
||||
}
|
||||
}
|
||||
320
src/lib.rs
Normal file
320
src/lib.rs
Normal file
@@ -0,0 +1,320 @@
|
||||
// Copyright 2021 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pub use error::Error;
|
||||
use log::warn;
|
||||
use serde::Serialize;
|
||||
pub use serde_json::Value as JsonValue;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
path::{Path, PathBuf},
|
||||
sync::Mutex,
|
||||
};
|
||||
pub use store::{Store, StoreBuilder};
|
||||
use tauri::{
|
||||
plugin::{self, TauriPlugin},
|
||||
AppHandle, Manager, RunEvent, Runtime, State,
|
||||
};
|
||||
|
||||
mod error;
|
||||
mod store;
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
struct ChangePayload<'a> {
|
||||
path: &'a Path,
|
||||
key: &'a str,
|
||||
value: &'a JsonValue,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct StoreCollection<R: Runtime> {
|
||||
stores: Mutex<HashMap<PathBuf, Store<R>>>,
|
||||
frozen: bool,
|
||||
}
|
||||
|
||||
pub fn with_store<R: Runtime, T, F: FnOnce(&mut Store<R>) -> Result<T, Error>>(
|
||||
app: AppHandle<R>,
|
||||
collection: State<'_, StoreCollection<R>>,
|
||||
path: impl AsRef<Path>,
|
||||
f: F,
|
||||
) -> Result<T, Error> {
|
||||
let mut stores = collection.stores.lock().expect("mutex poisoned");
|
||||
|
||||
let path = path.as_ref();
|
||||
if !stores.contains_key(path) {
|
||||
if collection.frozen {
|
||||
return Err(Error::NotFound(path.to_path_buf()));
|
||||
}
|
||||
let mut store = StoreBuilder::new(app, path.to_path_buf()).build();
|
||||
// ignore loading errors, just use the default
|
||||
if let Err(err) = store.load() {
|
||||
warn!(
|
||||
"Failed to load store {:?} from disk: {}. Falling back to default values.",
|
||||
path, err
|
||||
);
|
||||
}
|
||||
stores.insert(path.to_path_buf(), store);
|
||||
}
|
||||
|
||||
f(stores
|
||||
.get_mut(path)
|
||||
.expect("failed to retrieve store. This is a bug!"))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn set<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
stores: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
key: String,
|
||||
value: JsonValue,
|
||||
) -> Result<(), Error> {
|
||||
with_store(app, stores, path, |store| store.insert(key, value))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn get<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
stores: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
key: String,
|
||||
) -> Result<Option<JsonValue>, Error> {
|
||||
with_store(app, stores, path, |store| Ok(store.get(key).cloned()))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn has<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
stores: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
key: String,
|
||||
) -> Result<bool, Error> {
|
||||
with_store(app, stores, path, |store| Ok(store.has(key)))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn delete<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
stores: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
key: String,
|
||||
) -> Result<bool, Error> {
|
||||
with_store(app, stores, path, |store| store.delete(key))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn clear<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
stores: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
) -> Result<(), Error> {
|
||||
with_store(app, stores, path, |store| store.clear())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn reset<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
collection: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
) -> Result<(), Error> {
|
||||
with_store(app, collection, path, |store| store.reset())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn keys<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
stores: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
) -> Result<Vec<String>, Error> {
|
||||
with_store(app, stores, path, |store| {
|
||||
Ok(store.keys().cloned().collect())
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn values<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
stores: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
) -> Result<Vec<JsonValue>, Error> {
|
||||
with_store(app, stores, path, |store| {
|
||||
Ok(store.values().cloned().collect())
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn entries<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
stores: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
) -> Result<Vec<(String, JsonValue)>, Error> {
|
||||
with_store(app, stores, path, |store| {
|
||||
Ok(store
|
||||
.entries()
|
||||
.map(|(k, v)| (k.to_owned(), v.to_owned()))
|
||||
.collect())
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn length<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
stores: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
) -> Result<usize, Error> {
|
||||
with_store(app, stores, path, |store| Ok(store.len()))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn load<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
stores: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
) -> Result<(), Error> {
|
||||
with_store(app, stores, path, |store| store.load())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn save<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
stores: State<'_, StoreCollection<R>>,
|
||||
path: PathBuf,
|
||||
) -> Result<(), Error> {
|
||||
with_store(app, stores, path, |store| store.save())
|
||||
}
|
||||
|
||||
// #[derive(Default)]
|
||||
pub struct Builder<R: Runtime> {
|
||||
stores: HashMap<PathBuf, Store<R>>,
|
||||
frozen: bool,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Default for Builder<R> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
stores: Default::default(),
|
||||
frozen: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Runtime> Builder<R> {
|
||||
/// Registers a store with the plugin.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use tauri_plugin_store::{StoreBuilder,PluginBuilder};
|
||||
///
|
||||
/// let store = StoreBuilder::new("store.bin".parse()?).build();
|
||||
///
|
||||
/// let builder = PluginBuilder::default().store(store);
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn store(mut self, store: Store<R>) -> Self {
|
||||
self.stores.insert(store.path.clone(), store);
|
||||
self
|
||||
}
|
||||
|
||||
/// Registers multiple stores with the plugin.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use tauri_plugin_store::{StoreBuilder,PluginBuilder};
|
||||
///
|
||||
/// let store = StoreBuilder::new("store.bin".parse()?).build();
|
||||
///
|
||||
/// let builder = PluginBuilder::default().stores([store]);
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn stores<T: IntoIterator<Item = Store<R>>>(mut self, stores: T) -> Self {
|
||||
self.stores = stores
|
||||
.into_iter()
|
||||
.map(|store| (store.path.clone(), store))
|
||||
.collect();
|
||||
self
|
||||
}
|
||||
|
||||
/// Freezes the collection.
|
||||
///
|
||||
/// This causes requests for plugins that haven't been registered to fail
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use tauri_plugin_store::{StoreBuilder,PluginBuilder};
|
||||
///
|
||||
/// let store = StoreBuilder::new("store.bin".parse()?).build();
|
||||
///
|
||||
/// let builder = PluginBuilder::default().freeze();
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn freeze(mut self) -> Self {
|
||||
self.frozen = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// Builds the plugin.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use tauri_plugin_store::{StoreBuilder,PluginBuilder};
|
||||
/// use tauri::Wry;
|
||||
///
|
||||
/// let store = StoreBuilder::new("store.bin".parse()?).build();
|
||||
///
|
||||
/// let plugin = PluginBuilder::default().build::<Wry>();
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn build(mut self) -> TauriPlugin<R> {
|
||||
plugin::Builder::new("store")
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
set, get, has, delete, clear, reset, keys, values, length, entries, load, save
|
||||
])
|
||||
.setup(move |app_handle| {
|
||||
for (path, store) in self.stores.iter_mut() {
|
||||
// ignore loading errors, just use the default
|
||||
if let Err(err) = store.load() {
|
||||
warn!(
|
||||
"Failed to load store {:?} from disk: {}. Falling back to default values.",
|
||||
path, err
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
app_handle.manage(StoreCollection {
|
||||
stores: Mutex::new(self.stores),
|
||||
frozen: self.frozen,
|
||||
});
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.on_event(|app_handle, event| {
|
||||
if let RunEvent::Exit = event {
|
||||
let collection = app_handle.state::<StoreCollection<R>>();
|
||||
|
||||
for store in collection.stores.lock().expect("mutex poisoned").values() {
|
||||
if let Err(err) = store.save() {
|
||||
eprintln!("failed to save store {:?} with error {:?}", store.path, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.build()
|
||||
}
|
||||
}
|
||||
319
src/store.rs
Normal file
319
src/store.rs
Normal file
@@ -0,0 +1,319 @@
|
||||
// Copyright 2021 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use crate::{ChangePayload, Error};
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::{create_dir_all, read, File},
|
||||
io::Write,
|
||||
path::PathBuf,
|
||||
};
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
|
||||
type SerializeFn =
|
||||
fn(&HashMap<String, JsonValue>) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>;
|
||||
type DeserializeFn =
|
||||
fn(&[u8]) -> Result<HashMap<String, JsonValue>, Box<dyn std::error::Error + Send + Sync>>;
|
||||
|
||||
fn default_serialize(
|
||||
cache: &HashMap<String, JsonValue>,
|
||||
) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
Ok(serde_json::to_vec(&cache)?)
|
||||
}
|
||||
|
||||
fn default_deserialize(
|
||||
bytes: &[u8],
|
||||
) -> Result<HashMap<String, JsonValue>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
serde_json::from_slice(bytes).map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Builds a [`Store`]
|
||||
pub struct StoreBuilder<R: Runtime> {
|
||||
app: AppHandle<R>,
|
||||
path: PathBuf,
|
||||
defaults: Option<HashMap<String, JsonValue>>,
|
||||
cache: HashMap<String, JsonValue>,
|
||||
serialize: SerializeFn,
|
||||
deserialize: DeserializeFn,
|
||||
}
|
||||
|
||||
impl<R: Runtime> StoreBuilder<R> {
|
||||
/// Creates a new [`StoreBuilder`].
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use tauri_plugin_store::StoreBuilder;
|
||||
///
|
||||
/// let builder = StoreBuilder::new("store.bin".parse()?);
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn new(app: AppHandle<R>, path: PathBuf) -> Self {
|
||||
Self {
|
||||
app,
|
||||
path,
|
||||
defaults: None,
|
||||
cache: Default::default(),
|
||||
serialize: default_serialize,
|
||||
deserialize: default_deserialize,
|
||||
}
|
||||
}
|
||||
|
||||
/// Inserts a default key-value pair.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use tauri_plugin_store::StoreBuilder;
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// let mut defaults = HashMap::new();
|
||||
///
|
||||
/// defaults.insert("foo".to_string(), "bar".into());
|
||||
///
|
||||
/// let builder = StoreBuilder::new("store.bin".parse()?)
|
||||
/// .defaults(defaults);
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
pub fn defaults(mut self, defaults: HashMap<String, JsonValue>) -> Self {
|
||||
self.cache = defaults.clone();
|
||||
self.defaults = Some(defaults);
|
||||
self
|
||||
}
|
||||
|
||||
/// Inserts multiple key-value pairs.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use tauri_plugin_store::StoreBuilder;
|
||||
///
|
||||
/// let builder = StoreBuilder::new("store.bin".parse()?)
|
||||
/// .default("foo".to_string(), "bar".into());
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
pub fn default(mut self, key: String, value: JsonValue) -> Self {
|
||||
self.cache.insert(key.clone(), value.clone());
|
||||
self.defaults
|
||||
.get_or_insert(HashMap::new())
|
||||
.insert(key, value);
|
||||
self
|
||||
}
|
||||
|
||||
/// Defines a custom serialization function.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use tauri_plugin_store::StoreBuilder;
|
||||
///
|
||||
/// let builder = StoreBuilder::new("store.json".parse()?)
|
||||
/// .serialize(|cache| serde_json::to_vec(&cache).map_err(Into::into));
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
pub fn serialize(mut self, serialize: SerializeFn) -> Self {
|
||||
self.serialize = serialize;
|
||||
self
|
||||
}
|
||||
|
||||
/// Defines a custom deserialization function
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use tauri_plugin_store::StoreBuilder;
|
||||
///
|
||||
/// let builder = StoreBuilder::new("store.json".parse()?)
|
||||
/// .deserialize(|bytes| serde_json::from_slice(&bytes).map_err(Into::into));
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
pub fn deserialize(mut self, deserialize: DeserializeFn) -> Self {
|
||||
self.deserialize = deserialize;
|
||||
self
|
||||
}
|
||||
|
||||
/// Builds the [`Store`].
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use tauri_plugin_store::StoreBuilder;
|
||||
///
|
||||
/// let store = StoreBuilder::new("store.bin".parse()?).build();
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
pub fn build(self) -> Store<R> {
|
||||
Store {
|
||||
app: self.app,
|
||||
path: self.path,
|
||||
defaults: self.defaults,
|
||||
cache: self.cache,
|
||||
serialize: self.serialize,
|
||||
deserialize: self.deserialize,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Store<R: Runtime> {
|
||||
app: AppHandle<R>,
|
||||
pub(crate) path: PathBuf,
|
||||
defaults: Option<HashMap<String, JsonValue>>,
|
||||
cache: HashMap<String, JsonValue>,
|
||||
serialize: SerializeFn,
|
||||
deserialize: DeserializeFn,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Store<R> {
|
||||
/// Update the store from the on-disk state
|
||||
pub fn load(&mut self) -> Result<(), Error> {
|
||||
let app_dir = self
|
||||
.app
|
||||
.path_resolver()
|
||||
.app_data_dir()
|
||||
.expect("failed to resolve app dir");
|
||||
let store_path = app_dir.join(&self.path);
|
||||
|
||||
let bytes = read(store_path)?;
|
||||
|
||||
self.cache
|
||||
.extend((self.deserialize)(&bytes).map_err(Error::Deserialize)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Saves the store to disk
|
||||
pub fn save(&self) -> Result<(), Error> {
|
||||
let app_dir = self
|
||||
.app
|
||||
.path_resolver()
|
||||
.app_data_dir()
|
||||
.expect("failed to resolve app dir");
|
||||
let store_path = app_dir.join(&self.path);
|
||||
|
||||
create_dir_all(store_path.parent().expect("invalid store path"))?;
|
||||
|
||||
let bytes = (self.serialize)(&self.cache).map_err(Error::Serialize)?;
|
||||
let mut f = File::create(&store_path)?;
|
||||
f.write_all(&bytes)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, key: String, value: JsonValue) -> Result<(), Error> {
|
||||
self.cache.insert(key.clone(), value.clone());
|
||||
self.app.emit_all(
|
||||
"store://change",
|
||||
ChangePayload {
|
||||
path: &self.path,
|
||||
key: &key,
|
||||
value: &value,
|
||||
},
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get(&self, key: impl AsRef<str>) -> Option<&JsonValue> {
|
||||
self.cache.get(key.as_ref())
|
||||
}
|
||||
|
||||
pub fn has(&self, key: impl AsRef<str>) -> bool {
|
||||
self.cache.contains_key(key.as_ref())
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, key: impl AsRef<str>) -> Result<bool, Error> {
|
||||
let flag = self.cache.remove(key.as_ref()).is_some();
|
||||
if flag {
|
||||
self.app.emit_all(
|
||||
"store://change",
|
||||
ChangePayload {
|
||||
path: &self.path,
|
||||
key: key.as_ref(),
|
||||
value: &JsonValue::Null,
|
||||
},
|
||||
)?;
|
||||
}
|
||||
Ok(flag)
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) -> Result<(), Error> {
|
||||
let keys: Vec<String> = self.cache.keys().cloned().collect();
|
||||
self.cache.clear();
|
||||
for key in keys {
|
||||
self.app.emit_all(
|
||||
"store://change",
|
||||
ChangePayload {
|
||||
path: &self.path,
|
||||
key: &key,
|
||||
value: &JsonValue::Null,
|
||||
},
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) -> Result<(), Error> {
|
||||
let has_defaults = self.defaults.is_some();
|
||||
|
||||
if has_defaults {
|
||||
if let Some(defaults) = &self.defaults {
|
||||
for (key, value) in &self.cache {
|
||||
if defaults.get(key) != Some(value) {
|
||||
let _ = self.app.emit_all(
|
||||
"store://change",
|
||||
ChangePayload {
|
||||
path: &self.path,
|
||||
key,
|
||||
value: defaults.get(key).unwrap_or(&JsonValue::Null),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
self.cache = defaults.clone();
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
self.clear()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> impl Iterator<Item = &String> {
|
||||
self.cache.keys()
|
||||
}
|
||||
|
||||
pub fn values(&self) -> impl Iterator<Item = &JsonValue> {
|
||||
self.cache.values()
|
||||
}
|
||||
|
||||
pub fn entries(&self) -> impl Iterator<Item = (&String, &JsonValue)> {
|
||||
self.cache.iter()
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.cache.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.cache.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Runtime> std::fmt::Debug for Store<R> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Store")
|
||||
.field("path", &self.path)
|
||||
.field("defaults", &self.defaults)
|
||||
.field("cache", &self.cache)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
1
tsconfig.json
Symbolic link
1
tsconfig.json
Symbolic link
@@ -0,0 +1 @@
|
||||
../../shared/tsconfig.json
|
||||
Reference in New Issue
Block a user