reorganize history memory model

This commit is contained in:
yggverse 2025-03-08 21:02:31 +02:00
parent 6526ca85d8
commit 268af30830
14 changed files with 172 additions and 218 deletions

View file

@ -1,13 +1,81 @@
mod request;
mod tab;
use request::Request;
use tab::Tab;
use super::Item;
use itertools::Itertools;
/// Reduce disk usage by cache Bookmarks index in memory
pub struct Memory {
pub request: Request,
pub tab: Tab,
pub struct Memory(Vec<Item>);
impl Memory {
// Constructors
/// Create new `Self`
pub fn new() -> Self {
Self(Vec::new())
}
// Actions
pub fn add(&mut self, item: Item) {
self.0.push(item)
}
/// Update `opened` time for given `request`
/// * return `false` if the `request` not found in memory index
pub fn open(&mut self, request: &str) -> bool {
for record in &mut self.0 {
if record.request == request {
record.open();
return true;
}
}
false
}
/// Update `closed` time for given `request`
pub fn close(&mut self, request: &str) {
for record in &mut self.0 {
if record.request == request {
record.close();
return;
}
}
}
// Getters
/// Get recent Items vector sorted by `closed` ASC
pub fn recently_closed(&self, limit: Option<usize>) -> Vec<Item> {
let mut recent: Vec<Item> = Vec::new();
for (i, item) in self
.0
.iter()
.filter(|x| x.closed.is_some())
.sorted_by(|a, b| Ord::cmp(&a.closed, &b.closed))
.enumerate()
{
if limit.is_some_and(|l| i > l) {
break;
}
recent.push(item.clone())
}
recent
}
/// Get recent Items vector sorted by `opened` ASC
pub fn recently_opened(&self, limit: Option<usize>) -> Vec<Item> {
let mut recent: Vec<Item> = Vec::new();
for (i, item) in self
.0
.iter()
.sorted_by(|a, b| Ord::cmp(&a.opened, &b.opened))
.enumerate()
{
if limit.is_some_and(|l| i > l) {
break;
}
recent.push(item.clone())
}
recent
}
}
impl Default for Memory {
@ -15,15 +83,3 @@ impl Default for Memory {
Self::new()
}
}
impl Memory {
// Constructors
/// Create new `Self`
pub fn new() -> Self {
Self {
request: Request::new(),
tab: Tab::new(),
}
}
}