AppFlowy/rust-lib/flowy-workspace/src/module.rs

98 lines
3.3 KiB
Rust
Raw Normal View History

use crate::{
2021-09-16 04:35:55 +00:00
errors::WorkspaceError,
event::WorkspaceEvent,
2021-09-01 14:50:22 +00:00
handlers::*,
services::{server::construct_workspace_server, AppController, ViewController, WorkspaceController},
2021-09-01 14:50:22 +00:00
};
2021-09-20 07:38:55 +00:00
2021-10-13 03:10:29 +00:00
use crate::services::TrashCan;
use flowy_database::DBConnection;
use flowy_dispatch::prelude::*;
2021-09-13 15:09:57 +00:00
use flowy_document::module::FlowyDocument;
2021-09-27 15:23:23 +00:00
use flowy_net::config::ServerConfig;
use flowy_sqlite::ConnectionPool;
2021-07-14 00:07:25 +00:00
use std::sync::Arc;
2021-07-20 06:03:21 +00:00
pub trait WorkspaceDeps: WorkspaceUser + WorkspaceDatabase {}
pub trait WorkspaceUser: Send + Sync {
2021-07-26 05:51:41 +00:00
fn user_id(&self) -> Result<String, WorkspaceError>;
2021-09-01 14:50:22 +00:00
fn token(&self) -> Result<String, WorkspaceError>;
2021-07-20 06:03:21 +00:00
}
pub trait WorkspaceDatabase: Send + Sync {
fn db_pool(&self) -> Result<Arc<ConnectionPool>, WorkspaceError>;
fn db_connection(&self) -> Result<DBConnection, WorkspaceError> {
let pool = self.db_pool()?;
2021-09-16 04:35:55 +00:00
let conn = pool.get().map_err(|e| WorkspaceError::internal().context(e))?;
Ok(conn)
}
}
2021-10-05 06:37:45 +00:00
pub fn mk_workspace(
2021-09-27 15:23:23 +00:00
user: Arc<dyn WorkspaceUser>,
database: Arc<dyn WorkspaceDatabase>,
flowy_document: Arc<FlowyDocument>,
server_config: &ServerConfig,
2021-10-05 06:37:45 +00:00
) -> Arc<WorkspaceController> {
2021-09-27 15:23:23 +00:00
let server = construct_workspace_server(server_config);
2021-10-05 06:37:45 +00:00
2021-10-13 03:10:29 +00:00
let trash_can = Arc::new(TrashCan::new(database.clone()));
2021-10-12 14:31:38 +00:00
2021-09-27 15:23:23 +00:00
let view_controller = Arc::new(ViewController::new(
user.clone(),
database.clone(),
server.clone(),
2021-10-13 03:10:29 +00:00
trash_can.clone(),
2021-09-27 15:23:23 +00:00
flowy_document,
));
2021-10-05 06:37:45 +00:00
let app_controller = Arc::new(AppController::new(user.clone(), database.clone(), server.clone()));
2021-07-20 06:03:21 +00:00
let workspace_controller = Arc::new(WorkspaceController::new(
user.clone(),
database.clone(),
app_controller.clone(),
view_controller.clone(),
2021-10-13 03:10:29 +00:00
trash_can.clone(),
2021-09-01 14:50:22 +00:00
server.clone(),
2021-07-20 06:03:21 +00:00
));
2021-10-05 06:37:45 +00:00
workspace_controller
}
2021-10-05 06:37:45 +00:00
pub fn create(workspace: Arc<WorkspaceController>) -> Module {
let mut module = Module::new()
2021-07-14 00:07:25 +00:00
.name("Flowy-Workspace")
2021-10-05 06:37:45 +00:00
.data(workspace.clone())
.data(workspace.app_controller.clone())
.data(workspace.view_controller.clone())
2021-10-13 03:10:29 +00:00
.data(workspace.trash_can.clone())
2021-10-05 06:37:45 +00:00
.event(WorkspaceEvent::InitWorkspace, init_workspace_handler);
module = module
2021-09-05 05:50:23 +00:00
.event(WorkspaceEvent::CreateWorkspace, create_workspace_handler)
.event(WorkspaceEvent::ReadCurWorkspace, read_cur_workspace_handler)
.event(WorkspaceEvent::ReadWorkspaces, read_workspaces_handler)
2021-09-07 09:12:03 +00:00
.event(WorkspaceEvent::OpenWorkspace, open_workspace_handler)
.event(WorkspaceEvent::ReadWorkspaceApps, read_workspace_apps_handler);
module = module
2021-09-05 05:50:23 +00:00
.event(WorkspaceEvent::CreateApp, create_app_handler)
.event(WorkspaceEvent::ReadApp, read_app_handler)
.event(WorkspaceEvent::UpdateApp, update_app_handler)
.event(WorkspaceEvent::DeleteApp, delete_app_handler);
module = module
2021-09-05 05:50:23 +00:00
.event(WorkspaceEvent::CreateView, create_view_handler)
.event(WorkspaceEvent::ReadView, read_view_handler)
.event(WorkspaceEvent::UpdateView, update_view_handler)
2021-09-11 06:26:30 +00:00
.event(WorkspaceEvent::DeleteView, delete_view_handler)
.event(WorkspaceEvent::OpenView, open_view_handler)
2021-09-23 05:15:35 +00:00
.event(WorkspaceEvent::ApplyDocDelta, apply_doc_delta_handler);
2021-10-12 14:31:38 +00:00
module = module.event(WorkspaceEvent::ReadTrash, read_trash_handler);
module
2021-07-14 00:07:25 +00:00
}