Move note store from gdn-app to gdn
This commit is contained in:
parent
3e928f69dc
commit
3c42339291
5 changed files with 81 additions and 22 deletions
|
|
@ -1,12 +1,9 @@
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use gdn::ids::NoteId;
|
use gdn::{ids::NoteId, store::Store};
|
||||||
use tauri::{AppHandle, Emitter, State};
|
use tauri::{AppHandle, Emitter, State};
|
||||||
|
|
||||||
use crate::{
|
use crate::types::{EventNotesStoreUpdate, Note};
|
||||||
store::Store,
|
|
||||||
types::{EventNotesStoreUpdate, Note},
|
|
||||||
};
|
|
||||||
|
|
||||||
// API methods are sorted alphabetically.
|
// 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 mut guard = store.lock().unwrap();
|
||||||
let id = guard.create(text);
|
let id = guard.create(text);
|
||||||
update_if_required(&mut guard, &app);
|
update_if_required(&mut guard, &app);
|
||||||
guard.get(id).unwrap()
|
guard.get(id).unwrap().into()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
|
|
@ -89,7 +86,7 @@ pub fn note_delete(id: NoteId, app: AppHandle, store: State<'_, Arc<Mutex<Store>
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn note_get(id: NoteId, store: State<'_, Arc<Mutex<Store>>>) -> Option<Note> {
|
pub fn note_get(id: NoteId, store: State<'_, Arc<Mutex<Store>>>) -> Option<Note> {
|
||||||
let guard = store.lock().unwrap();
|
let guard = store.lock().unwrap();
|
||||||
guard.get(id)
|
guard.get(id).map(|it| it.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
|
use serde_json as _; // Silence unused dependency warning
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use store::Store;
|
use gdn::store::Store;
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
pub mod store;
|
|
||||||
mod types;
|
mod types;
|
||||||
|
|
||||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use gdn::ids::NoteId;
|
use gdn::{ids::NoteId, store::RichNote};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
|
@ -12,6 +12,17 @@ pub struct Note {
|
||||||
pub parents: HashSet<NoteId>,
|
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 //
|
// Events //
|
||||||
////////////
|
////////////
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
pub mod data;
|
pub mod data;
|
||||||
pub mod ids;
|
pub mod ids;
|
||||||
pub mod repo;
|
pub mod repo;
|
||||||
|
pub mod store;
|
||||||
|
|
||||||
pub const PROPER_NAME: &str = "GedächtNAS";
|
pub const PROPER_NAME: &str = "GedächtNAS";
|
||||||
pub const TECHNICAL_NAME: &str = "gedaechtnas";
|
pub const TECHNICAL_NAME: &str = "gedaechtnas";
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,46 @@
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
use gdn::ids::NoteId;
|
use crate::{
|
||||||
|
ids::NoteId,
|
||||||
|
repo::{Note, Repo},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::types::Note;
|
#[derive(Clone)]
|
||||||
|
pub struct RawNote {
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct NoteInfo {
|
|
||||||
pub text: String,
|
pub text: String,
|
||||||
pub children: Vec<NoteId>,
|
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)]
|
#[derive(Default)]
|
||||||
pub struct Store {
|
pub struct Store {
|
||||||
last_id: u64,
|
last_id: u64,
|
||||||
curr_id: u64,
|
curr_id: u64,
|
||||||
notes: HashMap<NoteId, NoteInfo>,
|
notes: HashMap<NoteId, RawNote>,
|
||||||
parents: HashMap<NoteId, HashMap<NoteId, usize>>,
|
parents: HashMap<NoteId, HashMap<NoteId, usize>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -24,6 +49,31 @@ impl Store {
|
||||||
Self::default()
|
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> {
|
pub fn needs_update(&self) -> Option<u64> {
|
||||||
if self.last_id != self.curr_id {
|
if self.last_id != self.curr_id {
|
||||||
Some(self.curr_id)
|
Some(self.curr_id)
|
||||||
|
|
@ -36,7 +86,7 @@ impl Store {
|
||||||
self.last_id = self.curr_id;
|
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 info = self.notes.get(&id)?;
|
||||||
|
|
||||||
let parents = self
|
let parents = self
|
||||||
|
|
@ -45,7 +95,7 @@ impl Store {
|
||||||
.map(|ps| ps.keys().copied().collect::<HashSet<_>>())
|
.map(|ps| ps.keys().copied().collect::<HashSet<_>>())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
Some(Note {
|
Some(RichNote {
|
||||||
id,
|
id,
|
||||||
text: info.text.clone(),
|
text: info.text.clone(),
|
||||||
children: info.children.clone(),
|
children: info.children.clone(),
|
||||||
|
|
@ -82,18 +132,18 @@ impl Store {
|
||||||
|
|
||||||
pub fn create(&mut self, text: String) -> NoteId {
|
pub fn create(&mut self, text: String) -> NoteId {
|
||||||
let id = NoteId::new();
|
let id = NoteId::new();
|
||||||
let info = NoteInfo {
|
let note = RawNote {
|
||||||
text,
|
text,
|
||||||
children: vec![],
|
children: vec![],
|
||||||
};
|
};
|
||||||
|
|
||||||
self.notes.insert(id, info);
|
self.notes.insert(id, note);
|
||||||
self.make_consistent_and_tick();
|
self.make_consistent_and_tick();
|
||||||
|
|
||||||
id
|
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)?;
|
let info = self.notes.remove(&id)?;
|
||||||
self.make_consistent_and_tick();
|
self.make_consistent_and_tick();
|
||||||
Some(info)
|
Some(info)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue