Skip to content

Commit

Permalink
refactor wip
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Oct 21, 2024
1 parent 93fa5a3 commit 79884c1
Show file tree
Hide file tree
Showing 16 changed files with 465 additions and 522 deletions.
50 changes: 28 additions & 22 deletions src/actionbar.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
note::NoteRef,
notecache::NoteCache,
notes_holder::{NotesHolder, NotesHolderStorage},
route::{Route, Router},
thread::Thread,

Check failure on line 5 in src/actionbar.rs

View workflow job for this annotation

GitHub Actions / Clippy

unresolved import `crate::thread`

Check failure on line 5 in src/actionbar.rs

View workflow job for this annotation

GitHub Actions / Clippy

unresolved import `crate::thread`

Check failure on line 5 in src/actionbar.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unresolved import `crate::thread`

Check failure on line 5 in src/actionbar.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unresolved import `crate::thread`
timeline::{TimelineCache, CachedTimeline},
};
use enostr::{NoteId, Pubkey, RelayPool};
use nostrdb::{Ndb, Transaction};
Expand All @@ -26,7 +26,7 @@ pub struct NewNotes {
pub notes: Vec<NoteRef>,
}

pub enum NotesHolderResult {
pub enum BarResult {
NewNotes(NewNotes),
}

Expand All @@ -41,13 +41,13 @@ fn open_thread(
router: &mut Router<Route>,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
threads: &mut NotesHolderStorage<Thread>,
timeline_cache: &mut TimelineCache,
selected_note: &[u8; 32],
) -> Option<NotesHolderResult> {
) -> Option<BarResult> {
router.route_to(Route::thread(NoteId::new(selected_note.to_owned())));

let root_id = crate::note::root_note_id_from_selected_id(ndb, note_cache, txn, selected_note);
Thread::open(ndb, note_cache, txn, pool, threads, root_id)
Thread::open(ndb, note_cache, txn, pool, timeline_cache, root_id)
}

impl BarAction {
Expand All @@ -56,21 +56,27 @@ impl BarAction {
self,
ndb: &Ndb,
router: &mut Router<Route>,
threads: &mut NotesHolderStorage<Thread>,
timeline_cache: &mut TimelineCache,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
txn: &Transaction,
) -> Option<NotesHolderResult> {
) -> Option<BarResult> {
match self {
BarAction::Reply(note_id) => {
router.route_to(Route::reply(note_id));
router.navigating = true;
None
}

BarAction::OpenThread(note_id) => {
open_thread(ndb, txn, router, note_cache, pool, threads, note_id.bytes())
}
BarAction::OpenThread(note_id) => open_thread(
ndb,
txn,
router,
note_cache,
pool,
timeline_cache,
note_id.bytes(),
),

BarAction::Quote(note_id) => {
router.route_to(Route::quote(note_id));
Expand All @@ -85,36 +91,36 @@ impl BarAction {
self,
ndb: &Ndb,
router: &mut Router<Route>,
threads: &mut NotesHolderStorage<Thread>,
timeline_cache: &mut TimelineCache,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
txn: &Transaction,
) {
if let Some(br) = self.execute(ndb, router, threads, note_cache, pool, txn) {
br.process(ndb, note_cache, txn, threads);
if let Some(br) = self.execute(ndb, router, timeline_cache, note_cache, pool, txn) {
br.process(ndb, note_cache, txn, timeline_cache);
}
}
}

impl NotesHolderResult {
impl BarResult {
pub fn new_notes(notes: Vec<NoteRef>, id: [u8; 32]) -> Self {
NotesHolderResult::NewNotes(NewNotes::new(notes, id))
Self::NewNotes(NewNotes::new(notes, id))
}

pub fn process<N: NotesHolder>(
pub fn process(
&self,
ndb: &Ndb,
note_cache: &mut NoteCache,
txn: &Transaction,
storage: &mut NotesHolderStorage<N>,
timeline_cache: &mut TimelineCache,
) {
match self {
// update the thread for next render if we have new notes
NotesHolderResult::NewNotes(new_notes) => {
let holder = storage
.notes_holder_mutated(ndb, note_cache, txn, &new_notes.id)
Self::NewNotes(new_notes) => {
let notes = timeline_cache
.notes(ndb, note_cache, txn, &new_notes.id)
.get_ptr();
new_notes.process(holder);
new_notes.process(notes);
}
}
}
Expand All @@ -127,7 +133,7 @@ impl NewNotes {

/// Simple helper for processing a NewThreadNotes result. It simply
/// inserts/merges the notes into the thread cache
pub fn process<N: NotesHolder>(&self, thread: &mut N) {
pub fn process(&self, thread: &mut CachedTimeline) {
// threads are chronological, ie reversed from reverse-chronological, the default.
let reversed = true;
thread.get_view().insert(&self.notes, reversed);
Expand Down
53 changes: 9 additions & 44 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ use crate::{
imgcache::ImageCache,
key_storage::KeyStorageType,
nav,
ui::{self, DesktopSidePanel},
note::NoteRef,
notecache::{CachedNote, NoteCache},
notes_holder::NotesHolderStorage,
profile::Profile,
subscriptions::{SubKind, Subscriptions},
thread::Thread,
timeline::{Timeline, TimelineId, TimelineKind, ViewFilter},
ui::{self, DesktopSidePanel},
timeline::{Timeline, TimelineCache, TimelineId, TimelineKind, ViewFilter},
unknowns::UnknownIds,
view_state::ViewState,
Result,
Expand All @@ -41,7 +38,6 @@ use tracing::{debug, error, info, trace, warn};
pub enum DamusState {
Initializing,
Initialized,
NewTimelineSub(TimelineId),
}

/// We derive Deserialize/Serialize so we can persist app state on shutdown.
Expand All @@ -55,8 +51,7 @@ pub struct Damus {
pub view_state: ViewState,
pub unknown_ids: UnknownIds,
pub drafts: Drafts,
pub threads: NotesHolderStorage<Thread>,
pub profiles: NotesHolderStorage<Profile>,
pub timeline_cache: TimelineCache,
pub img_cache: ImageCache,
pub accounts: AccountManager,
pub subscriptions: Subscriptions,
Expand Down Expand Up @@ -151,6 +146,7 @@ fn send_initial_timeline_filter(
//let sub_id = damus.gen_subid(&SubKind::Initial);
let sub_id = Uuid::new_v4().to_string();
subs.subs.insert(sub_id.clone(), SubKind::Initial);
timeline.remote_subid = Some(sub_id.clone());

let cmd = ClientMessage::req(sub_id, new_filters);
pool.send_to(&cmd, to);
Expand Down Expand Up @@ -473,33 +469,6 @@ fn update_damus(damus: &mut Damus, ctx: &egui::Context) {
.expect("home subscription failed");
}

DamusState::NewTimelineSub(new_timeline_id) => {
info!("adding new timeline {}", new_timeline_id);
setup_new_nostrdb_sub(
&damus.ndb,
&mut damus.note_cache,
&mut damus.columns,
new_timeline_id,
)
.expect("new timeline subscription failed");

if let Some(filter) = {
let timeline = damus
.columns
.find_timeline(new_timeline_id)
.expect("timeline");
match &timeline.filter {
FilterState::Ready(filters) => Some(filters.clone()),
_ => None,
}
} {
let subid = Uuid::new_v4().to_string();
damus.pool.subscribe(subid, filter);

damus.state = DamusState::Initialized;
}
}

DamusState::Initialized => (),
};

Expand Down Expand Up @@ -725,8 +694,7 @@ impl Damus {
unknown_ids: UnknownIds::default(),
subscriptions: Subscriptions::default(),
since_optimize: parsed_args.since_optimize,
threads: NotesHolderStorage::default(),
profiles: NotesHolderStorage::default(),
timeline_cache: TimelineCache::default(),
drafts: Drafts::default(),
state: DamusState::Initializing,
img_cache: ImageCache::new(imgcache_dir.into()),
Expand Down Expand Up @@ -784,10 +752,6 @@ impl Damus {
}
}

pub fn subscribe_new_timeline(&mut self, timeline_id: TimelineId) {
self.state = DamusState::NewTimelineSub(timeline_id);
}

pub fn mock<P: AsRef<Path>>(data_path: P) -> Self {
let mut columns = Columns::new();
let filter = Filter::from_json(include_str!("../queries/global.json")).unwrap();
Expand All @@ -807,8 +771,7 @@ impl Damus {
unknown_ids: UnknownIds::default(),
subscriptions: Subscriptions::default(),
since_optimize: true,
threads: NotesHolderStorage::default(),
profiles: NotesHolderStorage::default(),
timeline_cache: TimelineCache::default(),
drafts: Drafts::default(),
state: DamusState::Initializing,
pool: RelayPool::new(),
Expand All @@ -827,6 +790,7 @@ impl Damus {
&mut self.subscriptions.subs
}

/*
pub fn note_cache_mut(&mut self) -> &mut NoteCache {
&mut self.note_cache
}
Expand All @@ -839,13 +803,14 @@ impl Damus {
&self.threads
}
pub fn threads_mut(&mut self) -> &mut NotesHolderStorage<Thread> {
pub fn timeline_cache_mut(&mut self) -> &mut TimelineCache {
&mut self.threads
}
pub fn note_cache(&self) -> &NoteCache {
&self.note_cache
}
*/
}

/*
Expand Down
Loading

0 comments on commit 79884c1

Please sign in to comment.