Move note store from gdn-app to gdn

This commit is contained in:
Joscha 2025-05-04 14:20:15 +02:00
parent 3e928f69dc
commit 3c42339291
5 changed files with 81 additions and 22 deletions

View file

@ -1,12 +1,9 @@
use std::sync::{Arc, Mutex};
use gdn::ids::NoteId;
use gdn::{ids::NoteId, store::Store};
use tauri::{AppHandle, Emitter, State};
use crate::{
store::Store,
types::{EventNotesStoreUpdate, Note},
};
use crate::types::{EventNotesStoreUpdate, Note};
// API methods are sorted alphabetically.
@ -76,7 +73,7 @@ pub fn note_create(text: String, app: AppHandle, store: State<'_, Arc<Mutex<Stor
let mut guard = store.lock().unwrap();
let id = guard.create(text);
update_if_required(&mut guard, &app);
guard.get(id).unwrap()
guard.get(id).unwrap().into()
}
#[tauri::command]
@ -89,7 +86,7 @@ pub fn note_delete(id: NoteId, app: AppHandle, store: State<'_, Arc<Mutex<Store>
#[tauri::command]
pub fn note_get(id: NoteId, store: State<'_, Arc<Mutex<Store>>>) -> Option<Note> {
let guard = store.lock().unwrap();
guard.get(id)
guard.get(id).map(|it| it.into())
}
#[tauri::command]

View file

@ -1,9 +1,9 @@
use serde_json as _; // Silence unused dependency warning
use std::sync::{Arc, Mutex};
use store::Store;
use gdn::store::Store;
mod api;
pub mod store;
mod types;
#[cfg_attr(mobile, tauri::mobile_entry_point)]

View file

@ -1,6 +1,6 @@
use std::collections::HashSet;
use gdn::ids::NoteId;
use gdn::{ids::NoteId, store::RichNote};
use serde::Serialize;
#[derive(Serialize)]
@ -12,6 +12,17 @@ pub struct Note {
pub parents: HashSet<NoteId>,
}
impl From<RichNote> for Note {
fn from(value: RichNote) -> Self {
Self {
id: value.id,
text: value.text,
children: value.children,
parents: value.parents,
}
}
}
////////////
// Events //
////////////

View file

@ -1,6 +1,7 @@
pub mod data;
pub mod ids;
pub mod repo;
pub mod store;
pub const PROPER_NAME: &str = "GedächtNAS";
pub const TECHNICAL_NAME: &str = "gedaechtnas";

View file

@ -1,21 +1,46 @@
use std::collections::{HashMap, HashSet};
use gdn::ids::NoteId;
use crate::{
ids::NoteId,
repo::{Note, Repo},
};
use crate::types::Note;
#[derive(Debug)]
pub struct NoteInfo {
#[derive(Clone)]
pub struct RawNote {
pub text: String,
pub children: Vec<NoteId>,
}
/// A note store for testing.
impl RawNote {
pub fn load(note: Note) -> Self {
Self {
text: note.text,
children: note.children,
}
}
pub fn save(self, id: NoteId) -> Note {
Note {
id,
text: self.text,
children: self.children,
}
}
}
#[derive(Clone)]
pub struct RichNote {
pub id: NoteId,
pub text: String,
pub children: Vec<NoteId>,
pub parents: HashSet<NoteId>,
}
#[derive(Default)]
pub struct Store {
last_id: u64,
curr_id: u64,
notes: HashMap<NoteId, NoteInfo>,
notes: HashMap<NoteId, RawNote>,
parents: HashMap<NoteId, HashMap<NoteId, usize>>,
}
@ -24,6 +49,31 @@ impl Store {
Self::default()
}
pub fn load(repo: Repo) -> Self {
let notes = repo
.notes
.into_iter()
.map(|note| (note.id, RawNote::load(note)))
.collect::<HashMap<_, _>>();
let mut result = Self {
notes,
..Self::default()
};
result.make_consistent_and_tick();
result
}
pub fn save(&self) -> Repo {
let notes = self
.notes
.iter()
.map(|(id, note)| note.clone().save(*id))
.collect::<Vec<_>>();
Repo { notes }
}
pub fn needs_update(&self) -> Option<u64> {
if self.last_id != self.curr_id {
Some(self.curr_id)
@ -36,7 +86,7 @@ impl Store {
self.last_id = self.curr_id;
}
pub fn get(&self, id: NoteId) -> Option<Note> {
pub fn get(&self, id: NoteId) -> Option<RichNote> {
let info = self.notes.get(&id)?;
let parents = self
@ -45,7 +95,7 @@ impl Store {
.map(|ps| ps.keys().copied().collect::<HashSet<_>>())
.unwrap_or_default();
Some(Note {
Some(RichNote {
id,
text: info.text.clone(),
children: info.children.clone(),
@ -82,18 +132,18 @@ impl Store {
pub fn create(&mut self, text: String) -> NoteId {
let id = NoteId::new();
let info = NoteInfo {
let note = RawNote {
text,
children: vec![],
};
self.notes.insert(id, info);
self.notes.insert(id, note);
self.make_consistent_and_tick();
id
}
pub fn delete(&mut self, id: NoteId) -> Option<NoteInfo> {
pub fn delete(&mut self, id: NoteId) -> Option<RawNote> {
let info = self.notes.remove(&id)?;
self.make_consistent_and_tick();
Some(info)