2021-07-19 08:15:20 +00:00
|
|
|
mod deps_resolve;
|
2021-08-20 06:38:03 +00:00
|
|
|
// mod flowy_server;
|
2021-06-30 15:11:27 +00:00
|
|
|
pub mod module;
|
2021-10-05 09:54:11 +00:00
|
|
|
use crate::deps_resolve::WorkspaceDepsResolver;
|
2021-12-05 08:39:41 +00:00
|
|
|
use backend_service::configuration::ClientServerConfiguration;
|
2021-12-06 13:47:21 +00:00
|
|
|
use flowy_core::{errors::WorkspaceError, module::init_core, prelude::CoreContext};
|
2021-11-13 03:11:24 +00:00
|
|
|
use flowy_document::module::FlowyDocument;
|
2021-12-04 15:54:14 +00:00
|
|
|
use flowy_user::{
|
|
|
|
prelude::UserStatus,
|
|
|
|
services::user::{UserSession, UserSessionConfig},
|
|
|
|
};
|
2021-11-19 06:38:11 +00:00
|
|
|
use lib_dispatch::prelude::*;
|
2021-12-05 06:04:25 +00:00
|
|
|
use lib_infra::entities::network_state::NetworkType;
|
2021-10-05 06:37:45 +00:00
|
|
|
use module::mk_modules;
|
2021-07-14 13:12:52 +00:00
|
|
|
pub use module::*;
|
2021-09-04 07:12:53 +00:00
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
};
|
2021-11-08 15:15:29 +00:00
|
|
|
use tokio::sync::broadcast;
|
2021-07-09 15:31:44 +00:00
|
|
|
|
2021-07-13 05:14:49 +00:00
|
|
|
static INIT_LOG: AtomicBool = AtomicBool::new(false);
|
2021-09-05 05:50:23 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct FlowySDKConfig {
|
2021-11-09 09:50:32 +00:00
|
|
|
name: String,
|
2021-09-05 05:50:23 +00:00
|
|
|
root: String,
|
|
|
|
log_filter: String,
|
2021-12-05 08:39:41 +00:00
|
|
|
server_config: ClientServerConfiguration,
|
2021-09-05 05:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FlowySDKConfig {
|
2021-12-05 08:39:41 +00:00
|
|
|
pub fn new(root: &str, server_config: ClientServerConfiguration, name: &str) -> Self {
|
2021-09-05 05:50:23 +00:00
|
|
|
FlowySDKConfig {
|
2021-11-09 09:50:32 +00:00
|
|
|
name: name.to_owned(),
|
2021-09-05 05:50:23 +00:00
|
|
|
root: root.to_owned(),
|
2021-09-07 09:12:03 +00:00
|
|
|
log_filter: crate_log_filter(None),
|
2021-09-27 15:23:23 +00:00
|
|
|
server_config,
|
2021-09-05 05:50:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log_filter(mut self, filter: &str) -> Self {
|
2021-09-07 09:12:03 +00:00
|
|
|
self.log_filter = crate_log_filter(Some(filter.to_owned()));
|
2021-09-05 05:50:23 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:12:03 +00:00
|
|
|
fn crate_log_filter(level: Option<String>) -> String {
|
2021-11-27 11:19:41 +00:00
|
|
|
let level = level.unwrap_or_else(|| std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_owned()));
|
2021-09-07 09:12:03 +00:00
|
|
|
let mut filters = vec![];
|
|
|
|
filters.push(format!("flowy_sdk={}", level));
|
|
|
|
filters.push(format!("flowy_workspace={}", level));
|
|
|
|
filters.push(format!("flowy_user={}", level));
|
2021-09-09 09:34:01 +00:00
|
|
|
filters.push(format!("flowy_document={}", level));
|
2021-11-19 05:13:50 +00:00
|
|
|
filters.push(format!("flowy_document_infra={}", level));
|
|
|
|
filters.push(format!("dart_notify={}", level));
|
|
|
|
filters.push(format!("lib_ot={}", level));
|
|
|
|
filters.push(format!("lib_ws={}", level));
|
|
|
|
filters.push(format!("lib_infra={}", level));
|
2021-09-07 09:12:03 +00:00
|
|
|
filters.join(",")
|
|
|
|
}
|
|
|
|
|
2021-09-04 07:12:53 +00:00
|
|
|
#[derive(Clone)]
|
2021-07-14 13:12:52 +00:00
|
|
|
pub struct FlowySDK {
|
2021-11-23 09:45:18 +00:00
|
|
|
#[allow(dead_code)]
|
2021-09-05 05:50:23 +00:00
|
|
|
config: FlowySDKConfig,
|
2021-09-27 15:23:23 +00:00
|
|
|
pub user_session: Arc<UserSession>,
|
|
|
|
pub flowy_document: Arc<FlowyDocument>,
|
2021-12-06 13:47:21 +00:00
|
|
|
pub core: Arc<CoreContext>,
|
2021-12-04 03:26:17 +00:00
|
|
|
pub dispatcher: Arc<EventDispatcher>,
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
2021-06-28 15:58:43 +00:00
|
|
|
|
|
|
|
impl FlowySDK {
|
2021-09-05 05:50:23 +00:00
|
|
|
pub fn new(config: FlowySDKConfig) -> Self {
|
|
|
|
init_log(&config);
|
|
|
|
init_kv(&config.root);
|
|
|
|
tracing::debug!("🔥 {:?}", config);
|
2021-11-08 15:15:29 +00:00
|
|
|
|
2021-11-09 09:50:32 +00:00
|
|
|
let session_cache_key = format!("{}_session_cache", &config.name);
|
2021-12-04 15:54:14 +00:00
|
|
|
|
|
|
|
let user_config = UserSessionConfig::new(&config.root, &config.server_config, &session_cache_key);
|
|
|
|
let user_session = Arc::new(UserSession::new(user_config));
|
2021-10-05 06:37:45 +00:00
|
|
|
let flowy_document = mk_document_module(user_session.clone(), &config.server_config);
|
2021-12-06 07:49:21 +00:00
|
|
|
let core = mk_core(user_session.clone(), flowy_document.clone(), &config.server_config);
|
|
|
|
|
|
|
|
let modules = mk_modules(core.clone(), user_session.clone());
|
2021-12-04 03:26:17 +00:00
|
|
|
let dispatcher = Arc::new(EventDispatcher::construct(|| modules));
|
2021-12-06 07:49:21 +00:00
|
|
|
_init(&dispatcher, user_session.clone(), core.clone());
|
2021-11-08 15:15:29 +00:00
|
|
|
|
2021-09-27 15:23:23 +00:00
|
|
|
Self {
|
|
|
|
config,
|
|
|
|
user_session,
|
|
|
|
flowy_document,
|
2021-12-06 07:49:21 +00:00
|
|
|
core,
|
2021-12-04 03:26:17 +00:00
|
|
|
dispatcher,
|
2021-09-27 15:23:23 +00:00
|
|
|
}
|
2021-09-04 07:12:53 +00:00
|
|
|
}
|
2021-07-14 13:12:52 +00:00
|
|
|
|
2021-12-04 03:26:17 +00:00
|
|
|
pub fn dispatcher(&self) -> Arc<EventDispatcher> { self.dispatcher.clone() }
|
2021-09-04 07:12:53 +00:00
|
|
|
}
|
2021-07-17 02:26:05 +00:00
|
|
|
|
2021-12-06 13:47:21 +00:00
|
|
|
fn _init(dispatch: &EventDispatcher, user_session: Arc<UserSession>, core: Arc<CoreContext>) {
|
2021-12-04 15:54:14 +00:00
|
|
|
let user_status_subscribe = user_session.notifier.user_status_subscribe();
|
2021-12-05 06:04:25 +00:00
|
|
|
let network_status_subscribe = user_session.notifier.network_type_subscribe();
|
2021-12-06 07:49:21 +00:00
|
|
|
let cloned_core = core.clone();
|
2021-12-05 01:29:42 +00:00
|
|
|
|
2021-11-09 05:29:31 +00:00
|
|
|
dispatch.spawn(async move {
|
|
|
|
user_session.init();
|
2021-12-06 07:49:21 +00:00
|
|
|
_listen_user_status(user_status_subscribe, core.clone()).await;
|
2021-12-05 01:29:42 +00:00
|
|
|
});
|
|
|
|
dispatch.spawn(async move {
|
2021-12-06 07:49:21 +00:00
|
|
|
_listen_network_status(network_status_subscribe, cloned_core).await;
|
2021-11-09 05:29:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:47:21 +00:00
|
|
|
async fn _listen_user_status(mut subscribe: broadcast::Receiver<UserStatus>, core: Arc<CoreContext>) {
|
2021-12-04 15:54:14 +00:00
|
|
|
while let Ok(status) = subscribe.recv().await {
|
|
|
|
let result = || async {
|
|
|
|
match status {
|
|
|
|
UserStatus::Login { token } => {
|
2021-12-06 07:49:21 +00:00
|
|
|
let _ = core.user_did_sign_in(&token).await?;
|
2021-12-04 15:54:14 +00:00
|
|
|
},
|
|
|
|
UserStatus::Logout { .. } => {
|
2021-12-06 07:49:21 +00:00
|
|
|
core.user_did_logout().await;
|
2021-12-04 15:54:14 +00:00
|
|
|
},
|
|
|
|
UserStatus::Expired { .. } => {
|
2021-12-06 07:49:21 +00:00
|
|
|
core.user_session_expired().await;
|
2021-12-04 15:54:14 +00:00
|
|
|
},
|
|
|
|
UserStatus::SignUp { profile, ret } => {
|
2021-12-06 07:49:21 +00:00
|
|
|
let _ = core.user_did_sign_up(&profile.token).await?;
|
2021-12-04 15:54:14 +00:00
|
|
|
let _ = ret.send(());
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Ok::<(), WorkspaceError>(())
|
|
|
|
};
|
|
|
|
|
|
|
|
match result().await {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => log::error!("{}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:47:21 +00:00
|
|
|
async fn _listen_network_status(mut subscribe: broadcast::Receiver<NetworkType>, core: Arc<CoreContext>) {
|
2021-12-05 06:04:25 +00:00
|
|
|
while let Ok(new_type) = subscribe.recv().await {
|
2021-12-06 07:49:21 +00:00
|
|
|
core.network_state_changed(new_type);
|
2021-11-09 05:29:31 +00:00
|
|
|
}
|
2021-11-08 15:15:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 07:12:53 +00:00
|
|
|
fn init_kv(root: &str) {
|
2021-11-19 06:38:11 +00:00
|
|
|
match lib_infra::kv::KV::init(root) {
|
2021-09-04 07:12:53 +00:00
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => tracing::error!("Init kv store failedL: {}", e),
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
2021-09-04 07:12:53 +00:00
|
|
|
}
|
2021-07-14 13:12:52 +00:00
|
|
|
|
2021-09-05 05:50:23 +00:00
|
|
|
fn init_log(config: &FlowySDKConfig) {
|
2021-09-04 07:12:53 +00:00
|
|
|
if !INIT_LOG.load(Ordering::SeqCst) {
|
|
|
|
INIT_LOG.store(true, Ordering::SeqCst);
|
2021-08-19 06:08:24 +00:00
|
|
|
|
2021-11-19 07:04:56 +00:00
|
|
|
let _ = lib_log::Builder::new("flowy-client", &config.root)
|
2021-09-05 05:50:23 +00:00
|
|
|
.env_filter(&config.log_filter)
|
|
|
|
.build();
|
2021-07-13 05:14:49 +00:00
|
|
|
}
|
2021-09-04 07:12:53 +00:00
|
|
|
}
|
2021-10-05 06:37:45 +00:00
|
|
|
|
2021-12-06 07:49:21 +00:00
|
|
|
fn mk_core(
|
2021-10-05 06:37:45 +00:00
|
|
|
user_session: Arc<UserSession>,
|
|
|
|
flowy_document: Arc<FlowyDocument>,
|
2021-12-05 08:39:41 +00:00
|
|
|
server_config: &ClientServerConfiguration,
|
2021-12-06 13:47:21 +00:00
|
|
|
) -> Arc<CoreContext> {
|
2021-11-27 11:19:41 +00:00
|
|
|
let workspace_deps = WorkspaceDepsResolver::new(user_session);
|
2021-10-05 06:37:45 +00:00
|
|
|
let (user, database) = workspace_deps.split_into();
|
2021-12-06 07:49:21 +00:00
|
|
|
init_core(user, database, flowy_document, server_config)
|
2021-10-05 06:37:45 +00:00
|
|
|
}
|