File size: 20,027 Bytes
2d8be8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use crate::{ChangePayload, StoreState};
use serde_json::Value as JsonValue;
use std::{
collections::HashMap,
fs,
path::{Path, PathBuf},
sync::{Arc, Mutex},
time::Duration,
};
use tauri::{path::BaseDirectory, AppHandle, Emitter, Manager, Resource, ResourceId, Runtime};
use tokio::{
select,
sync::mpsc::{unbounded_channel, UnboundedSender},
time::sleep,
};
pub type SerializeFn =
fn(&HashMap<String, JsonValue>) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>;
pub type DeserializeFn =
fn(&[u8]) -> Result<HashMap<String, JsonValue>, Box<dyn std::error::Error + Send + Sync>>;
pub fn resolve_store_path<R: Runtime>(
app: &AppHandle<R>,
path: impl AsRef<Path>,
) -> crate::Result<PathBuf> {
Ok(dunce::simplified(&app.path().resolve(path, BaseDirectory::AppData)?).to_path_buf())
}
/// Builds a [`Store`]
pub struct StoreBuilder<R: Runtime> {
app: AppHandle<R>,
path: PathBuf,
defaults: Option<HashMap<String, JsonValue>>,
serialize_fn: SerializeFn,
deserialize_fn: DeserializeFn,
auto_save: Option<Duration>,
create_new: bool,
override_defaults: bool,
}
impl<R: Runtime> StoreBuilder<R> {
/// Creates a new [`StoreBuilder`].
///
/// # Examples
/// ```
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let builder = tauri_plugin_store::StoreBuilder::new(app, "store.bin");
/// Ok(())
/// });
/// ```
pub fn new<M: Manager<R>, P: AsRef<Path>>(manager: &M, path: P) -> Self {
let app = manager.app_handle().clone();
let state = app.state::<StoreState>();
let serialize_fn = state.default_serialize;
let deserialize_fn = state.default_deserialize;
Self {
app,
path: path.as_ref().to_path_buf(),
defaults: None,
serialize_fn,
deserialize_fn,
auto_save: Some(Duration::from_millis(100)),
create_new: false,
override_defaults: false,
}
}
/// Inserts a default key-value pair.
///
/// # Examples
/// ```
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let mut defaults = std::collections::HashMap::new();
/// defaults.insert("foo".to_string(), "bar".into());
///
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// .defaults(defaults)
/// .build()?;
/// Ok(())
/// });
/// ```
pub fn defaults(mut self, defaults: HashMap<String, JsonValue>) -> Self {
self.defaults = Some(defaults);
self
}
/// Inserts multiple default key-value pairs.
///
/// # Examples
/// ```
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// .default("foo".to_string(), "bar")
/// .build()?;
/// Ok(())
/// });
/// ```
pub fn default(mut self, key: impl Into<String>, value: impl Into<JsonValue>) -> Self {
let key = key.into();
let value = value.into();
self.defaults
.get_or_insert(HashMap::new())
.insert(key, value);
self
}
/// Defines a custom serialization function.
///
/// # Examples
/// ```
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .serialize(|cache| serde_json::to_vec(&cache).map_err(Into::into))
/// .build()?;
/// Ok(())
/// });
/// ```
pub fn serialize(mut self, serialize: SerializeFn) -> Self {
self.serialize_fn = serialize;
self
}
/// Defines a custom deserialization function
///
/// # Examples
/// ```
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .deserialize(|bytes| serde_json::from_slice(&bytes).map_err(Into::into))
/// .build()?;
/// Ok(())
/// });
/// ```
pub fn deserialize(mut self, deserialize: DeserializeFn) -> Self {
self.deserialize_fn = deserialize;
self
}
/// Auto save on modified with a debounce duration
///
/// # Examples
/// ```
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .auto_save(std::time::Duration::from_millis(100))
/// .build()?;
/// Ok(())
/// });
/// ```
pub fn auto_save(mut self, debounce_duration: Duration) -> Self {
self.auto_save = Some(debounce_duration);
self
}
/// Disable auto save on modified with a debounce duration.
pub fn disable_auto_save(mut self) -> Self {
self.auto_save = None;
self
}
/// Force create a new store with default values even if it already exists.
pub fn create_new(mut self) -> Self {
self.create_new = true;
self
}
/// Override the store values when creating the store, ignoring defaults.
pub fn override_defaults(mut self) -> Self {
self.override_defaults = true;
self
}
pub(crate) fn build_inner(mut self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
let stores = self.app.state::<StoreState>().stores.clone();
let mut stores = stores.lock().unwrap();
self.path = resolve_store_path(&self.app, self.path)?;
if self.create_new {
if let Some(rid) = stores.remove(&self.path) {
let _ = self.app.resources_table().take::<Store<R>>(rid);
}
} else if let Some(rid) = stores.get(&self.path) {
// The resource id we stored can be invalid due to
// the resource table getting modified by an external source
// (e.g. `App::cleanup_before_exit` > `manager.resources_table.clear()`)
return Ok((self.app.resources_table().get(*rid)?, *rid));
}
// if stores.contains_key(&self.path) {
// return Err(crate::Error::AlreadyExists(self.path));
// }
let mut store_inner = StoreInner::new(
self.app.clone(),
self.path.clone(),
self.defaults.take(),
self.serialize_fn,
self.deserialize_fn,
);
if !self.create_new {
if self.override_defaults {
let _ = store_inner.load_ignore_defaults();
} else {
let _ = store_inner.load();
}
}
let store = Store {
auto_save: self.auto_save,
auto_save_debounce_sender: Arc::new(Mutex::new(None)),
store: Arc::new(Mutex::new(store_inner)),
};
let store = Arc::new(store);
let rid = self.app.resources_table().add_arc(store.clone());
stores.insert(self.path, rid);
Ok((store, rid))
}
/// Load the existing store with the same path or creates a new [`Store`].
///
/// If a store with the same path has already been loaded its instance is returned.
///
/// # Examples
/// ```
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").build();
/// Ok(())
/// });
/// ```
pub fn build(self) -> crate::Result<Arc<Store<R>>> {
let (store, _) = self.build_inner()?;
Ok(store)
}
}
enum AutoSaveMessage {
Reset,
Cancel,
}
#[derive(Clone)]
struct StoreInner<R: Runtime> {
app: AppHandle<R>,
path: PathBuf,
cache: HashMap<String, JsonValue>,
defaults: Option<HashMap<String, JsonValue>>,
serialize_fn: SerializeFn,
deserialize_fn: DeserializeFn,
}
impl<R: Runtime> StoreInner<R> {
fn new(
app: AppHandle<R>,
path: PathBuf,
defaults: Option<HashMap<String, JsonValue>>,
serialize_fn: SerializeFn,
deserialize_fn: DeserializeFn,
) -> Self {
Self {
app,
path,
cache: defaults.clone().unwrap_or_default(),
defaults,
serialize_fn,
deserialize_fn,
}
}
/// Saves the store to disk at the store's `path`.
pub fn save(&self) -> crate::Result<()> {
fs::create_dir_all(self.path.parent().expect("invalid store path"))?;
let bytes = (self.serialize_fn)(&self.cache).map_err(crate::Error::Serialize)?;
fs::write(&self.path, bytes)?;
Ok(())
}
/// Update the store from the on-disk state
///
/// Note: This method loads the data and merges it with the current store
pub fn load(&mut self) -> crate::Result<()> {
let bytes = fs::read(&self.path)?;
self.cache
.extend((self.deserialize_fn)(&bytes).map_err(crate::Error::Deserialize)?);
Ok(())
}
/// Load the store from the on-disk state, ignoring defaults
pub fn load_ignore_defaults(&mut self) -> crate::Result<()> {
let bytes = fs::read(&self.path)?;
self.cache = (self.deserialize_fn)(&bytes).map_err(crate::Error::Deserialize)?;
Ok(())
}
/// Inserts a key-value pair into the store.
pub fn set(&mut self, key: impl Into<String>, value: impl Into<JsonValue>) {
let key = key.into();
let value = value.into();
self.cache.insert(key.clone(), value.clone());
let _ = self.emit_change_event(&key, Some(&value));
}
/// Returns a reference to the value corresponding to the key.
pub fn get(&self, key: impl AsRef<str>) -> Option<&JsonValue> {
self.cache.get(key.as_ref())
}
/// Returns `true` if the given `key` exists in the store.
pub fn has(&self, key: impl AsRef<str>) -> bool {
self.cache.contains_key(key.as_ref())
}
/// Removes a key-value pair from the store.
pub fn delete(&mut self, key: impl AsRef<str>) -> bool {
let flag = self.cache.remove(key.as_ref()).is_some();
if flag {
let _ = self.emit_change_event(key.as_ref(), None);
}
flag
}
/// Clears the store, removing all key-value pairs.
///
/// Note: To clear the storage and reset it to its `default` value, use [`reset`](Self::reset) instead.
pub fn clear(&mut self) {
let keys: Vec<String> = self.cache.keys().cloned().collect();
self.cache.clear();
for key in &keys {
let _ = self.emit_change_event(key, None);
}
}
/// Resets the store to its `default` value.
///
/// If no default value has been set, this method behaves identical to [`clear`](Self::clear).
pub fn reset(&mut self) {
if let Some(defaults) = &self.defaults {
for (key, value) in &self.cache {
if defaults.get(key) != Some(value) {
let _ = self.emit_change_event(key, defaults.get(key));
}
}
for (key, value) in defaults {
if !self.cache.contains_key(key) {
let _ = self.emit_change_event(key, Some(value));
}
}
self.cache.clone_from(defaults);
} else {
self.clear()
}
}
/// An iterator visiting all keys in arbitrary order.
pub fn keys(&self) -> impl Iterator<Item = &String> {
self.cache.keys()
}
/// An iterator visiting all values in arbitrary order.
pub fn values(&self) -> impl Iterator<Item = &JsonValue> {
self.cache.values()
}
/// An iterator visiting all key-value pairs in arbitrary order.
pub fn entries(&self) -> impl Iterator<Item = (&String, &JsonValue)> {
self.cache.iter()
}
/// Returns the number of elements in the store.
pub fn len(&self) -> usize {
self.cache.len()
}
/// Returns true if the store contains no elements.
pub fn is_empty(&self) -> bool {
self.cache.is_empty()
}
fn emit_change_event(&self, key: &str, value: Option<&JsonValue>) -> crate::Result<()> {
let state = self.app.state::<StoreState>();
let stores = state.stores.lock().unwrap();
let exists = value.is_some();
self.app.emit(
"store://change",
ChangePayload {
path: &self.path,
resource_id: stores.get(&self.path).copied(),
key,
value,
exists,
},
)?;
Ok(())
}
}
impl<R: Runtime> std::fmt::Debug for StoreInner<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Store")
.field("path", &self.path)
.field("cache", &self.cache)
.finish()
}
}
pub struct Store<R: Runtime> {
auto_save: Option<Duration>,
auto_save_debounce_sender: Arc<Mutex<Option<UnboundedSender<AutoSaveMessage>>>>,
store: Arc<Mutex<StoreInner<R>>>,
}
impl<R: Runtime> Resource for Store<R> {
fn close(self: Arc<Self>) {
let store = self.store.lock().unwrap();
let state = store.app.state::<StoreState>();
let mut stores = state.stores.lock().unwrap();
stores.remove(&store.path);
}
}
impl<R: Runtime> Store<R> {
// /// Do something with the inner store,
// /// useful for batching some work if you need higher performance
// pub fn with_store<T>(&self, f: impl FnOnce(&mut StoreInner<R>) -> T) -> T {
// let mut store = self.store.lock().unwrap();
// f(&mut store)
// }
/// Inserts a key-value pair into the store.
pub fn set(&self, key: impl Into<String>, value: impl Into<JsonValue>) {
self.store.lock().unwrap().set(key.into(), value.into());
let _ = self.trigger_auto_save();
}
/// Returns the value for the given `key` or `None` if the key does not exist.
pub fn get(&self, key: impl AsRef<str>) -> Option<JsonValue> {
self.store.lock().unwrap().get(key).cloned()
}
/// Returns `true` if the given `key` exists in the store.
pub fn has(&self, key: impl AsRef<str>) -> bool {
self.store.lock().unwrap().has(key)
}
/// Removes a key-value pair from the store.
pub fn delete(&self, key: impl AsRef<str>) -> bool {
let deleted = self.store.lock().unwrap().delete(key);
if deleted {
let _ = self.trigger_auto_save();
}
deleted
}
/// Clears the store, removing all key-value pairs.
///
/// Note: To clear the storage and reset it to its `default` value, use [`reset`](Self::reset) instead.
pub fn clear(&self) {
self.store.lock().unwrap().clear();
let _ = self.trigger_auto_save();
}
/// Resets the store to its `default` value.
///
/// If no default value has been set, this method behaves identical to [`clear`](Self::clear).
pub fn reset(&self) {
self.store.lock().unwrap().reset();
let _ = self.trigger_auto_save();
}
/// Returns a list of all keys in the store.
pub fn keys(&self) -> Vec<String> {
self.store.lock().unwrap().keys().cloned().collect()
}
/// Returns a list of all values in the store.
pub fn values(&self) -> Vec<JsonValue> {
self.store.lock().unwrap().values().cloned().collect()
}
/// Returns a list of all key-value pairs in the store.
pub fn entries(&self) -> Vec<(String, JsonValue)> {
self.store
.lock()
.unwrap()
.entries()
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect()
}
/// Returns the number of elements in the store.
pub fn length(&self) -> usize {
self.store.lock().unwrap().len()
}
/// Returns true if the store contains no elements.
pub fn is_empty(&self) -> bool {
self.store.lock().unwrap().is_empty()
}
/// Update the store from the on-disk state
///
/// Note:
/// - This method loads the data and merges it with the current store,
/// this behavior will be changed to resetting to default first and then merging with the on-disk state in v3,
/// to fully match the store with the on-disk state,
/// use [`reload_override_defaults`](Self::reload_override_defaults) instead
/// - This method does not emit change events
pub fn reload(&self) -> crate::Result<()> {
self.store.lock().unwrap().load()
}
/// Load the store from the on-disk state, ignoring defaults
///
/// Note: This method does not emit change events
pub fn reload_ignore_defaults(&self) -> crate::Result<()> {
self.store.lock().unwrap().load_ignore_defaults()
}
/// Saves the store to disk at the store's `path`.
pub fn save(&self) -> crate::Result<()> {
if let Some(sender) = self.auto_save_debounce_sender.lock().unwrap().take() {
let _ = sender.send(AutoSaveMessage::Cancel);
}
self.store.lock().unwrap().save()
}
/// Removes the store from the resource table
pub fn close_resource(&self) {
let store = self.store.lock().unwrap();
let app = store.app.clone();
let state = app.state::<StoreState>();
let stores = state.stores.lock().unwrap();
if let Some(rid) = stores.get(&store.path).copied() {
drop(store);
drop(stores);
let _ = app.resources_table().close(rid);
}
}
fn trigger_auto_save(&self) -> crate::Result<()> {
let Some(auto_save_delay) = self.auto_save else {
return Ok(());
};
if auto_save_delay.is_zero() {
return self.save();
}
let mut auto_save_debounce_sender = self.auto_save_debounce_sender.lock().unwrap();
if let Some(ref sender) = *auto_save_debounce_sender {
let _ = sender.send(AutoSaveMessage::Reset);
return Ok(());
}
let (sender, mut receiver) = unbounded_channel();
auto_save_debounce_sender.replace(sender);
drop(auto_save_debounce_sender);
let store = self.store.clone();
let auto_save_debounce_sender = self.auto_save_debounce_sender.clone();
tauri::async_runtime::spawn(async move {
loop {
select! {
should_cancel = receiver.recv() => {
if matches!(should_cancel, Some(AutoSaveMessage::Cancel) | None) {
return;
}
}
_ = sleep(auto_save_delay) => {
auto_save_debounce_sender.lock().unwrap().take();
let _ = store.lock().unwrap().save();
return;
}
};
}
});
Ok(())
}
fn apply_pending_auto_save(&self) {
// Cancel and save if auto save is pending
if let Some(sender) = self.auto_save_debounce_sender.lock().unwrap().take() {
let _ = sender.send(AutoSaveMessage::Cancel);
let _ = self.save();
};
}
}
impl<R: Runtime> Drop for Store<R> {
fn drop(&mut self) {
self.apply_pending_auto_save();
}
}
|