wip: refactor
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
};
|
||||
|
||||
pub struct MetadataRegistry {
|
||||
seens: Vec<PublicKey>,
|
||||
seens: Arc<Mutex<Vec<PublicKey>>>,
|
||||
profiles: Arc<RwLock<HashMap<PublicKey, Metadata>>>,
|
||||
}
|
||||
|
||||
impl Global for MetadataRegistry {}
|
||||
@@ -13,16 +18,31 @@ impl MetadataRegistry {
|
||||
}
|
||||
|
||||
pub fn contains(&self, public_key: PublicKey) -> bool {
|
||||
self.seens.contains(&public_key)
|
||||
self.seens.lock().unwrap().contains(&public_key)
|
||||
}
|
||||
|
||||
pub fn seen(&mut self, public_key: PublicKey) {
|
||||
if !self.seens.contains(&public_key) {
|
||||
self.seens.push(public_key);
|
||||
pub fn seen(&mut self, public_key: PublicKey, metadata: Option<Metadata>) {
|
||||
let mut seens = self.seens.lock().unwrap();
|
||||
|
||||
if !seens.contains(&public_key) {
|
||||
seens.push(public_key);
|
||||
|
||||
drop(seens);
|
||||
|
||||
if let Some(metadata) = metadata {
|
||||
self.profiles.write().unwrap().insert(public_key, metadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, public_key: PublicKey) -> Option<Metadata> {
|
||||
self.profiles.read().unwrap().get(&public_key).cloned()
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
Self { seens: Vec::new() }
|
||||
let seens = Arc::new(Mutex::new(Vec::new()));
|
||||
let profiles = Arc::new(RwLock::new(HashMap::new()));
|
||||
|
||||
Self { seens, profiles }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user