use gpui::{App, Task};
use std::path::PathBuf;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DataStore {
pub(crate) incognito: bool,
pub(crate) directory: Option<PathBuf>,
pub(crate) identifier: Option<[u8; 16]>,
}
impl DataStore {
pub fn new() -> Self {
Self::default()
}
pub fn incognito(mut self) -> Self {
self.incognito = true;
self
}
pub fn with_directory(mut self, directory: impl Into<PathBuf>) -> Self {
self.directory = Some(directory.into());
self
}
pub fn with_identifier(mut self, identifier: [u8; 16]) -> Self {
self.identifier = Some(identifier);
self
}
pub fn clear(&self, cx: &App) -> Task<bool> {
if self.incognito {
return Task::ready(true);
}
let (cleared, answer) = async_channel::bounded(1);
crate::page::clear_store(self, move |ok| {
let _ = cleared.try_send(ok);
});
cx.foreground_executor()
.spawn(async move { answer.recv().await.unwrap_or(false) })
}
pub fn usage(&self, cx: &App) -> Task<Option<Usage>> {
if self.incognito {
return Task::ready(Some(Usage::default()));
}
let (reported, answer) = async_channel::bounded(1);
crate::page::store_usage(self, move |usage| {
let _ = reported.try_send(usage);
});
cx.foreground_executor()
.spawn(async move { answer.recv().await.ok().flatten() })
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Usage {
pub sites: Vec<String>,
}