From 46f5bc38b8aab6228e16e0be83b2947e50617023 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 26 May 2025 23:45:58 +0200 Subject: [PATCH] Extract store invalidation detection into separate AppState --- gdn-app/src-tauri/src/api.rs | 84 ++++++++++++++++++++-------------- gdn-app/src-tauri/src/lib.rs | 9 +++- gdn-app/src-tauri/src/state.rs | 15 ++++++ gdn/src/store.rs | 17 ++----- 4 files changed, 75 insertions(+), 50 deletions(-) create mode 100644 gdn-app/src-tauri/src/state.rs diff --git a/gdn-app/src-tauri/src/api.rs b/gdn-app/src-tauri/src/api.rs index 5417fff..4af2d1a 100644 --- a/gdn-app/src-tauri/src/api.rs +++ b/gdn-app/src-tauri/src/api.rs @@ -1,18 +1,25 @@ use std::sync::{Arc, Mutex}; -use gdn::{ids::NoteId, store::Store}; +use gdn::ids::NoteId; use tauri::{AppHandle, Emitter, State}; -use crate::types::{EventNotesStoreUpdate, Note}; +use crate::{ + state::AppState, + types::{EventNotesStoreUpdate, Note}, +}; // API methods are sorted alphabetically. -fn update_if_required(store: &mut Store, app: &AppHandle) { - if let Some(store_id) = store.needs_update() { - let payload = EventNotesStoreUpdate { store_id }; - app.emit("notes_store_update", payload).unwrap(); - store.update(); +fn update_if_required(state: &mut AppState, app: &AppHandle) { + let store_id = state.store.id(); + if state.store_last_id == Some(store_id) { + // No update necessary if the id hasn't changed + return; } + + let payload = EventNotesStoreUpdate { store_id }; + app.emit("notes_store_update", payload).unwrap(); + state.store_last_id = Some(store_id) } #[tauri::command] @@ -21,10 +28,12 @@ pub fn note_child_add( child_id: NoteId, child_position: isize, app: AppHandle, - store: State<'_, Arc>>, + state: State<'_, Arc>>, ) { - let mut guard = store.lock().unwrap(); - guard.add_child_at_position(id, child_id, child_position); + let mut guard = state.lock().unwrap(); + guard + .store + .add_child_at_position(id, child_id, child_position); update_if_required(&mut guard, &app); } @@ -36,10 +45,12 @@ pub fn note_child_move( to_id: NoteId, to_position: isize, app: AppHandle, - store: State<'_, Arc>>, + state: State<'_, Arc>>, ) { - let mut guard = store.lock().unwrap(); - guard.move_child_by_id_to_position(child_id, from_id, from_iteration, to_id, to_position); + let mut guard = state.lock().unwrap(); + guard + .store + .move_child_by_id_to_position(child_id, from_id, from_iteration, to_id, to_position); update_if_required(&mut guard, &app); } @@ -49,10 +60,12 @@ pub fn note_child_remove( child_id: NoteId, child_iteration: usize, app: AppHandle, - store: State<'_, Arc>>, + state: State<'_, Arc>>, ) { - let mut guard = store.lock().unwrap(); - guard.remove_child_by_id(id, child_id, child_iteration); + let mut guard = state.lock().unwrap(); + guard + .store + .remove_child_by_id(id, child_id, child_iteration); update_if_required(&mut guard, &app); } @@ -61,32 +74,33 @@ pub fn note_children_set( id: NoteId, children: Vec, app: AppHandle, - store: State<'_, Arc>>, + state: State<'_, Arc>>, ) { - let mut guard = store.lock().unwrap(); - guard.set_children(id, children); + let mut guard = state.lock().unwrap(); + guard.store.set_children(id, children); update_if_required(&mut guard, &app); } #[tauri::command] -pub fn note_create(text: String, app: AppHandle, store: State<'_, Arc>>) -> Note { - let mut guard = store.lock().unwrap(); - let id = guard.create(text); +pub fn note_create(text: String, app: AppHandle, state: State<'_, Arc>>) -> Note { + let mut guard = state.lock().unwrap(); + let id = guard.store.create(text); + let note = guard.store.get(id).unwrap().into(); update_if_required(&mut guard, &app); - guard.get(id).unwrap().into() + note } #[tauri::command] -pub fn note_delete(id: NoteId, app: AppHandle, store: State<'_, Arc>>) { - let mut guard = store.lock().unwrap(); - guard.delete(id); +pub fn note_delete(id: NoteId, app: AppHandle, state: State<'_, Arc>>) { + let mut guard = state.lock().unwrap(); + guard.store.delete(id); update_if_required(&mut guard, &app); } #[tauri::command] -pub fn note_get(id: NoteId, store: State<'_, Arc>>) -> Option { - let guard = store.lock().unwrap(); - guard.get(id).map(|it| it.into()) +pub fn note_get(id: NoteId, state: State<'_, Arc>>) -> Option { + let guard = state.lock().unwrap(); + guard.store.get(id).map(|it| it.into()) } #[tauri::command] @@ -94,16 +108,16 @@ pub fn note_text_set( id: NoteId, text: String, app: AppHandle, - store: State<'_, Arc>>, + state: State<'_, Arc>>, ) { - let mut guard = store.lock().unwrap(); - guard.set_text(id, text); + let mut guard = state.lock().unwrap(); + guard.store.set_text(id, text); update_if_required(&mut guard, &app); } #[tauri::command] -pub fn notes_clear(app: AppHandle, store: State<'_, Arc>>) { - let mut guard = store.lock().unwrap(); - guard.clear(); +pub fn notes_clear(app: AppHandle, state: State<'_, Arc>>) { + let mut guard = state.lock().unwrap(); + guard.store.clear(); update_if_required(&mut guard, &app); } diff --git a/gdn-app/src-tauri/src/lib.rs b/gdn-app/src-tauri/src/lib.rs index b72fc08..bc0c323 100644 --- a/gdn-app/src-tauri/src/lib.rs +++ b/gdn-app/src-tauri/src/lib.rs @@ -1,16 +1,21 @@ use serde_json as _; // Silence unused dependency warning use std::sync::{Arc, Mutex}; -use gdn::store::Store; +use crate::state::AppState; mod api; +mod state; mod types; #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { + let state = Arc::new(Mutex::new(AppState::new())); + + // TODO Launch store loading task + tauri::Builder::default() .plugin(tauri_plugin_opener::init()) - .manage(Arc::new(Mutex::new(Store::new()))) + .manage(state) .invoke_handler(tauri::generate_handler![ api::note_child_add, api::note_child_move, diff --git a/gdn-app/src-tauri/src/state.rs b/gdn-app/src-tauri/src/state.rs new file mode 100644 index 0000000..68c5d61 --- /dev/null +++ b/gdn-app/src-tauri/src/state.rs @@ -0,0 +1,15 @@ +use gdn::store::Store; + +pub struct AppState { + pub store: Store, + pub store_last_id: Option, +} + +impl AppState { + pub fn new() -> Self { + Self { + store: Store::new(), + store_last_id: None, + } + } +} diff --git a/gdn/src/store.rs b/gdn/src/store.rs index 2882c92..65c4291 100644 --- a/gdn/src/store.rs +++ b/gdn/src/store.rs @@ -38,8 +38,7 @@ pub struct RichNote { #[derive(Default)] pub struct Store { - last_id: u64, - curr_id: u64, + id: u64, notes: HashMap, parents: HashMap>, } @@ -74,16 +73,8 @@ impl Store { Repo { notes } } - pub fn needs_update(&self) -> Option { - if self.last_id != self.curr_id { - Some(self.curr_id) - } else { - None - } - } - - pub fn update(&mut self) { - self.last_id = self.curr_id; + pub fn id(&self) -> u64 { + self.id } pub fn get(&self, id: NoteId) -> Option { @@ -104,7 +95,7 @@ impl Store { } fn tick(&mut self) { - self.curr_id += 1; + self.id += 1; } fn make_consistent_and_tick(&mut self) {