use crate::storage::{ClientRemoteTabs, RemoteTab, TabsStorage};
use crate::{ApiResult, PendingCommand, RemoteCommand};
use std::path::Path;
use std::sync::{Arc, Mutex};
pub struct TabsStore {
pub storage: Mutex<TabsStorage>,
}
impl TabsStore {
pub fn new(db_path: impl AsRef<Path>) -> Self {
Self {
storage: Mutex::new(TabsStorage::new(db_path)),
}
}
pub fn new_with_mem_path(db_path: &str) -> Self {
Self {
storage: Mutex::new(TabsStorage::new_with_mem_path(db_path)),
}
}
pub fn close_connection(&self) {
self.storage.lock().unwrap().close()
}
pub fn set_local_tabs(&self, local_state: Vec<RemoteTab>) {
self.storage.lock().unwrap().update_local_state(local_state);
}
pub fn get_all(&self) -> Vec<ClientRemoteTabs> {
self.remote_tabs().unwrap_or_default()
}
pub fn remote_tabs(&self) -> Option<Vec<ClientRemoteTabs>> {
self.storage.lock().unwrap().get_remote_tabs()
}
pub fn new_remote_command_store(self: Arc<Self>) -> Arc<RemoteCommandStore> {
Arc::new(RemoteCommandStore {
store: Arc::clone(&self),
})
}
}
pub struct RemoteCommandStore {
store: Arc<TabsStore>,
}
impl RemoteCommandStore {
#[error_support::handle_error(crate::Error)]
pub fn add_remote_command(&self, device_id: &str, command: &RemoteCommand) -> ApiResult<bool> {
self.store
.storage
.lock()
.unwrap()
.add_remote_tab_command(device_id, command)
}
#[error_support::handle_error(crate::Error)]
pub fn add_remote_command_at(
&self,
device_id: &str,
command: &RemoteCommand,
when: types::Timestamp,
) -> ApiResult<bool> {
self.store
.storage
.lock()
.unwrap()
.add_remote_tab_command_at(device_id, command, when)
}
#[error_support::handle_error(crate::Error)]
pub fn remove_remote_command(
&self,
device_id: &str,
command: &RemoteCommand,
) -> ApiResult<bool> {
self.store
.storage
.lock()
.unwrap()
.remove_remote_tab_command(device_id, command)
}
#[error_support::handle_error(crate::Error)]
pub fn get_unsent_commands(&self) -> ApiResult<Vec<PendingCommand>> {
self.store.storage.lock().unwrap().get_unsent_commands()
}
#[error_support::handle_error(crate::Error)]
pub fn set_pending_command_sent(&self, command: &PendingCommand) -> ApiResult<bool> {
self.store
.storage
.lock()
.unwrap()
.set_pending_command_sent(command)
}
}