mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
generic DispatchFuture and mark async_send with sync trait
This commit is contained in:
parent
e1f73f5246
commit
602018765d
@ -14,7 +14,7 @@ import window_size
|
|||||||
|
|
||||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||||
FlowyEditorPlugin.register(with: registry.registrar(forPlugin: "FlowyEditorPlugin"))
|
FlowyEditorPlugin.register(with: registry.registrar(forPlugin: "FlowyEditorPlugin"))
|
||||||
FlowyInfraUiPlugin.register(with: registry.registrar(forPlugin: "FlowyInfraUiPlugin"))
|
FlowyInfraUIPlugin.register(with: registry.registrar(forPlugin: "FlowyInfraUIPlugin"))
|
||||||
FlowySdkPlugin.register(with: registry.registrar(forPlugin: "FlowySdkPlugin"))
|
FlowySdkPlugin.register(with: registry.registrar(forPlugin: "FlowySdkPlugin"))
|
||||||
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
||||||
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
|
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
|
||||||
|
@ -253,6 +253,20 @@ packages:
|
|||||||
relative: true
|
relative: true
|
||||||
source: path
|
source: path
|
||||||
version: "0.0.1"
|
version: "0.0.1"
|
||||||
|
flowy_infra_ui_platform_interface:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
path: "packages/flowy_infra_ui/flowy_infra_ui_platform_interface"
|
||||||
|
relative: true
|
||||||
|
source: path
|
||||||
|
version: "0.0.1"
|
||||||
|
flowy_infra_ui_web:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
path: "packages/flowy_infra_ui/flowy_infra_ui_web"
|
||||||
|
relative: true
|
||||||
|
source: path
|
||||||
|
version: "0.0.1"
|
||||||
flowy_sdk:
|
flowy_sdk:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
4
rust-lib/.gitignore
vendored
4
rust-lib/.gitignore
vendored
@ -10,4 +10,6 @@ Cargo.lock
|
|||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
**/**/*.log*
|
**/**/*.log*
|
||||||
**/**/temp
|
**/**/temp
|
||||||
bin/
|
bin/
|
||||||
|
|
||||||
|
.idea
|
@ -1,7 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
errors::{DispatchError, Error, InternalError},
|
errors::{DispatchError, Error, InternalError},
|
||||||
module::{as_module_map, Module, ModuleMap, ModuleRequest},
|
module::{as_module_map, Module, ModuleMap, ModuleRequest},
|
||||||
response::EventResponse,
|
response::{EventResponse, Responder},
|
||||||
service::{Service, ServiceFactory},
|
service::{Service, ServiceFactory},
|
||||||
util::tokio_default_runtime,
|
util::tokio_default_runtime,
|
||||||
};
|
};
|
||||||
@ -38,7 +38,7 @@ impl EventDispatch {
|
|||||||
*(EVENT_DISPATCH.write().unwrap()) = Some(dispatch);
|
*(EVENT_DISPATCH.write().unwrap()) = Some(dispatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn async_send<Req>(request: Req) -> DispatchFuture
|
pub fn async_send<Req>(request: Req) -> DispatchFuture<EventResponse>
|
||||||
where
|
where
|
||||||
Req: std::convert::Into<ModuleRequest>,
|
Req: std::convert::Into<ModuleRequest>,
|
||||||
{
|
{
|
||||||
@ -48,7 +48,7 @@ impl EventDispatch {
|
|||||||
pub fn async_send_with_callback<Req, Callback>(
|
pub fn async_send_with_callback<Req, Callback>(
|
||||||
request: Req,
|
request: Req,
|
||||||
callback: Callback,
|
callback: Callback,
|
||||||
) -> DispatchFuture
|
) -> DispatchFuture<EventResponse>
|
||||||
where
|
where
|
||||||
Req: std::convert::Into<ModuleRequest>,
|
Req: std::convert::Into<ModuleRequest>,
|
||||||
Callback: FnOnce(EventResponse) -> BoxFuture<'static, ()> + 'static + Send + Sync,
|
Callback: FnOnce(EventResponse) -> BoxFuture<'static, ()> + 'static + Send + Sync,
|
||||||
@ -99,13 +99,16 @@ impl EventDispatch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project]
|
#[pin_project]
|
||||||
pub struct DispatchFuture {
|
pub struct DispatchFuture<T: Responder + Send + Sync> {
|
||||||
#[pin]
|
#[pin]
|
||||||
fut: BoxFuture<'static, EventResponse>,
|
pub fut: Pin<Box<dyn Future<Output = T> + Sync + Send>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Future for DispatchFuture {
|
impl<T> Future for DispatchFuture<T>
|
||||||
type Output = EventResponse;
|
where
|
||||||
|
T: Responder + Send + Sync,
|
||||||
|
{
|
||||||
|
type Output = T;
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let this = self.as_mut().project();
|
let this = self.as_mut().project();
|
||||||
|
@ -12,6 +12,9 @@ mod data;
|
|||||||
mod dispatch;
|
mod dispatch;
|
||||||
mod system;
|
mod system;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
pub mod macros;
|
||||||
|
|
||||||
pub use errors::Error;
|
pub use errors::Error;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
|
8
rust-lib/flowy-dispatch/src/macros.rs
Normal file
8
rust-lib/flowy-dispatch/src/macros.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#[macro_export]
|
||||||
|
macro_rules! dispatch_future {
|
||||||
|
($fut:expr) => {
|
||||||
|
DispatchFuture {
|
||||||
|
fut: Box::pin(async move { $fut.await }),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -1,4 +1,3 @@
|
|||||||
use flowy_infra::uuid;
|
|
||||||
use flowy_user::{
|
use flowy_user::{
|
||||||
entities::{SignInParams, SignUpParams, UserDetail},
|
entities::{SignInParams, SignUpParams, UserDetail},
|
||||||
errors::{ErrorBuilder, UserError, UserErrorCode},
|
errors::{ErrorBuilder, UserError, UserErrorCode},
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
use crate::flowy_server::{ArcFlowyServer, FlowyServerMocker};
|
use crate::flowy_server::{ArcFlowyServer, FlowyServerMocker};
|
||||||
use flowy_database::DBConnection;
|
use flowy_database::DBConnection;
|
||||||
use flowy_dispatch::prelude::Module;
|
use flowy_dispatch::prelude::{DispatchFuture, Module};
|
||||||
use flowy_user::{errors::UserError, prelude::*};
|
use flowy_user::prelude::*;
|
||||||
use flowy_workspace::prelude::*;
|
use flowy_workspace::prelude::*;
|
||||||
use futures_core::future::BoxFuture;
|
|
||||||
use std::{pin::Pin, sync::Arc};
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub struct ModuleConfig {
|
pub struct ModuleConfig {
|
||||||
pub root: String,
|
pub root: String,
|
||||||
@ -32,20 +32,26 @@ pub struct WorkspaceUserImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl WorkspaceUser for WorkspaceUserImpl {
|
impl WorkspaceUser for WorkspaceUserImpl {
|
||||||
fn set_current_workspace(&self, workspace_id: &str) -> BoxFuture<()> {
|
fn set_workspace(&self, workspace_id: &str) -> DispatchFuture<Result<(), WorkspaceError>> {
|
||||||
let user_session = self.user_session.clone();
|
let user_session = self.user_session.clone();
|
||||||
let workspace_id = workspace_id.to_owned();
|
let workspace_id = workspace_id.to_owned();
|
||||||
Box::pin(async move {
|
DispatchFuture {
|
||||||
match user_session.set_current_workspace(&workspace_id).await {
|
fut: Box::pin(async move {
|
||||||
Ok(_) => {},
|
let _ = user_session
|
||||||
Err(e) => {
|
.set_current_workspace(&workspace_id)
|
||||||
log::error!("Set current workspace error: {:?}", e);
|
.await
|
||||||
},
|
.map_err(|e| {
|
||||||
}
|
ErrorBuilder::new(WorkspaceErrorCode::UserInternalError)
|
||||||
})
|
.error(e)
|
||||||
|
.build()
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_workspace(&self) -> Result<String, WorkspaceError> {
|
fn get_workspace(&self) -> Result<String, WorkspaceError> {
|
||||||
let user_detail = self.user_session.user_detail().map_err(|e| {
|
let user_detail = self.user_session.user_detail().map_err(|e| {
|
||||||
ErrorBuilder::new(WorkspaceErrorCode::UserNotLoginYet)
|
ErrorBuilder::new(WorkspaceErrorCode::UserNotLoginYet)
|
||||||
.error(e)
|
.error(e)
|
||||||
|
@ -16,10 +16,11 @@ use std::marker::PhantomData;
|
|||||||
pub type WorkspaceTestBuilder = TestBuilder<FixedUserTester<WorkspaceError>>;
|
pub type WorkspaceTestBuilder = TestBuilder<FixedUserTester<WorkspaceError>>;
|
||||||
impl WorkspaceTestBuilder {
|
impl WorkspaceTestBuilder {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
let builder = Self {
|
||||||
tester: Box::new(FixedUserTester::<WorkspaceError>::new()),
|
tester: Box::new(FixedUserTester::<WorkspaceError>::new()),
|
||||||
user_detail: None,
|
user_detail: None,
|
||||||
}
|
};
|
||||||
|
builder.login()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@ use std::{
|
|||||||
fmt::{Debug, Display},
|
fmt::{Debug, Display},
|
||||||
hash::Hash,
|
hash::Hash,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
thread,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct TesterContext {
|
pub struct TesterContext {
|
||||||
|
@ -23,6 +23,35 @@ pub struct UpdateUserRequest {
|
|||||||
pub password: Option<String>,
|
pub password: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl UpdateUserRequest {
|
||||||
|
pub fn new(id: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
id: id.to_owned(),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn name(mut self, name: &str) -> Self {
|
||||||
|
self.name = Some(name.to_owned());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn email(mut self, email: &str) -> Self {
|
||||||
|
self.email = Some(email.to_owned());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn workspace(mut self, workspace: &str) -> Self {
|
||||||
|
self.workspace = Some(workspace.to_owned());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn password(mut self, password: &str) -> Self {
|
||||||
|
self.password = Some(password.to_owned());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct UpdateUserParams {
|
pub struct UpdateUserParams {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
|
@ -3,14 +3,7 @@ use flowy_database::{DBConnection, Database};
|
|||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use std::{
|
use std::{collections::HashMap, sync::RwLock};
|
||||||
cell::RefCell,
|
|
||||||
collections::HashMap,
|
|
||||||
sync::{
|
|
||||||
atomic::{AtomicBool, Ordering},
|
|
||||||
RwLock,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref DB: RwLock<Option<Database>> = RwLock::new(None);
|
static ref DB: RwLock<Option<Database>> = RwLock::new(None);
|
||||||
|
@ -6,7 +6,7 @@ use flowy_database::{
|
|||||||
UserDatabaseConnection,
|
UserDatabaseConnection,
|
||||||
};
|
};
|
||||||
use flowy_infra::kv::KVStore;
|
use flowy_infra::kv::KVStore;
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -159,15 +159,10 @@ impl UserSession {
|
|||||||
|
|
||||||
pub async fn set_current_workspace(&self, workspace_id: &str) -> Result<(), UserError> {
|
pub async fn set_current_workspace(&self, workspace_id: &str) -> Result<(), UserError> {
|
||||||
let user_id = self.get_user_id()?;
|
let user_id = self.get_user_id()?;
|
||||||
let payload: Vec<u8> = UpdateUserRequest {
|
let payload: Vec<u8> = UpdateUserRequest::new(&user_id)
|
||||||
id: user_id,
|
.workspace(workspace_id)
|
||||||
name: None,
|
.into_bytes()
|
||||||
email: None,
|
.unwrap();
|
||||||
workspace: Some(workspace_id.to_owned()),
|
|
||||||
password: None,
|
|
||||||
}
|
|
||||||
.into_bytes()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let request = ModuleRequest::new(UpdateUser).payload(payload);
|
let request = ModuleRequest::new(UpdateUser).payload(payload);
|
||||||
let _user_detail = EventDispatch::async_send(request)
|
let _user_detail = EventDispatch::async_send(request)
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
entities::workspace::{CreateWorkspaceParams, CreateWorkspaceRequest, WorkspaceDetail},
|
entities::workspace::{CreateWorkspaceParams, CreateWorkspaceRequest, WorkspaceDetail},
|
||||||
errors::WorkspaceError,
|
errors::WorkspaceError,
|
||||||
services::{save_workspace, WorkspaceController},
|
services::WorkspaceController,
|
||||||
};
|
};
|
||||||
use flowy_dispatch::prelude::{response_ok, Data, EventResponse, ModuleData, ResponseResult};
|
use flowy_dispatch::prelude::{response_ok, Data, ModuleData, ResponseResult};
|
||||||
use std::{convert::TryInto, pin::Pin, sync::Arc};
|
use std::{convert::TryInto, sync::Arc};
|
||||||
|
|
||||||
pub async fn create_workspace(
|
pub async fn create_workspace(
|
||||||
data: Data<CreateWorkspaceRequest>,
|
data: Data<CreateWorkspaceRequest>,
|
||||||
@ -12,6 +12,16 @@ pub async fn create_workspace(
|
|||||||
) -> ResponseResult<WorkspaceDetail, WorkspaceError> {
|
) -> ResponseResult<WorkspaceDetail, WorkspaceError> {
|
||||||
let controller = controller.get_ref().clone();
|
let controller = controller.get_ref().clone();
|
||||||
let params: CreateWorkspaceParams = data.into_inner().try_into()?;
|
let params: CreateWorkspaceParams = data.into_inner().try_into()?;
|
||||||
let detail = save_workspace(controller, params).await?;
|
let detail = controller.save_workspace(params).await?;
|
||||||
|
response_ok(detail)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn workspace_user(
|
||||||
|
data: Data<CreateWorkspaceRequest>,
|
||||||
|
controller: ModuleData<Arc<WorkspaceController>>,
|
||||||
|
) -> ResponseResult<WorkspaceDetail, WorkspaceError> {
|
||||||
|
let controller = controller.get_ref().clone();
|
||||||
|
let params: CreateWorkspaceParams = data.into_inner().try_into()?;
|
||||||
|
let detail = controller.save_workspace(params).await?;
|
||||||
response_ok(detail)
|
response_ok(detail)
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,9 @@ mod services;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate flowy_database;
|
extern crate flowy_database;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate flowy_dispatch;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use crate::{errors::*, module::*, services::*};
|
pub use crate::{errors::*, module::*, services::*};
|
||||||
}
|
}
|
||||||
|
@ -3,16 +3,16 @@ use flowy_dispatch::prelude::*;
|
|||||||
use crate::{
|
use crate::{
|
||||||
errors::WorkspaceError,
|
errors::WorkspaceError,
|
||||||
event::WorkspaceEvent,
|
event::WorkspaceEvent,
|
||||||
handlers::create_workspace,
|
|
||||||
services::{AppController, WorkspaceController},
|
services::{AppController, WorkspaceController},
|
||||||
};
|
};
|
||||||
use flowy_database::DBConnection;
|
use flowy_database::DBConnection;
|
||||||
use futures_core::future::BoxFuture;
|
|
||||||
|
use crate::handlers::*;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub trait WorkspaceUser: Send + Sync {
|
pub trait WorkspaceUser: Send + Sync {
|
||||||
fn set_current_workspace(&self, id: &str) -> BoxFuture<()>;
|
fn set_workspace(&self, id: &str) -> DispatchFuture<Result<(), WorkspaceError>>;
|
||||||
fn get_current_workspace(&self) -> Result<String, WorkspaceError>;
|
fn get_workspace(&self) -> Result<String, WorkspaceError>;
|
||||||
fn db_connection(&self) -> Result<DBConnection, WorkspaceError>;
|
fn db_connection(&self) -> Result<DBConnection, WorkspaceError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,4 +25,5 @@ pub fn create(user: Arc<dyn WorkspaceUser>) -> Module {
|
|||||||
.data(workspace_controller)
|
.data(workspace_controller)
|
||||||
.data(app_controller)
|
.data(app_controller)
|
||||||
.event(WorkspaceEvent::CreateWorkspace, create_workspace)
|
.event(WorkspaceEvent::CreateWorkspace, create_workspace)
|
||||||
|
.event(WorkspaceEvent::GetWorkspaceUserDetail, workspace_user)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::{entities::workspace::*, errors::*, module::WorkspaceUser, sql_tables::workspace::*};
|
use crate::{entities::workspace::*, errors::*, module::WorkspaceUser, sql_tables::workspace::*};
|
||||||
use flowy_database::{prelude::*, schema::workspace_table};
|
use flowy_database::{prelude::*, schema::workspace_table};
|
||||||
use futures_core::future::BoxFuture;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub struct WorkspaceController {
|
pub struct WorkspaceController {
|
||||||
@ -15,14 +15,13 @@ impl WorkspaceController {
|
|||||||
params: CreateWorkspaceParams,
|
params: CreateWorkspaceParams,
|
||||||
) -> Result<WorkspaceDetail, WorkspaceError> {
|
) -> Result<WorkspaceDetail, WorkspaceError> {
|
||||||
let workspace = Workspace::new(params);
|
let workspace = Workspace::new(params);
|
||||||
let conn = self.user.db_connection()?;
|
|
||||||
let detail: WorkspaceDetail = workspace.clone().into();
|
let detail: WorkspaceDetail = workspace.clone().into();
|
||||||
|
|
||||||
let _ = diesel::insert_into(workspace_table::table)
|
let _ = diesel::insert_into(workspace_table::table)
|
||||||
.values(workspace)
|
.values(workspace)
|
||||||
.execute(&*conn)?;
|
.execute(&*(self.user.db_connection()?))?;
|
||||||
|
|
||||||
self.user.set_current_workspace(&detail.id);
|
let _ = self.user.set_workspace(&detail.id).await?;
|
||||||
|
|
||||||
Ok(detail)
|
Ok(detail)
|
||||||
}
|
}
|
||||||
@ -35,19 +34,3 @@ impl WorkspaceController {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn save_workspace(
|
|
||||||
controller: Arc<WorkspaceController>,
|
|
||||||
params: CreateWorkspaceParams,
|
|
||||||
) -> Result<WorkspaceDetail, WorkspaceError> {
|
|
||||||
let workspace = Workspace::new(params);
|
|
||||||
let detail: WorkspaceDetail = workspace.clone().into();
|
|
||||||
|
|
||||||
let _ = diesel::insert_into(workspace_table::table)
|
|
||||||
.values(workspace)
|
|
||||||
.execute(&*(controller.user.db_connection()?))?;
|
|
||||||
|
|
||||||
// set_current_workspace(controller.clone(), &detail.id).await;
|
|
||||||
|
|
||||||
Ok(detail)
|
|
||||||
}
|
|
||||||
|
@ -13,7 +13,6 @@ fn workspace_create_success() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let response = WorkspaceTestBuilder::new()
|
let response = WorkspaceTestBuilder::new()
|
||||||
.login()
|
|
||||||
.event(CreateWorkspace)
|
.event(CreateWorkspace)
|
||||||
.request(request)
|
.request(request)
|
||||||
.sync_send()
|
.sync_send()
|
||||||
|
Loading…
Reference in New Issue
Block a user