1use crate::storage::{ClientRemoteTabs, RemoteTab, TabsStorage};
6use crate::{ApiResult, PendingCommand, RemoteCommand};
7use std::path::Path;
8use std::sync::{Arc, Mutex};
9
10pub struct TabsStore {
11 pub storage: Mutex<TabsStorage>,
12}
13
14impl TabsStore {
15 pub fn new(db_path: impl AsRef<Path>) -> Self {
16 Self {
17 storage: Mutex::new(TabsStorage::new(db_path)),
18 }
19 }
20
21 pub fn new_with_mem_path(db_path: &str) -> Self {
22 Self {
23 storage: Mutex::new(TabsStorage::new_with_mem_path(db_path)),
24 }
25 }
26
27 pub fn close_connection(&self) {
31 self.storage.lock().unwrap().close()
32 }
33
34 pub fn set_local_tabs(&self, local_state: Vec<RemoteTab>) {
35 self.storage.lock().unwrap().update_local_state(local_state);
36 }
37
38 pub fn get_all(&self) -> Vec<ClientRemoteTabs> {
40 self.remote_tabs().unwrap_or_default()
41 }
42
43 pub fn remote_tabs(&self) -> Option<Vec<ClientRemoteTabs>> {
44 self.storage.lock().unwrap().get_remote_tabs()
45 }
46
47 pub fn new_remote_command_store(self: Arc<Self>) -> Arc<RemoteCommandStore> {
48 Arc::new(RemoteCommandStore {
49 store: Arc::clone(&self),
50 })
51 }
52}
53
54pub struct RemoteCommandStore {
55 store: Arc<TabsStore>,
57}
58
59impl RemoteCommandStore {
60 #[error_support::handle_error(crate::Error)]
70 pub fn add_remote_command(&self, device_id: &str, command: &RemoteCommand) -> ApiResult<bool> {
71 self.store
72 .storage
73 .lock()
74 .unwrap()
75 .add_remote_tab_command(device_id, command)
76 }
77
78 #[error_support::handle_error(crate::Error)]
79 pub fn add_remote_command_at(
80 &self,
81 device_id: &str,
82 command: &RemoteCommand,
83 when: types::Timestamp,
84 ) -> ApiResult<bool> {
85 self.store
86 .storage
87 .lock()
88 .unwrap()
89 .add_remote_tab_command_at(device_id, command, when)
90 }
91
92 #[error_support::handle_error(crate::Error)]
94 pub fn remove_remote_command(
95 &self,
96 device_id: &str,
97 command: &RemoteCommand,
98 ) -> ApiResult<bool> {
99 self.store
100 .storage
101 .lock()
102 .unwrap()
103 .remove_remote_tab_command(device_id, command)
104 }
105
106 #[error_support::handle_error(crate::Error)]
107 pub fn get_unsent_commands(&self) -> ApiResult<Vec<PendingCommand>> {
108 self.store.storage.lock().unwrap().get_unsent_commands()
109 }
110
111 #[error_support::handle_error(crate::Error)]
112 pub fn set_pending_command_sent(&self, command: &PendingCommand) -> ApiResult<bool> {
113 self.store
114 .storage
115 .lock()
116 .unwrap()
117 .set_pending_command_sent(command)
118 }
119}