mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
refactor: rename flowy-dispatch structs
This commit is contained in:
parent
fff4af53e2
commit
96c427e869
@ -31,7 +31,7 @@ pub extern "C" fn init_sdk(path: *mut c_char) -> i64 {
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn async_event(port: i64, input: *const u8, len: usize) {
|
||||
let request: ModuleRequest = FFIRequest::from_u8_pointer(input, len).into();
|
||||
let request: AFPluginRequest = FFIRequest::from_u8_pointer(input, len).into();
|
||||
log::trace!(
|
||||
"[FFI]: {} Async Event: {:?} with {} port",
|
||||
&request.id,
|
||||
@ -46,7 +46,7 @@ pub extern "C" fn async_event(port: i64, input: *const u8, len: usize) {
|
||||
}
|
||||
Some(e) => e.event_dispatcher.clone(),
|
||||
};
|
||||
let _ = EventDispatcher::async_send_with_callback(dispatcher, request, move |resp: EventResponse| {
|
||||
let _ = AFPluginDispatcher::async_send_with_callback(dispatcher, request, move |resp: EventResponse| {
|
||||
log::trace!("[FFI]: Post data to dart through {} port", port);
|
||||
Box::pin(post_to_flutter(resp, port))
|
||||
});
|
||||
@ -54,7 +54,7 @@ pub extern "C" fn async_event(port: i64, input: *const u8, len: usize) {
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn sync_event(input: *const u8, len: usize) -> *const u8 {
|
||||
let request: ModuleRequest = FFIRequest::from_u8_pointer(input, len).into();
|
||||
let request: AFPluginRequest = FFIRequest::from_u8_pointer(input, len).into();
|
||||
log::trace!("[FFI]: {} Sync Event: {:?}", &request.id, &request.event,);
|
||||
|
||||
let dispatcher = match FLOWY_SDK.get() {
|
||||
@ -64,7 +64,7 @@ pub extern "C" fn sync_event(input: *const u8, len: usize) -> *const u8 {
|
||||
}
|
||||
Some(e) => e.event_dispatcher.clone(),
|
||||
};
|
||||
let _response = EventDispatcher::sync_send(dispatcher, request);
|
||||
let _response = AFPluginDispatcher::sync_send(dispatcher, request);
|
||||
|
||||
// FFIResponse { }
|
||||
let response_bytes = vec![];
|
||||
|
@ -1,6 +1,6 @@
|
||||
use bytes::Bytes;
|
||||
use flowy_derive::ProtoBuf;
|
||||
use lib_dispatch::prelude::ModuleRequest;
|
||||
use lib_dispatch::prelude::AFPluginRequest;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
#[derive(Default, ProtoBuf)]
|
||||
@ -21,8 +21,8 @@ impl FFIRequest {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::From<FFIRequest> for ModuleRequest {
|
||||
impl std::convert::From<FFIRequest> for AFPluginRequest {
|
||||
fn from(ffi_request: FFIRequest) -> Self {
|
||||
ModuleRequest::new(ffi_request.event).payload(ffi_request.payload)
|
||||
AFPluginRequest::new(ffi_request.event).payload(ffi_request.payload)
|
||||
}
|
||||
}
|
||||
|
@ -4,13 +4,13 @@ use crate::entities::{
|
||||
use crate::DocumentManager;
|
||||
use flowy_error::FlowyError;
|
||||
|
||||
use lib_dispatch::prelude::{data_result, AppData, Data, DataResult};
|
||||
use lib_dispatch::prelude::{data_result, AFPluginData, AFPluginState, DataResult};
|
||||
use std::convert::TryInto;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(crate) async fn get_document_handler(
|
||||
data: Data<OpenDocumentContextPB>,
|
||||
manager: AppData<Arc<DocumentManager>>,
|
||||
data: AFPluginData<OpenDocumentContextPB>,
|
||||
manager: AFPluginState<Arc<DocumentManager>>,
|
||||
) -> DataResult<DocumentSnapshotPB, FlowyError> {
|
||||
let context: OpenDocumentContextPB = data.into_inner();
|
||||
let editor = manager.open_document_editor(&context.document_id).await?;
|
||||
@ -22,8 +22,8 @@ pub(crate) async fn get_document_handler(
|
||||
}
|
||||
|
||||
pub(crate) async fn apply_edit_handler(
|
||||
data: Data<EditPayloadPB>,
|
||||
manager: AppData<Arc<DocumentManager>>,
|
||||
data: AFPluginData<EditPayloadPB>,
|
||||
manager: AFPluginState<Arc<DocumentManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: EditParams = data.into_inner().try_into()?;
|
||||
let _ = manager.apply_edit(params).await?;
|
||||
@ -32,8 +32,8 @@ pub(crate) async fn apply_edit_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
||||
pub(crate) async fn export_handler(
|
||||
data: Data<ExportPayloadPB>,
|
||||
manager: AppData<Arc<DocumentManager>>,
|
||||
data: AFPluginData<ExportPayloadPB>,
|
||||
manager: AFPluginState<Arc<DocumentManager>>,
|
||||
) -> DataResult<ExportDataPB, FlowyError> {
|
||||
let params: ExportParams = data.into_inner().try_into()?;
|
||||
let editor = manager.open_document_editor(¶ms.view_id).await?;
|
||||
|
@ -1,19 +1,19 @@
|
||||
use crate::event_handler::*;
|
||||
use crate::DocumentManager;
|
||||
use flowy_derive::{Flowy_Event, ProtoBuf_Enum};
|
||||
use lib_dispatch::prelude::Module;
|
||||
use lib_dispatch::prelude::AFPlugin;
|
||||
use std::sync::Arc;
|
||||
use strum_macros::Display;
|
||||
|
||||
pub fn create(document_manager: Arc<DocumentManager>) -> Module {
|
||||
let mut module = Module::new().name(env!("CARGO_PKG_NAME")).data(document_manager);
|
||||
pub fn init(document_manager: Arc<DocumentManager>) -> AFPlugin {
|
||||
let mut plugin = AFPlugin::new().name(env!("CARGO_PKG_NAME")).state(document_manager);
|
||||
|
||||
module = module
|
||||
plugin = plugin
|
||||
.event(DocumentEvent::GetDocument, get_document_handler)
|
||||
.event(DocumentEvent::ApplyEdit, apply_edit_handler)
|
||||
.event(DocumentEvent::ExportDocument, export_handler);
|
||||
|
||||
module
|
||||
plugin
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)]
|
||||
|
@ -34,17 +34,17 @@ pub trait WorkspaceDatabase: Send + Sync {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create(folder: Arc<FolderManager>) -> Module {
|
||||
let mut module = Module::new()
|
||||
pub fn init(folder: Arc<FolderManager>) -> AFPlugin {
|
||||
let mut plugin = AFPlugin::new()
|
||||
.name("Flowy-Workspace")
|
||||
.data(folder.workspace_controller.clone())
|
||||
.data(folder.app_controller.clone())
|
||||
.data(folder.view_controller.clone())
|
||||
.data(folder.trash_controller.clone())
|
||||
.data(folder.clone());
|
||||
.state(folder.workspace_controller.clone())
|
||||
.state(folder.app_controller.clone())
|
||||
.state(folder.view_controller.clone())
|
||||
.state(folder.trash_controller.clone())
|
||||
.state(folder.clone());
|
||||
|
||||
// Workspace
|
||||
module = module
|
||||
plugin = plugin
|
||||
.event(FolderEvent::CreateWorkspace, create_workspace_handler)
|
||||
.event(FolderEvent::ReadCurrentWorkspace, read_cur_workspace_handler)
|
||||
.event(FolderEvent::ReadWorkspaces, read_workspaces_handler)
|
||||
@ -52,14 +52,14 @@ pub fn create(folder: Arc<FolderManager>) -> Module {
|
||||
.event(FolderEvent::ReadWorkspaceApps, read_workspace_apps_handler);
|
||||
|
||||
// App
|
||||
module = module
|
||||
plugin = plugin
|
||||
.event(FolderEvent::CreateApp, create_app_handler)
|
||||
.event(FolderEvent::ReadApp, read_app_handler)
|
||||
.event(FolderEvent::UpdateApp, update_app_handler)
|
||||
.event(FolderEvent::DeleteApp, delete_app_handler);
|
||||
|
||||
// View
|
||||
module = module
|
||||
plugin = plugin
|
||||
.event(FolderEvent::CreateView, create_view_handler)
|
||||
.event(FolderEvent::ReadView, read_view_handler)
|
||||
.event(FolderEvent::UpdateView, update_view_handler)
|
||||
@ -71,14 +71,14 @@ pub fn create(folder: Arc<FolderManager>) -> Module {
|
||||
.event(FolderEvent::MoveFolderItem, move_item_handler);
|
||||
|
||||
// Trash
|
||||
module = module
|
||||
plugin = plugin
|
||||
.event(FolderEvent::ReadTrash, read_trash_handler)
|
||||
.event(FolderEvent::PutbackTrash, putback_trash_handler)
|
||||
.event(FolderEvent::DeleteTrash, delete_trash_handler)
|
||||
.event(FolderEvent::RestoreAllTrash, restore_all_trash_handler)
|
||||
.event(FolderEvent::DeleteAllTrash, delete_all_trash_handler);
|
||||
|
||||
module
|
||||
plugin
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)]
|
||||
|
@ -4,12 +4,12 @@ use crate::{
|
||||
services::{AppController, TrashController, ViewController},
|
||||
};
|
||||
use folder_rev_model::TrashRevision;
|
||||
use lib_dispatch::prelude::{data_result, AppData, Data, DataResult};
|
||||
use lib_dispatch::prelude::{data_result, AFPluginData, AFPluginState, DataResult};
|
||||
use std::{convert::TryInto, sync::Arc};
|
||||
|
||||
pub(crate) async fn create_app_handler(
|
||||
data: Data<CreateAppPayloadPB>,
|
||||
controller: AppData<Arc<AppController>>,
|
||||
data: AFPluginData<CreateAppPayloadPB>,
|
||||
controller: AFPluginState<Arc<AppController>>,
|
||||
) -> DataResult<AppPB, FlowyError> {
|
||||
let params: CreateAppParams = data.into_inner().try_into()?;
|
||||
let detail = controller.create_app_from_params(params).await?;
|
||||
@ -18,9 +18,9 @@ pub(crate) async fn create_app_handler(
|
||||
}
|
||||
|
||||
pub(crate) async fn delete_app_handler(
|
||||
data: Data<AppIdPB>,
|
||||
app_controller: AppData<Arc<AppController>>,
|
||||
trash_controller: AppData<Arc<TrashController>>,
|
||||
data: AFPluginData<AppIdPB>,
|
||||
app_controller: AFPluginState<Arc<AppController>>,
|
||||
trash_controller: AFPluginState<Arc<TrashController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: AppIdPB = data.into_inner();
|
||||
let trash = app_controller
|
||||
@ -36,8 +36,8 @@ pub(crate) async fn delete_app_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, controller))]
|
||||
pub(crate) async fn update_app_handler(
|
||||
data: Data<UpdateAppPayloadPB>,
|
||||
controller: AppData<Arc<AppController>>,
|
||||
data: AFPluginData<UpdateAppPayloadPB>,
|
||||
controller: AFPluginState<Arc<AppController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: UpdateAppParams = data.into_inner().try_into()?;
|
||||
let _ = controller.update_app(params).await?;
|
||||
@ -46,9 +46,9 @@ pub(crate) async fn update_app_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, app_controller, view_controller), err)]
|
||||
pub(crate) async fn read_app_handler(
|
||||
data: Data<AppIdPB>,
|
||||
app_controller: AppData<Arc<AppController>>,
|
||||
view_controller: AppData<Arc<ViewController>>,
|
||||
data: AFPluginData<AppIdPB>,
|
||||
app_controller: AFPluginState<Arc<AppController>>,
|
||||
view_controller: AFPluginState<Arc<ViewController>>,
|
||||
) -> DataResult<AppPB, FlowyError> {
|
||||
let params: AppIdPB = data.into_inner();
|
||||
let mut app_rev = app_controller.read_app(params.clone()).await?;
|
||||
|
@ -3,12 +3,12 @@ use crate::{
|
||||
errors::FlowyError,
|
||||
services::TrashController,
|
||||
};
|
||||
use lib_dispatch::prelude::{data_result, AppData, Data, DataResult};
|
||||
use lib_dispatch::prelude::{data_result, AFPluginData, AFPluginState, DataResult};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(controller), err)]
|
||||
pub(crate) async fn read_trash_handler(
|
||||
controller: AppData<Arc<TrashController>>,
|
||||
controller: AFPluginState<Arc<TrashController>>,
|
||||
) -> DataResult<RepeatedTrashPB, FlowyError> {
|
||||
let repeated_trash = controller.read_trash().await?;
|
||||
data_result(repeated_trash)
|
||||
@ -16,8 +16,8 @@ pub(crate) async fn read_trash_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(identifier, controller), err)]
|
||||
pub(crate) async fn putback_trash_handler(
|
||||
identifier: Data<TrashIdPB>,
|
||||
controller: AppData<Arc<TrashController>>,
|
||||
identifier: AFPluginData<TrashIdPB>,
|
||||
controller: AFPluginState<Arc<TrashController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let _ = controller.putback(&identifier.id).await?;
|
||||
Ok(())
|
||||
@ -25,21 +25,25 @@ pub(crate) async fn putback_trash_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(identifiers, controller), err)]
|
||||
pub(crate) async fn delete_trash_handler(
|
||||
identifiers: Data<RepeatedTrashIdPB>,
|
||||
controller: AppData<Arc<TrashController>>,
|
||||
identifiers: AFPluginData<RepeatedTrashIdPB>,
|
||||
controller: AFPluginState<Arc<TrashController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let _ = controller.delete(identifiers.into_inner()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(controller), err)]
|
||||
pub(crate) async fn restore_all_trash_handler(controller: AppData<Arc<TrashController>>) -> Result<(), FlowyError> {
|
||||
pub(crate) async fn restore_all_trash_handler(
|
||||
controller: AFPluginState<Arc<TrashController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let _ = controller.restore_all_trash().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(controller), err)]
|
||||
pub(crate) async fn delete_all_trash_handler(controller: AppData<Arc<TrashController>>) -> Result<(), FlowyError> {
|
||||
pub(crate) async fn delete_all_trash_handler(
|
||||
controller: AFPluginState<Arc<TrashController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let _ = controller.delete_all_trash().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -14,12 +14,12 @@ use crate::{
|
||||
services::{TrashController, ViewController},
|
||||
};
|
||||
use folder_rev_model::TrashRevision;
|
||||
use lib_dispatch::prelude::{data_result, AppData, Data, DataResult};
|
||||
use lib_dispatch::prelude::{data_result, AFPluginData, AFPluginState, DataResult};
|
||||
use std::{convert::TryInto, sync::Arc};
|
||||
|
||||
pub(crate) async fn create_view_handler(
|
||||
data: Data<CreateViewPayloadPB>,
|
||||
controller: AppData<Arc<ViewController>>,
|
||||
data: AFPluginData<CreateViewPayloadPB>,
|
||||
controller: AFPluginState<Arc<ViewController>>,
|
||||
) -> DataResult<ViewPB, FlowyError> {
|
||||
let params: CreateViewParams = data.into_inner().try_into()?;
|
||||
let view_rev = controller.create_view_from_params(params).await?;
|
||||
@ -27,8 +27,8 @@ pub(crate) async fn create_view_handler(
|
||||
}
|
||||
|
||||
pub(crate) async fn read_view_handler(
|
||||
data: Data<ViewIdPB>,
|
||||
controller: AppData<Arc<ViewController>>,
|
||||
data: AFPluginData<ViewIdPB>,
|
||||
controller: AFPluginState<Arc<ViewController>>,
|
||||
) -> DataResult<ViewPB, FlowyError> {
|
||||
let view_id: ViewIdPB = data.into_inner();
|
||||
let view_rev = controller.read_view(&view_id.value).await?;
|
||||
@ -36,8 +36,8 @@ pub(crate) async fn read_view_handler(
|
||||
}
|
||||
|
||||
pub(crate) async fn read_view_info_handler(
|
||||
data: Data<ViewIdPB>,
|
||||
controller: AppData<Arc<ViewController>>,
|
||||
data: AFPluginData<ViewIdPB>,
|
||||
controller: AFPluginState<Arc<ViewController>>,
|
||||
) -> DataResult<ViewInfoPB, FlowyError> {
|
||||
let view_id: ViewIdPB = data.into_inner();
|
||||
let view_info = controller.read_view_pb(view_id.clone()).await?;
|
||||
@ -46,8 +46,8 @@ pub(crate) async fn read_view_info_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, controller), err)]
|
||||
pub(crate) async fn update_view_handler(
|
||||
data: Data<UpdateViewPayloadPB>,
|
||||
controller: AppData<Arc<ViewController>>,
|
||||
data: AFPluginData<UpdateViewPayloadPB>,
|
||||
controller: AFPluginState<Arc<ViewController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: UpdateViewParams = data.into_inner().try_into()?;
|
||||
let _ = controller.update_view(params).await?;
|
||||
@ -56,9 +56,9 @@ pub(crate) async fn update_view_handler(
|
||||
}
|
||||
|
||||
pub(crate) async fn delete_view_handler(
|
||||
data: Data<RepeatedViewIdPB>,
|
||||
view_controller: AppData<Arc<ViewController>>,
|
||||
trash_controller: AppData<Arc<TrashController>>,
|
||||
data: AFPluginData<RepeatedViewIdPB>,
|
||||
view_controller: AFPluginState<Arc<ViewController>>,
|
||||
trash_controller: AFPluginState<Arc<TrashController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: RepeatedViewIdPB = data.into_inner();
|
||||
for view_id in ¶ms.items {
|
||||
@ -80,9 +80,9 @@ pub(crate) async fn delete_view_handler(
|
||||
}
|
||||
|
||||
pub(crate) async fn set_latest_view_handler(
|
||||
data: Data<ViewIdPB>,
|
||||
folder: AppData<Arc<FolderManager>>,
|
||||
controller: AppData<Arc<ViewController>>,
|
||||
data: AFPluginData<ViewIdPB>,
|
||||
folder: AFPluginState<Arc<FolderManager>>,
|
||||
controller: AFPluginState<Arc<ViewController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let view_id: ViewIdPB = data.into_inner();
|
||||
let _ = controller.set_latest_view(&view_id.value)?;
|
||||
@ -91,8 +91,8 @@ pub(crate) async fn set_latest_view_handler(
|
||||
}
|
||||
|
||||
pub(crate) async fn close_view_handler(
|
||||
data: Data<ViewIdPB>,
|
||||
controller: AppData<Arc<ViewController>>,
|
||||
data: AFPluginData<ViewIdPB>,
|
||||
controller: AFPluginState<Arc<ViewController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let view_id: ViewIdPB = data.into_inner();
|
||||
let _ = controller.close_view(&view_id.value).await?;
|
||||
@ -101,9 +101,9 @@ pub(crate) async fn close_view_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip_all, err)]
|
||||
pub(crate) async fn move_item_handler(
|
||||
data: Data<MoveFolderItemPayloadPB>,
|
||||
view_controller: AppData<Arc<ViewController>>,
|
||||
app_controller: AppData<Arc<AppController>>,
|
||||
data: AFPluginData<MoveFolderItemPayloadPB>,
|
||||
view_controller: AFPluginState<Arc<ViewController>>,
|
||||
app_controller: AFPluginState<Arc<AppController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: MoveFolderItemParams = data.into_inner().try_into()?;
|
||||
match params.ty {
|
||||
@ -121,8 +121,8 @@ pub(crate) async fn move_item_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, controller), err)]
|
||||
pub(crate) async fn duplicate_view_handler(
|
||||
data: Data<ViewPB>,
|
||||
controller: AppData<Arc<ViewController>>,
|
||||
data: AFPluginData<ViewPB>,
|
||||
controller: AFPluginState<Arc<ViewController>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let view: ViewPB = data.into_inner();
|
||||
let _ = controller.duplicate_view(view).await?;
|
||||
|
@ -9,13 +9,13 @@ use crate::{
|
||||
manager::FolderManager,
|
||||
services::{get_current_workspace, read_local_workspace_apps, WorkspaceController},
|
||||
};
|
||||
use lib_dispatch::prelude::{data_result, AppData, Data, DataResult};
|
||||
use lib_dispatch::prelude::{data_result, AFPluginData, AFPluginState, DataResult};
|
||||
use std::{convert::TryInto, sync::Arc};
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, controller), err)]
|
||||
pub(crate) async fn create_workspace_handler(
|
||||
data: Data<CreateWorkspacePayloadPB>,
|
||||
controller: AppData<Arc<WorkspaceController>>,
|
||||
data: AFPluginData<CreateWorkspacePayloadPB>,
|
||||
controller: AFPluginState<Arc<WorkspaceController>>,
|
||||
) -> DataResult<WorkspacePB, FlowyError> {
|
||||
let controller = controller.get_ref().clone();
|
||||
let params: CreateWorkspaceParams = data.into_inner().try_into()?;
|
||||
@ -25,7 +25,7 @@ pub(crate) async fn create_workspace_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(controller), err)]
|
||||
pub(crate) async fn read_workspace_apps_handler(
|
||||
controller: AppData<Arc<WorkspaceController>>,
|
||||
controller: AFPluginState<Arc<WorkspaceController>>,
|
||||
) -> DataResult<RepeatedAppPB, FlowyError> {
|
||||
let items = controller
|
||||
.read_current_workspace_apps()
|
||||
@ -39,8 +39,8 @@ pub(crate) async fn read_workspace_apps_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, controller), err)]
|
||||
pub(crate) async fn open_workspace_handler(
|
||||
data: Data<WorkspaceIdPB>,
|
||||
controller: AppData<Arc<WorkspaceController>>,
|
||||
data: AFPluginData<WorkspaceIdPB>,
|
||||
controller: AFPluginState<Arc<WorkspaceController>>,
|
||||
) -> DataResult<WorkspacePB, FlowyError> {
|
||||
let params: WorkspaceIdPB = data.into_inner();
|
||||
let workspaces = controller.open_workspace(params).await?;
|
||||
@ -49,8 +49,8 @@ pub(crate) async fn open_workspace_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, folder), err)]
|
||||
pub(crate) async fn read_workspaces_handler(
|
||||
data: Data<WorkspaceIdPB>,
|
||||
folder: AppData<Arc<FolderManager>>,
|
||||
data: AFPluginData<WorkspaceIdPB>,
|
||||
folder: AFPluginState<Arc<FolderManager>>,
|
||||
) -> DataResult<RepeatedWorkspacePB, FlowyError> {
|
||||
let params: WorkspaceIdPB = data.into_inner();
|
||||
let user_id = folder.user.user_id()?;
|
||||
@ -78,7 +78,7 @@ pub(crate) async fn read_workspaces_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(folder), err)]
|
||||
pub async fn read_cur_workspace_handler(
|
||||
folder: AppData<Arc<FolderManager>>,
|
||||
folder: AFPluginState<Arc<FolderManager>>,
|
||||
) -> DataResult<WorkspaceSettingPB, FlowyError> {
|
||||
let workspace_id = get_current_workspace()?;
|
||||
let user_id = folder.user.user_id()?;
|
||||
@ -108,7 +108,7 @@ pub async fn read_cur_workspace_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(folder_manager), err)]
|
||||
fn read_workspaces_on_server(
|
||||
folder_manager: AppData<Arc<FolderManager>>,
|
||||
folder_manager: AFPluginState<Arc<FolderManager>>,
|
||||
user_id: String,
|
||||
params: WorkspaceIdPB,
|
||||
) -> Result<(), FlowyError> {
|
||||
|
@ -10,13 +10,13 @@ use crate::services::field::{
|
||||
use crate::services::row::{make_block_pbs, make_row_from_row_rev};
|
||||
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
|
||||
use grid_rev_model::FieldRevision;
|
||||
use lib_dispatch::prelude::{data_result, AppData, Data, DataResult};
|
||||
use lib_dispatch::prelude::{data_result, AFPluginData, AFPluginState, DataResult};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn get_grid_handler(
|
||||
data: Data<GridIdPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<GridIdPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<GridPB, FlowyError> {
|
||||
let grid_id: GridIdPB = data.into_inner();
|
||||
let editor = manager.open_grid(grid_id).await?;
|
||||
@ -26,8 +26,8 @@ pub(crate) async fn get_grid_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn get_grid_setting_handler(
|
||||
data: Data<GridIdPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<GridIdPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<GridSettingPB, FlowyError> {
|
||||
let grid_id: GridIdPB = data.into_inner();
|
||||
let editor = manager.open_grid(grid_id).await?;
|
||||
@ -37,8 +37,8 @@ pub(crate) async fn get_grid_setting_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn update_grid_setting_handler(
|
||||
data: Data<GridSettingChangesetPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<GridSettingChangesetPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: GridSettingChangesetParams = data.into_inner().try_into()?;
|
||||
|
||||
@ -63,8 +63,8 @@ pub(crate) async fn update_grid_setting_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn get_all_filters_handler(
|
||||
data: Data<GridIdPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<GridIdPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<RepeatedFilterPB, FlowyError> {
|
||||
let grid_id: GridIdPB = data.into_inner();
|
||||
let editor = manager.open_grid(grid_id).await?;
|
||||
@ -76,8 +76,8 @@ pub(crate) async fn get_all_filters_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
||||
pub(crate) async fn get_grid_blocks_handler(
|
||||
data: Data<QueryBlocksPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<QueryBlocksPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<RepeatedBlockPB, FlowyError> {
|
||||
let params: QueryGridBlocksParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -87,8 +87,8 @@ pub(crate) async fn get_grid_blocks_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn get_fields_handler(
|
||||
data: Data<GetFieldPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<GetFieldPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<RepeatedFieldPB, FlowyError> {
|
||||
let params: GetFieldParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -99,8 +99,8 @@ pub(crate) async fn get_fields_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn update_field_handler(
|
||||
data: Data<FieldChangesetPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<FieldChangesetPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let changeset: FieldChangesetParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(&changeset.grid_id).await?;
|
||||
@ -110,8 +110,8 @@ pub(crate) async fn update_field_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn update_field_type_option_handler(
|
||||
data: Data<TypeOptionChangesetPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<TypeOptionChangesetPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: TypeOptionChangesetParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -129,8 +129,8 @@ pub(crate) async fn update_field_type_option_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn delete_field_handler(
|
||||
data: Data<DeleteFieldPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<DeleteFieldPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: FieldIdParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -140,8 +140,8 @@ pub(crate) async fn delete_field_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn switch_to_field_handler(
|
||||
data: Data<EditFieldChangesetPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<EditFieldChangesetPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: EditFieldParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -167,8 +167,8 @@ pub(crate) async fn switch_to_field_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn duplicate_field_handler(
|
||||
data: Data<DuplicateFieldPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<DuplicateFieldPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: FieldIdParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -179,8 +179,8 @@ pub(crate) async fn duplicate_field_handler(
|
||||
/// Return the FieldTypeOptionData if the Field exists otherwise return record not found error.
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn get_field_type_option_data_handler(
|
||||
data: Data<TypeOptionPathPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<TypeOptionPathPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<TypeOptionPB, FlowyError> {
|
||||
let params: TypeOptionPathParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -202,8 +202,8 @@ pub(crate) async fn get_field_type_option_data_handler(
|
||||
/// Create FieldMeta and save it. Return the FieldTypeOptionData.
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn create_field_type_option_data_handler(
|
||||
data: Data<CreateFieldPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<CreateFieldPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<TypeOptionPB, FlowyError> {
|
||||
let params: CreateFieldParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -222,8 +222,8 @@ pub(crate) async fn create_field_type_option_data_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn move_field_handler(
|
||||
data: Data<MoveFieldPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<MoveFieldPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: MoveFieldParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -247,8 +247,8 @@ async fn get_type_option_data(field_rev: &FieldRevision, field_type: &FieldType)
|
||||
|
||||
// #[tracing::instrument(level = "debug", skip(data, manager), err)]
|
||||
pub(crate) async fn get_row_handler(
|
||||
data: Data<RowIdPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<RowIdPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<OptionalRowPB, FlowyError> {
|
||||
let params: RowIdParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -259,8 +259,8 @@ pub(crate) async fn get_row_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
||||
pub(crate) async fn delete_row_handler(
|
||||
data: Data<RowIdPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<RowIdPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: RowIdParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -270,8 +270,8 @@ pub(crate) async fn delete_row_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
||||
pub(crate) async fn duplicate_row_handler(
|
||||
data: Data<RowIdPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<RowIdPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: RowIdParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -281,8 +281,8 @@ pub(crate) async fn duplicate_row_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
||||
pub(crate) async fn move_row_handler(
|
||||
data: Data<MoveRowPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<MoveRowPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: MoveRowParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.view_id).await?;
|
||||
@ -292,8 +292,8 @@ pub(crate) async fn move_row_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
||||
pub(crate) async fn create_table_row_handler(
|
||||
data: Data<CreateTableRowPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<CreateTableRowPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<RowPB, FlowyError> {
|
||||
let params: CreateRowParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(params.grid_id.as_ref()).await?;
|
||||
@ -303,8 +303,8 @@ pub(crate) async fn create_table_row_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip_all, err)]
|
||||
pub(crate) async fn get_cell_handler(
|
||||
data: Data<CellPathPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<CellPathPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<CellPB, FlowyError> {
|
||||
let params: CellPathParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -316,8 +316,8 @@ pub(crate) async fn get_cell_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip_all, err)]
|
||||
pub(crate) async fn update_cell_handler(
|
||||
data: Data<CellChangesetPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<CellChangesetPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let changeset: CellChangesetPB = data.into_inner();
|
||||
let editor = manager.get_grid_editor(&changeset.grid_id).await?;
|
||||
@ -327,8 +327,8 @@ pub(crate) async fn update_cell_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip_all, err)]
|
||||
pub(crate) async fn new_select_option_handler(
|
||||
data: Data<CreateSelectOptionPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<CreateSelectOptionPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<SelectOptionPB, FlowyError> {
|
||||
let params: CreateSelectOptionParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -344,8 +344,8 @@ pub(crate) async fn new_select_option_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip_all, err)]
|
||||
pub(crate) async fn update_select_option_handler(
|
||||
data: Data<SelectOptionChangesetPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<SelectOptionChangesetPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let changeset: SelectOptionChangeset = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(&changeset.cell_identifier.grid_id).await?;
|
||||
@ -401,8 +401,8 @@ pub(crate) async fn update_select_option_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn get_select_option_handler(
|
||||
data: Data<CellPathPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<CellPathPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<SelectOptionCellDataPB, FlowyError> {
|
||||
let params: CellPathParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id).await?;
|
||||
@ -430,8 +430,8 @@ pub(crate) async fn get_select_option_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip_all, err)]
|
||||
pub(crate) async fn update_select_option_cell_handler(
|
||||
data: Data<SelectOptionCellChangesetPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<SelectOptionCellChangesetPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: SelectOptionCellChangesetParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.cell_identifier.grid_id).await?;
|
||||
@ -441,8 +441,8 @@ pub(crate) async fn update_select_option_cell_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip_all, err)]
|
||||
pub(crate) async fn update_date_cell_handler(
|
||||
data: Data<DateChangesetPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<DateChangesetPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let data = data.into_inner();
|
||||
let cell_path: CellPathParams = data.cell_path.try_into()?;
|
||||
@ -461,8 +461,8 @@ pub(crate) async fn update_date_cell_handler(
|
||||
|
||||
#[tracing::instrument(level = "trace", skip_all, err)]
|
||||
pub(crate) async fn get_groups_handler(
|
||||
data: Data<GridIdPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<GridIdPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<RepeatedGroupPB, FlowyError> {
|
||||
let params: GridIdPB = data.into_inner();
|
||||
let editor = manager.get_grid_editor(¶ms.value).await?;
|
||||
@ -472,8 +472,8 @@ pub(crate) async fn get_groups_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
||||
pub(crate) async fn create_board_card_handler(
|
||||
data: Data<CreateBoardCardPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<CreateBoardCardPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> DataResult<RowPB, FlowyError> {
|
||||
let params: CreateRowParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(params.grid_id.as_ref()).await?;
|
||||
@ -483,8 +483,8 @@ pub(crate) async fn create_board_card_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
||||
pub(crate) async fn move_group_handler(
|
||||
data: Data<MoveGroupPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<MoveGroupPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> FlowyResult<()> {
|
||||
let params: MoveGroupParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(params.view_id.as_ref()).await?;
|
||||
@ -494,8 +494,8 @@ pub(crate) async fn move_group_handler(
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
||||
pub(crate) async fn move_group_row_handler(
|
||||
data: Data<MoveGroupRowPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
data: AFPluginData<MoveGroupRowPayloadPB>,
|
||||
manager: AFPluginState<Arc<GridManager>>,
|
||||
) -> FlowyResult<()> {
|
||||
let params: MoveGroupRowParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(params.view_id.as_ref()).await?;
|
||||
|
@ -5,9 +5,9 @@ use lib_dispatch::prelude::*;
|
||||
use std::sync::Arc;
|
||||
use strum_macros::Display;
|
||||
|
||||
pub fn create(grid_manager: Arc<GridManager>) -> Module {
|
||||
let mut module = Module::new().name(env!("CARGO_PKG_NAME")).data(grid_manager);
|
||||
module = module
|
||||
pub fn init(grid_manager: Arc<GridManager>) -> AFPlugin {
|
||||
let mut plugin = AFPlugin::new().name(env!("CARGO_PKG_NAME")).state(grid_manager);
|
||||
plugin = plugin
|
||||
.event(GridEvent::GetGrid, get_grid_handler)
|
||||
.event(GridEvent::GetGridBlocks, get_grid_blocks_handler)
|
||||
.event(GridEvent::GetGridSetting, get_grid_setting_handler)
|
||||
@ -45,7 +45,7 @@ pub fn create(grid_manager: Arc<GridManager>) -> Module {
|
||||
.event(GridEvent::MoveGroupRow, move_group_row_handler)
|
||||
.event(GridEvent::GetGroup, get_groups_handler);
|
||||
|
||||
module
|
||||
plugin
|
||||
}
|
||||
|
||||
/// [GridEvent] defines events that are used to interact with the Grid. You could check [this](https://appflowy.gitbook.io/docs/essential-documentation/contribute-to-appflowy/architecture/backend/protobuf)
|
||||
|
@ -4,10 +4,10 @@ use lib_dispatch::prelude::*;
|
||||
use std::sync::Arc;
|
||||
use strum_macros::Display;
|
||||
|
||||
pub fn create(ws_conn: Arc<FlowyWebSocketConnect>) -> Module {
|
||||
Module::new()
|
||||
pub fn init(ws_conn: Arc<FlowyWebSocketConnect>) -> AFPlugin {
|
||||
AFPlugin::new()
|
||||
.name("Flowy-Network")
|
||||
.data(ws_conn)
|
||||
.state(ws_conn)
|
||||
.event(NetworkEvent::UpdateNetworkType, update_network_ty)
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
use crate::{entities::NetworkState, ws::connection::FlowyWebSocketConnect};
|
||||
use flowy_error::FlowyError;
|
||||
use lib_dispatch::prelude::{AppData, Data};
|
||||
use lib_dispatch::prelude::{AFPluginData, AFPluginState};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, ws_manager))]
|
||||
pub async fn update_network_ty(
|
||||
data: Data<NetworkState>,
|
||||
ws_manager: AppData<Arc<FlowyWebSocketConnect>>,
|
||||
data: AFPluginData<NetworkState>,
|
||||
ws_manager: AFPluginState<Arc<FlowyWebSocketConnect>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let network_state = data.into_inner();
|
||||
ws_manager.update_network_type(&network_state.ty);
|
||||
|
@ -19,7 +19,7 @@ use flowy_task::{TaskDispatcher, TaskRunner};
|
||||
use flowy_user::services::{notifier::UserStatus, UserSession, UserSessionConfig};
|
||||
use lib_dispatch::prelude::*;
|
||||
use lib_dispatch::runtime::tokio_default_runtime;
|
||||
use module::mk_modules;
|
||||
use module::make_plugins;
|
||||
pub use module::*;
|
||||
use std::time::Duration;
|
||||
use std::{
|
||||
@ -105,7 +105,7 @@ pub struct FlowySDK {
|
||||
pub document_manager: Arc<DocumentManager>,
|
||||
pub folder_manager: Arc<FolderManager>,
|
||||
pub grid_manager: Arc<GridManager>,
|
||||
pub event_dispatcher: Arc<EventDispatcher>,
|
||||
pub event_dispatcher: Arc<AFPluginDispatcher>,
|
||||
pub ws_conn: Arc<FlowyWebSocketConnect>,
|
||||
pub local_server: Option<Arc<LocalServer>>,
|
||||
pub task_dispatcher: Arc<RwLock<TaskDispatcher>>,
|
||||
@ -158,8 +158,8 @@ impl FlowySDK {
|
||||
)
|
||||
});
|
||||
|
||||
let event_dispatcher = Arc::new(EventDispatcher::construct(runtime, || {
|
||||
mk_modules(
|
||||
let event_dispatcher = Arc::new(AFPluginDispatcher::construct(runtime, || {
|
||||
make_plugins(
|
||||
&ws_conn,
|
||||
&folder_manager,
|
||||
&grid_manager,
|
||||
@ -191,14 +191,14 @@ impl FlowySDK {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dispatcher(&self) -> Arc<EventDispatcher> {
|
||||
pub fn dispatcher(&self) -> Arc<AFPluginDispatcher> {
|
||||
self.event_dispatcher.clone()
|
||||
}
|
||||
}
|
||||
|
||||
fn _start_listening(
|
||||
config: &FlowySDKConfig,
|
||||
event_dispatch: &EventDispatcher,
|
||||
event_dispatcher: &AFPluginDispatcher,
|
||||
ws_conn: &Arc<FlowyWebSocketConnect>,
|
||||
user_session: &Arc<UserSession>,
|
||||
document_manager: &Arc<DocumentManager>,
|
||||
@ -215,7 +215,7 @@ fn _start_listening(
|
||||
let document_manager = document_manager.clone();
|
||||
let config = config.clone();
|
||||
|
||||
event_dispatch.spawn(async move {
|
||||
event_dispatcher.spawn(async move {
|
||||
user_session.init();
|
||||
listen_on_websocket(ws_conn.clone());
|
||||
_listen_user_status(
|
||||
@ -229,7 +229,7 @@ fn _start_listening(
|
||||
.await;
|
||||
});
|
||||
|
||||
event_dispatch.spawn(async move {
|
||||
event_dispatcher.spawn(async move {
|
||||
_listen_network_status(subscribe_network_type, cloned_folder_manager).await;
|
||||
});
|
||||
}
|
||||
|
@ -3,40 +3,20 @@ use flowy_folder::manager::FolderManager;
|
||||
use flowy_grid::manager::GridManager;
|
||||
use flowy_net::ws::connection::FlowyWebSocketConnect;
|
||||
use flowy_user::services::UserSession;
|
||||
use lib_dispatch::prelude::Module;
|
||||
use lib_dispatch::prelude::AFPlugin;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn mk_modules(
|
||||
pub fn make_plugins(
|
||||
ws_conn: &Arc<FlowyWebSocketConnect>,
|
||||
folder_manager: &Arc<FolderManager>,
|
||||
grid_manager: &Arc<GridManager>,
|
||||
user_session: &Arc<UserSession>,
|
||||
document_manager: &Arc<DocumentManager>,
|
||||
) -> Vec<Module> {
|
||||
let user_module = mk_user_module(user_session.clone());
|
||||
let folder_module = mk_folder_module(folder_manager.clone());
|
||||
let network_module = mk_network_module(ws_conn.clone());
|
||||
let grid_module = mk_grid_module(grid_manager.clone());
|
||||
let document_module = mk_text_block_module(document_manager.clone());
|
||||
vec![user_module, folder_module, network_module, grid_module, document_module]
|
||||
}
|
||||
|
||||
fn mk_user_module(user_session: Arc<UserSession>) -> Module {
|
||||
flowy_user::event_map::create(user_session)
|
||||
}
|
||||
|
||||
fn mk_folder_module(folder_manager: Arc<FolderManager>) -> Module {
|
||||
flowy_folder::event_map::create(folder_manager)
|
||||
}
|
||||
|
||||
fn mk_network_module(ws_conn: Arc<FlowyWebSocketConnect>) -> Module {
|
||||
flowy_net::event_map::create(ws_conn)
|
||||
}
|
||||
|
||||
fn mk_grid_module(grid_manager: Arc<GridManager>) -> Module {
|
||||
flowy_grid::event_map::create(grid_manager)
|
||||
}
|
||||
|
||||
fn mk_text_block_module(text_block_manager: Arc<DocumentManager>) -> Module {
|
||||
flowy_document::event_map::create(text_block_manager)
|
||||
) -> Vec<AFPlugin> {
|
||||
let user_plugin = flowy_user::event_map::init(user_session.clone());
|
||||
let folder_plugin = flowy_folder::event_map::init(folder_manager.clone());
|
||||
let network_plugin = flowy_net::event_map::init(ws_conn.clone());
|
||||
let grid_plugin = flowy_grid::event_map::init(grid_manager.clone());
|
||||
let document_plugin = flowy_document::event_map::init(document_manager.clone());
|
||||
vec![user_plugin, folder_plugin, network_plugin, grid_plugin, document_plugin]
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
use crate::FlowySDKTest;
|
||||
use flowy_user::{entities::UserProfilePB, errors::FlowyError};
|
||||
use lib_dispatch::prelude::{EventDispatcher, EventResponse, FromBytes, ModuleRequest, StatusCode, ToBytes, *};
|
||||
use lib_dispatch::prelude::{
|
||||
AFPluginDispatcher, AFPluginFromBytes, AFPluginRequest, EventResponse, StatusCode, ToBytes, *,
|
||||
};
|
||||
use std::{
|
||||
convert::TryFrom,
|
||||
fmt::{Debug, Display},
|
||||
@ -30,7 +32,7 @@ pub struct EventBuilder<E> {
|
||||
|
||||
impl<E> EventBuilder<E>
|
||||
where
|
||||
E: FromBytes + Debug,
|
||||
E: AFPluginFromBytes + Debug,
|
||||
{
|
||||
fn test(context: TestContext) -> Self {
|
||||
Self {
|
||||
@ -60,27 +62,27 @@ where
|
||||
where
|
||||
Event: Eq + Hash + Debug + Clone + Display,
|
||||
{
|
||||
self.context.request = Some(ModuleRequest::new(event));
|
||||
self.context.request = Some(AFPluginRequest::new(event));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn sync_send(mut self) -> Self {
|
||||
let request = self.get_request();
|
||||
let resp = EventDispatcher::sync_send(self.dispatch(), request);
|
||||
let resp = AFPluginDispatcher::sync_send(self.dispatch(), request);
|
||||
self.context.response = Some(resp);
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn async_send(mut self) -> Self {
|
||||
let request = self.get_request();
|
||||
let resp = EventDispatcher::async_send(self.dispatch(), request).await;
|
||||
let resp = AFPluginDispatcher::async_send(self.dispatch(), request).await;
|
||||
self.context.response = Some(resp);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn parse<R>(self) -> R
|
||||
where
|
||||
R: FromBytes,
|
||||
R: AFPluginFromBytes,
|
||||
{
|
||||
let response = self.get_response();
|
||||
match response.clone().parse::<R, E>() {
|
||||
@ -105,7 +107,7 @@ where
|
||||
pub fn error(self) -> E {
|
||||
let response = self.get_response();
|
||||
assert_eq!(response.status_code, StatusCode::Err);
|
||||
<Data<E>>::try_from(response.payload).unwrap().into_inner()
|
||||
<AFPluginData<E>>::try_from(response.payload).unwrap().into_inner()
|
||||
}
|
||||
|
||||
pub fn assert_error(self) -> Self {
|
||||
@ -118,7 +120,7 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
fn dispatch(&self) -> Arc<EventDispatcher> {
|
||||
fn dispatch(&self) -> Arc<AFPluginDispatcher> {
|
||||
self.context.sdk.dispatcher()
|
||||
}
|
||||
|
||||
@ -130,7 +132,7 @@ where
|
||||
.clone()
|
||||
}
|
||||
|
||||
fn get_request(&mut self) -> ModuleRequest {
|
||||
fn get_request(&mut self) -> AFPluginRequest {
|
||||
self.context.request.take().expect("must call event first")
|
||||
}
|
||||
}
|
||||
@ -138,7 +140,7 @@ where
|
||||
#[derive(Clone)]
|
||||
pub struct TestContext {
|
||||
pub sdk: FlowySDKTest,
|
||||
request: Option<ModuleRequest>,
|
||||
request: Option<AFPluginRequest>,
|
||||
response: Option<EventResponse>,
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ use flowy_user::{
|
||||
errors::FlowyError,
|
||||
event_map::UserEvent::{InitUser, SignIn, SignOut, SignUp},
|
||||
};
|
||||
use lib_dispatch::prelude::{EventDispatcher, ModuleRequest, ToBytes};
|
||||
use lib_dispatch::prelude::{AFPluginDispatcher, AFPluginRequest, ToBytes};
|
||||
use std::{fs, path::PathBuf, sync::Arc};
|
||||
|
||||
pub struct ViewTest {
|
||||
@ -161,7 +161,7 @@ pub struct SignUpContext {
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
pub fn sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
|
||||
pub fn sign_up(dispatch: Arc<AFPluginDispatcher>) -> SignUpContext {
|
||||
let password = login_password();
|
||||
let payload = SignUpPayloadPB {
|
||||
email: random_email(),
|
||||
@ -171,8 +171,8 @@ pub fn sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
|
||||
.into_bytes()
|
||||
.unwrap();
|
||||
|
||||
let request = ModuleRequest::new(SignUp).payload(payload);
|
||||
let user_profile = EventDispatcher::sync_send(dispatch, request)
|
||||
let request = AFPluginRequest::new(SignUp).payload(payload);
|
||||
let user_profile = AFPluginDispatcher::sync_send(dispatch, request)
|
||||
.parse::<UserProfilePB, FlowyError>()
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@ -180,7 +180,7 @@ pub fn sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
|
||||
SignUpContext { user_profile, password }
|
||||
}
|
||||
|
||||
pub async fn async_sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
|
||||
pub async fn async_sign_up(dispatch: Arc<AFPluginDispatcher>) -> SignUpContext {
|
||||
let password = login_password();
|
||||
let email = random_email();
|
||||
let payload = SignUpPayloadPB {
|
||||
@ -191,8 +191,8 @@ pub async fn async_sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
|
||||
.into_bytes()
|
||||
.unwrap();
|
||||
|
||||
let request = ModuleRequest::new(SignUp).payload(payload);
|
||||
let user_profile = EventDispatcher::async_send(dispatch.clone(), request)
|
||||
let request = AFPluginRequest::new(SignUp).payload(payload);
|
||||
let user_profile = AFPluginDispatcher::async_send(dispatch.clone(), request)
|
||||
.await
|
||||
.parse::<UserProfilePB, FlowyError>()
|
||||
.unwrap()
|
||||
@ -202,13 +202,13 @@ pub async fn async_sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
|
||||
SignUpContext { user_profile, password }
|
||||
}
|
||||
|
||||
pub async fn init_user_setting(dispatch: Arc<EventDispatcher>) {
|
||||
let request = ModuleRequest::new(InitUser);
|
||||
let _ = EventDispatcher::async_send(dispatch.clone(), request).await;
|
||||
pub async fn init_user_setting(dispatch: Arc<AFPluginDispatcher>) {
|
||||
let request = AFPluginRequest::new(InitUser);
|
||||
let _ = AFPluginDispatcher::async_send(dispatch.clone(), request).await;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn sign_in(dispatch: Arc<EventDispatcher>) -> UserProfilePB {
|
||||
fn sign_in(dispatch: Arc<AFPluginDispatcher>) -> UserProfilePB {
|
||||
let payload = SignInPayloadPB {
|
||||
email: login_email(),
|
||||
password: login_password(),
|
||||
@ -217,14 +217,14 @@ fn sign_in(dispatch: Arc<EventDispatcher>) -> UserProfilePB {
|
||||
.into_bytes()
|
||||
.unwrap();
|
||||
|
||||
let request = ModuleRequest::new(SignIn).payload(payload);
|
||||
EventDispatcher::sync_send(dispatch, request)
|
||||
let request = AFPluginRequest::new(SignIn).payload(payload);
|
||||
AFPluginDispatcher::sync_send(dispatch, request)
|
||||
.parse::<UserProfilePB, FlowyError>()
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn logout(dispatch: Arc<EventDispatcher>) {
|
||||
let _ = EventDispatcher::sync_send(dispatch, ModuleRequest::new(SignOut));
|
||||
fn logout(dispatch: Arc<AFPluginDispatcher>) {
|
||||
let _ = AFPluginDispatcher::sync_send(dispatch, AFPluginRequest::new(SignOut));
|
||||
}
|
||||
|
@ -6,10 +6,10 @@ use lib_dispatch::prelude::*;
|
||||
use lib_infra::future::FutureResult;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn create(user_session: Arc<UserSession>) -> Module {
|
||||
Module::new()
|
||||
pub fn init(user_session: Arc<UserSession>) -> AFPlugin {
|
||||
AFPlugin::new()
|
||||
.name("Flowy-User")
|
||||
.data(user_session)
|
||||
.state(user_session)
|
||||
.event(UserEvent::SignIn, sign_in)
|
||||
.event(UserEvent::SignUp, sign_up)
|
||||
.event(UserEvent::InitUser, init_user_handler)
|
||||
|
@ -7,8 +7,8 @@ use std::{convert::TryInto, sync::Arc};
|
||||
// tracing instrument 👉🏻 https://docs.rs/tracing/0.1.26/tracing/attr.instrument.html
|
||||
#[tracing::instrument(level = "debug", name = "sign_in", skip(data, session), fields(email = %data.email), err)]
|
||||
pub async fn sign_in(
|
||||
data: Data<SignInPayloadPB>,
|
||||
session: AppData<Arc<UserSession>>,
|
||||
data: AFPluginData<SignInPayloadPB>,
|
||||
session: AFPluginState<Arc<UserSession>>,
|
||||
) -> DataResult<UserProfilePB, FlowyError> {
|
||||
let params: SignInParams = data.into_inner().try_into()?;
|
||||
let user_profile = session.sign_in(params).await?;
|
||||
@ -26,8 +26,8 @@ pub async fn sign_in(
|
||||
err
|
||||
)]
|
||||
pub async fn sign_up(
|
||||
data: Data<SignUpPayloadPB>,
|
||||
session: AppData<Arc<UserSession>>,
|
||||
data: AFPluginData<SignUpPayloadPB>,
|
||||
session: AFPluginState<Arc<UserSession>>,
|
||||
) -> DataResult<UserProfilePB, FlowyError> {
|
||||
let params: SignUpParams = data.into_inner().try_into()?;
|
||||
let user_profile = session.sign_up(params).await?;
|
||||
|
@ -8,33 +8,35 @@ use lib_dispatch::prelude::*;
|
||||
use std::{convert::TryInto, sync::Arc};
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(session))]
|
||||
pub async fn init_user_handler(session: AppData<Arc<UserSession>>) -> Result<(), FlowyError> {
|
||||
pub async fn init_user_handler(session: AFPluginState<Arc<UserSession>>) -> Result<(), FlowyError> {
|
||||
let _ = session.init_user().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(session))]
|
||||
pub async fn check_user_handler(session: AppData<Arc<UserSession>>) -> DataResult<UserProfilePB, FlowyError> {
|
||||
pub async fn check_user_handler(session: AFPluginState<Arc<UserSession>>) -> DataResult<UserProfilePB, FlowyError> {
|
||||
let user_profile = session.check_user().await?;
|
||||
data_result(user_profile)
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(session))]
|
||||
pub async fn get_user_profile_handler(session: AppData<Arc<UserSession>>) -> DataResult<UserProfilePB, FlowyError> {
|
||||
pub async fn get_user_profile_handler(
|
||||
session: AFPluginState<Arc<UserSession>>,
|
||||
) -> DataResult<UserProfilePB, FlowyError> {
|
||||
let user_profile = session.get_user_profile().await?;
|
||||
data_result(user_profile)
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", name = "sign_out", skip(session))]
|
||||
pub async fn sign_out(session: AppData<Arc<UserSession>>) -> Result<(), FlowyError> {
|
||||
pub async fn sign_out(session: AFPluginState<Arc<UserSession>>) -> Result<(), FlowyError> {
|
||||
let _ = session.sign_out().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data, session))]
|
||||
pub async fn update_user_profile_handler(
|
||||
data: Data<UpdateUserProfilePayloadPB>,
|
||||
session: AppData<Arc<UserSession>>,
|
||||
data: AFPluginData<UpdateUserProfilePayloadPB>,
|
||||
session: AFPluginState<Arc<UserSession>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: UpdateUserProfileParams = data.into_inner().try_into()?;
|
||||
session.update_user_profile(params).await?;
|
||||
@ -44,7 +46,7 @@ pub async fn update_user_profile_handler(
|
||||
const APPEARANCE_SETTING_CACHE_KEY: &str = "appearance_settings";
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(data), err)]
|
||||
pub async fn set_appearance_setting(data: Data<AppearanceSettingsPB>) -> Result<(), FlowyError> {
|
||||
pub async fn set_appearance_setting(data: AFPluginData<AppearanceSettingsPB>) -> Result<(), FlowyError> {
|
||||
let mut setting = data.into_inner();
|
||||
if setting.theme.is_empty() {
|
||||
setting.theme = APPEARANCE_DEFAULT_THEME.to_string();
|
||||
@ -73,7 +75,7 @@ pub async fn get_appearance_setting() -> DataResult<AppearanceSettingsPB, FlowyE
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip_all, err)]
|
||||
pub async fn get_user_setting(session: AppData<Arc<UserSession>>) -> DataResult<UserSettingPB, FlowyError> {
|
||||
pub async fn get_user_setting(session: AFPluginState<Arc<UserSession>>) -> DataResult<UserSettingPB, FlowyError> {
|
||||
let user_setting = session.user_setting()?;
|
||||
data_result(user_setting)
|
||||
}
|
||||
|
@ -39,12 +39,12 @@ where
|
||||
|
||||
// From bytes
|
||||
|
||||
pub trait FromBytes: Sized {
|
||||
pub trait AFPluginFromBytes: Sized {
|
||||
fn parse_from_bytes(bytes: Bytes) -> Result<Self, DispatchError>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "use_protobuf")]
|
||||
impl<T> FromBytes for T
|
||||
impl<T> AFPluginFromBytes for T
|
||||
where
|
||||
// // https://stackoverflow.com/questions/62871045/tryfromu8-trait-bound-in-trait
|
||||
// T: for<'a> std::convert::TryFrom<&'a Bytes, Error =
|
||||
@ -67,7 +67,7 @@ where
|
||||
}
|
||||
|
||||
#[cfg(feature = "use_serde")]
|
||||
impl<T> FromBytes for T
|
||||
impl<T> AFPluginFromBytes for T
|
||||
where
|
||||
T: serde::de::DeserializeOwned + 'static,
|
||||
{
|
||||
|
@ -1,22 +1,22 @@
|
||||
use crate::{
|
||||
byte_trait::*,
|
||||
errors::{DispatchError, InternalError},
|
||||
request::{unexpected_none_payload, EventRequest, FromRequest, Payload},
|
||||
response::{EventResponse, Responder, ResponseBuilder},
|
||||
request::{unexpected_none_payload, AFPluginEventRequest, FromAFPluginRequest, Payload},
|
||||
response::{AFPluginResponder, EventResponse, ResponseBuilder},
|
||||
util::ready::{ready, Ready},
|
||||
};
|
||||
use bytes::Bytes;
|
||||
use std::ops;
|
||||
|
||||
pub struct Data<T>(pub T);
|
||||
pub struct AFPluginData<T>(pub T);
|
||||
|
||||
impl<T> Data<T> {
|
||||
impl<T> AFPluginData<T> {
|
||||
pub fn into_inner(self) -> T {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ops::Deref for Data<T> {
|
||||
impl<T> ops::Deref for AFPluginData<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &T {
|
||||
@ -24,36 +24,36 @@ impl<T> ops::Deref for Data<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ops::DerefMut for Data<T> {
|
||||
impl<T> ops::DerefMut for AFPluginData<T> {
|
||||
fn deref_mut(&mut self) -> &mut T {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> FromRequest for Data<T>
|
||||
impl<T> FromAFPluginRequest for AFPluginData<T>
|
||||
where
|
||||
T: FromBytes + 'static,
|
||||
T: AFPluginFromBytes + 'static,
|
||||
{
|
||||
type Error = DispatchError;
|
||||
type Future = Ready<Result<Self, DispatchError>>;
|
||||
|
||||
#[inline]
|
||||
fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
|
||||
fn from_request(req: &AFPluginEventRequest, payload: &mut Payload) -> Self::Future {
|
||||
match payload {
|
||||
Payload::None => ready(Err(unexpected_none_payload(req))),
|
||||
Payload::Bytes(bytes) => match T::parse_from_bytes(bytes.clone()) {
|
||||
Ok(data) => ready(Ok(Data(data))),
|
||||
Ok(data) => ready(Ok(AFPluginData(data))),
|
||||
Err(e) => ready(Err(InternalError::DeserializeFromBytes(format!("{}", e)).into())),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Responder for Data<T>
|
||||
impl<T> AFPluginResponder for AFPluginData<T>
|
||||
where
|
||||
T: ToBytes,
|
||||
{
|
||||
fn respond_to(self, _request: &EventRequest) -> EventResponse {
|
||||
fn respond_to(self, _request: &AFPluginEventRequest) -> EventResponse {
|
||||
match self.into_inner().into_bytes() {
|
||||
Ok(bytes) => {
|
||||
log::trace!("Serialize Data: {:?} to event response", std::any::type_name::<T>());
|
||||
@ -64,29 +64,29 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::convert::TryFrom<&Payload> for Data<T>
|
||||
impl<T> std::convert::TryFrom<&Payload> for AFPluginData<T>
|
||||
where
|
||||
T: FromBytes,
|
||||
T: AFPluginFromBytes,
|
||||
{
|
||||
type Error = DispatchError;
|
||||
fn try_from(payload: &Payload) -> Result<Data<T>, Self::Error> {
|
||||
fn try_from(payload: &Payload) -> Result<AFPluginData<T>, Self::Error> {
|
||||
parse_payload(payload)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::convert::TryFrom<Payload> for Data<T>
|
||||
impl<T> std::convert::TryFrom<Payload> for AFPluginData<T>
|
||||
where
|
||||
T: FromBytes,
|
||||
T: AFPluginFromBytes,
|
||||
{
|
||||
type Error = DispatchError;
|
||||
fn try_from(payload: Payload) -> Result<Data<T>, Self::Error> {
|
||||
fn try_from(payload: Payload) -> Result<AFPluginData<T>, Self::Error> {
|
||||
parse_payload(&payload)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_payload<T>(payload: &Payload) -> Result<Data<T>, DispatchError>
|
||||
fn parse_payload<T>(payload: &Payload) -> Result<AFPluginData<T>, DispatchError>
|
||||
where
|
||||
T: FromBytes,
|
||||
T: AFPluginFromBytes,
|
||||
{
|
||||
match payload {
|
||||
Payload::None => Err(InternalError::UnexpectedNone(format!(
|
||||
@ -96,12 +96,12 @@ where
|
||||
.into()),
|
||||
Payload::Bytes(bytes) => {
|
||||
let data = T::parse_from_bytes(bytes.clone())?;
|
||||
Ok(Data(data))
|
||||
Ok(AFPluginData(data))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::convert::TryInto<Payload> for Data<T>
|
||||
impl<T> std::convert::TryInto<Payload> for AFPluginData<T>
|
||||
where
|
||||
T: ToBytes,
|
||||
{
|
||||
@ -114,7 +114,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl ToBytes for Data<String> {
|
||||
impl ToBytes for AFPluginData<String> {
|
||||
fn into_bytes(self) -> Result<Bytes, DispatchError> {
|
||||
Ok(Bytes::from(self.0))
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
use crate::runtime::FlowyRuntime;
|
||||
use crate::runtime::AFPluginRuntime;
|
||||
use crate::{
|
||||
errors::{DispatchError, Error, InternalError},
|
||||
module::{as_module_map, Module, ModuleMap, ModuleRequest},
|
||||
module::{as_plugin_map, AFPlugin, AFPluginMap, AFPluginRequest},
|
||||
response::EventResponse,
|
||||
service::{Service, ServiceFactory},
|
||||
service::{AFPluginServiceFactory, Service},
|
||||
};
|
||||
use derivative::*;
|
||||
use futures_core::future::BoxFuture;
|
||||
@ -12,42 +12,43 @@ use pin_project::pin_project;
|
||||
use std::{future::Future, sync::Arc};
|
||||
use tokio::macros::support::{Pin, Poll};
|
||||
|
||||
pub struct EventDispatcher {
|
||||
module_map: ModuleMap,
|
||||
runtime: FlowyRuntime,
|
||||
pub struct AFPluginDispatcher {
|
||||
plugins: AFPluginMap,
|
||||
runtime: AFPluginRuntime,
|
||||
}
|
||||
|
||||
impl EventDispatcher {
|
||||
pub fn construct<F>(runtime: FlowyRuntime, module_factory: F) -> EventDispatcher
|
||||
impl AFPluginDispatcher {
|
||||
pub fn construct<F>(runtime: AFPluginRuntime, module_factory: F) -> AFPluginDispatcher
|
||||
where
|
||||
F: FnOnce() -> Vec<Module>,
|
||||
F: FnOnce() -> Vec<AFPlugin>,
|
||||
{
|
||||
let modules = module_factory();
|
||||
tracing::trace!("{}", module_info(&modules));
|
||||
let module_map = as_module_map(modules);
|
||||
|
||||
EventDispatcher { module_map, runtime }
|
||||
let plugins = module_factory();
|
||||
tracing::trace!("{}", plugin_info(&plugins));
|
||||
AFPluginDispatcher {
|
||||
plugins: as_plugin_map(plugins),
|
||||
runtime,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn async_send<Req>(dispatch: Arc<EventDispatcher>, request: Req) -> DispatchFuture<EventResponse>
|
||||
pub fn async_send<Req>(dispatch: Arc<AFPluginDispatcher>, request: Req) -> DispatchFuture<EventResponse>
|
||||
where
|
||||
Req: std::convert::Into<ModuleRequest>,
|
||||
Req: std::convert::Into<AFPluginRequest>,
|
||||
{
|
||||
EventDispatcher::async_send_with_callback(dispatch, request, |_| Box::pin(async {}))
|
||||
AFPluginDispatcher::async_send_with_callback(dispatch, request, |_| Box::pin(async {}))
|
||||
}
|
||||
|
||||
pub fn async_send_with_callback<Req, Callback>(
|
||||
dispatch: Arc<EventDispatcher>,
|
||||
dispatch: Arc<AFPluginDispatcher>,
|
||||
request: Req,
|
||||
callback: Callback,
|
||||
) -> DispatchFuture<EventResponse>
|
||||
where
|
||||
Req: std::convert::Into<ModuleRequest>,
|
||||
Req: std::convert::Into<AFPluginRequest>,
|
||||
Callback: FnOnce(EventResponse) -> BoxFuture<'static, ()> + 'static + Send + Sync,
|
||||
{
|
||||
let request: ModuleRequest = request.into();
|
||||
let module_map = dispatch.module_map.clone();
|
||||
let service = Box::new(DispatchService { module_map });
|
||||
let request: AFPluginRequest = request.into();
|
||||
let plugins = dispatch.plugins.clone();
|
||||
let service = Box::new(DispatchService { plugins });
|
||||
tracing::trace!("Async event: {:?}", &request.event);
|
||||
let service_ctx = DispatchContext {
|
||||
request,
|
||||
@ -72,9 +73,9 @@ impl EventDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sync_send(dispatch: Arc<EventDispatcher>, request: ModuleRequest) -> EventResponse {
|
||||
pub fn sync_send(dispatch: Arc<AFPluginDispatcher>, request: AFPluginRequest) -> EventResponse {
|
||||
futures::executor::block_on(async {
|
||||
EventDispatcher::async_send_with_callback(dispatch, request, |_| Box::pin(async {})).await
|
||||
AFPluginDispatcher::async_send_with_callback(dispatch, request, |_| Box::pin(async {})).await
|
||||
})
|
||||
}
|
||||
|
||||
@ -109,20 +110,20 @@ pub type BoxFutureCallback = Box<dyn FnOnce(EventResponse) -> BoxFuture<'static,
|
||||
#[derive(Derivative)]
|
||||
#[derivative(Debug)]
|
||||
pub struct DispatchContext {
|
||||
pub request: ModuleRequest,
|
||||
pub request: AFPluginRequest,
|
||||
#[derivative(Debug = "ignore")]
|
||||
pub callback: Option<BoxFutureCallback>,
|
||||
}
|
||||
|
||||
impl DispatchContext {
|
||||
pub(crate) fn into_parts(self) -> (ModuleRequest, Option<BoxFutureCallback>) {
|
||||
pub(crate) fn into_parts(self) -> (AFPluginRequest, Option<BoxFutureCallback>) {
|
||||
let DispatchContext { request, callback } = self;
|
||||
(request, callback)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct DispatchService {
|
||||
pub(crate) module_map: ModuleMap,
|
||||
pub(crate) plugins: AFPluginMap,
|
||||
}
|
||||
|
||||
impl Service<DispatchContext> for DispatchService {
|
||||
@ -135,7 +136,7 @@ impl Service<DispatchContext> for DispatchService {
|
||||
tracing::instrument(name = "DispatchService", level = "debug", skip(self, ctx))
|
||||
)]
|
||||
fn call(&self, ctx: DispatchContext) -> Self::Future {
|
||||
let module_map = self.module_map.clone();
|
||||
let module_map = self.plugins.clone();
|
||||
let (request, callback) = ctx.into_parts();
|
||||
|
||||
Box::pin(async move {
|
||||
@ -168,17 +169,17 @@ impl Service<DispatchContext> for DispatchService {
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn module_info(modules: &[Module]) -> String {
|
||||
let mut info = format!("{} modules loaded\n", modules.len());
|
||||
for module in modules {
|
||||
fn plugin_info(plugins: &[AFPlugin]) -> String {
|
||||
let mut info = format!("{} plugins loaded\n", plugins.len());
|
||||
for module in plugins {
|
||||
info.push_str(&format!("-> {} loaded \n", module.name));
|
||||
}
|
||||
info
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn print_module_map_info(module_map: &ModuleMap) {
|
||||
module_map.iter().for_each(|(k, v)| {
|
||||
tracing::info!("Event: {:?} module: {:?}", k, v.name);
|
||||
fn print_plugins(plugins: &AFPluginMap) {
|
||||
plugins.iter().for_each(|(k, v)| {
|
||||
tracing::info!("Event: {:?} plugin : {:?}", k, v.name);
|
||||
})
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
byte_trait::FromBytes,
|
||||
request::EventRequest,
|
||||
byte_trait::AFPluginFromBytes,
|
||||
request::AFPluginEventRequest,
|
||||
response::{EventResponse, ResponseBuilder},
|
||||
};
|
||||
use bytes::Bytes;
|
||||
@ -54,8 +54,8 @@ impl std::error::Error for DispatchError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SendError<EventRequest>> for DispatchError {
|
||||
fn from(err: SendError<EventRequest>) -> Self {
|
||||
impl From<SendError<AFPluginEventRequest>> for DispatchError {
|
||||
fn from(err: SendError<AFPluginEventRequest>) -> Self {
|
||||
InternalError::Other(format!("{}", err)).into()
|
||||
}
|
||||
}
|
||||
@ -73,7 +73,7 @@ impl From<protobuf::ProtobufError> for DispatchError {
|
||||
}
|
||||
}
|
||||
|
||||
impl FromBytes for DispatchError {
|
||||
impl AFPluginFromBytes for DispatchError {
|
||||
fn parse_from_bytes(bytes: Bytes) -> Result<Self, DispatchError> {
|
||||
let s = String::from_utf8(bytes.to_vec()).unwrap();
|
||||
Ok(InternalError::DeserializeFromBytes(s).into())
|
||||
|
@ -4,46 +4,40 @@ use std::{
|
||||
};
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct ModuleDataMap {
|
||||
map: HashMap<TypeId, Box<dyn Any + Sync + Send>>,
|
||||
}
|
||||
pub struct AFPluginStateMap(HashMap<TypeId, Box<dyn Any + Sync + Send>>);
|
||||
|
||||
impl ModuleDataMap {
|
||||
impl AFPluginStateMap {
|
||||
#[inline]
|
||||
pub fn new() -> ModuleDataMap {
|
||||
ModuleDataMap {
|
||||
map: HashMap::default(),
|
||||
}
|
||||
pub fn new() -> AFPluginStateMap {
|
||||
AFPluginStateMap(HashMap::default())
|
||||
}
|
||||
|
||||
pub fn insert<T>(&mut self, val: T) -> Option<T>
|
||||
where
|
||||
T: 'static + Send + Sync,
|
||||
{
|
||||
self.map
|
||||
.insert(TypeId::of::<T>(), Box::new(val))
|
||||
.and_then(downcast_owned)
|
||||
self.0.insert(TypeId::of::<T>(), Box::new(val)).and_then(downcast_owned)
|
||||
}
|
||||
|
||||
pub fn remove<T>(&mut self) -> Option<T>
|
||||
where
|
||||
T: 'static + Send + Sync,
|
||||
{
|
||||
self.map.remove(&TypeId::of::<T>()).and_then(downcast_owned)
|
||||
self.0.remove(&TypeId::of::<T>()).and_then(downcast_owned)
|
||||
}
|
||||
|
||||
pub fn get<T>(&self) -> Option<&T>
|
||||
where
|
||||
T: 'static + Send + Sync,
|
||||
{
|
||||
self.map.get(&TypeId::of::<T>()).and_then(|boxed| boxed.downcast_ref())
|
||||
self.0.get(&TypeId::of::<T>()).and_then(|boxed| boxed.downcast_ref())
|
||||
}
|
||||
|
||||
pub fn get_mut<T>(&mut self) -> Option<&mut T>
|
||||
where
|
||||
T: 'static + Send + Sync,
|
||||
{
|
||||
self.map
|
||||
self.0
|
||||
.get_mut(&TypeId::of::<T>())
|
||||
.and_then(|boxed| boxed.downcast_mut())
|
||||
}
|
||||
@ -52,11 +46,11 @@ impl ModuleDataMap {
|
||||
where
|
||||
T: 'static + Send + Sync,
|
||||
{
|
||||
self.map.contains_key(&TypeId::of::<T>())
|
||||
self.0.contains_key(&TypeId::of::<T>())
|
||||
}
|
||||
|
||||
pub fn extend(&mut self, other: ModuleDataMap) {
|
||||
self.map.extend(other.map);
|
||||
pub fn extend(&mut self, other: AFPluginStateMap) {
|
||||
self.0.extend(other.0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,18 +1,18 @@
|
||||
use crate::{
|
||||
errors::{DispatchError, InternalError},
|
||||
request::{payload::Payload, EventRequest, FromRequest},
|
||||
request::{payload::Payload, AFPluginEventRequest, FromAFPluginRequest},
|
||||
util::ready::{ready, Ready},
|
||||
};
|
||||
use std::{any::type_name, ops::Deref, sync::Arc};
|
||||
|
||||
pub struct AppData<T: ?Sized + Send + Sync>(Arc<T>);
|
||||
pub struct AFPluginState<T: ?Sized + Send + Sync>(Arc<T>);
|
||||
|
||||
impl<T> AppData<T>
|
||||
impl<T> AFPluginState<T>
|
||||
where
|
||||
T: Send + Sync,
|
||||
{
|
||||
pub fn new(data: T) -> Self {
|
||||
AppData(Arc::new(data))
|
||||
AFPluginState(Arc::new(data))
|
||||
}
|
||||
|
||||
pub fn get_ref(&self) -> &T {
|
||||
@ -20,7 +20,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref for AppData<T>
|
||||
impl<T> Deref for AFPluginState<T>
|
||||
where
|
||||
T: ?Sized + Send + Sync,
|
||||
{
|
||||
@ -31,25 +31,25 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for AppData<T>
|
||||
impl<T> Clone for AFPluginState<T>
|
||||
where
|
||||
T: ?Sized + Send + Sync,
|
||||
{
|
||||
fn clone(&self) -> AppData<T> {
|
||||
AppData(self.0.clone())
|
||||
fn clone(&self) -> AFPluginState<T> {
|
||||
AFPluginState(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<Arc<T>> for AppData<T>
|
||||
impl<T> From<Arc<T>> for AFPluginState<T>
|
||||
where
|
||||
T: ?Sized + Send + Sync,
|
||||
{
|
||||
fn from(arc: Arc<T>) -> Self {
|
||||
AppData(arc)
|
||||
AFPluginState(arc)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> FromRequest for AppData<T>
|
||||
impl<T> FromAFPluginRequest for AFPluginState<T>
|
||||
where
|
||||
T: ?Sized + Send + Sync + 'static,
|
||||
{
|
||||
@ -57,11 +57,11 @@ where
|
||||
type Future = Ready<Result<Self, DispatchError>>;
|
||||
|
||||
#[inline]
|
||||
fn from_request(req: &EventRequest, _: &mut Payload) -> Self::Future {
|
||||
if let Some(data) = req.module_data::<AppData<T>>() {
|
||||
ready(Ok(data.clone()))
|
||||
fn from_request(req: &AFPluginEventRequest, _: &mut Payload) -> Self::Future {
|
||||
if let Some(state) = req.get_state::<AFPluginState<T>>() {
|
||||
ready(Ok(state.clone()))
|
||||
} else {
|
||||
let msg = format!("Failed to get the module data of type: {}", type_name::<T>());
|
||||
let msg = format!("Failed to get the plugin state of type: {}", type_name::<T>());
|
||||
log::error!("{}", msg,);
|
||||
ready(Err(InternalError::Other(msg).into()))
|
||||
}
|
||||
|
@ -1,3 +1,18 @@
|
||||
use crate::{
|
||||
errors::{DispatchError, InternalError},
|
||||
module::{container::AFPluginStateMap, AFPluginState},
|
||||
request::{payload::Payload, AFPluginEventRequest, FromAFPluginRequest},
|
||||
response::{AFPluginResponder, EventResponse},
|
||||
service::{
|
||||
factory, AFPluginHandler, AFPluginHandlerService, AFPluginServiceFactory, BoxService, BoxServiceFactory,
|
||||
Service, ServiceRequest, ServiceResponse,
|
||||
},
|
||||
};
|
||||
use futures_core::future::BoxFuture;
|
||||
use futures_core::ready;
|
||||
use nanoid::nanoid;
|
||||
use pin_project::pin_project;
|
||||
use std::sync::Arc;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fmt,
|
||||
@ -8,66 +23,48 @@ use std::{
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use futures_core::ready;
|
||||
use pin_project::pin_project;
|
||||
|
||||
use crate::{
|
||||
errors::{DispatchError, InternalError},
|
||||
module::{container::ModuleDataMap, AppData},
|
||||
request::{payload::Payload, EventRequest, FromRequest},
|
||||
response::{EventResponse, Responder},
|
||||
service::{
|
||||
factory, BoxService, BoxServiceFactory, Handler, HandlerService, Service, ServiceFactory, ServiceRequest,
|
||||
ServiceResponse,
|
||||
},
|
||||
};
|
||||
use futures_core::future::BoxFuture;
|
||||
use nanoid::nanoid;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub type ModuleMap = Arc<HashMap<Event, Arc<Module>>>;
|
||||
pub(crate) fn as_module_map(modules: Vec<Module>) -> ModuleMap {
|
||||
let mut module_map = HashMap::new();
|
||||
modules.into_iter().for_each(|m| {
|
||||
pub type AFPluginMap = Arc<HashMap<AFPluginEvent, Arc<AFPlugin>>>;
|
||||
pub(crate) fn as_plugin_map(plugins: Vec<AFPlugin>) -> AFPluginMap {
|
||||
let mut plugin_map = HashMap::new();
|
||||
plugins.into_iter().for_each(|m| {
|
||||
let events = m.events();
|
||||
let module = Arc::new(m);
|
||||
let plugins = Arc::new(m);
|
||||
events.into_iter().for_each(|e| {
|
||||
module_map.insert(e, module.clone());
|
||||
plugin_map.insert(e, plugins.clone());
|
||||
});
|
||||
});
|
||||
Arc::new(module_map)
|
||||
Arc::new(plugin_map)
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
|
||||
pub struct Event(String);
|
||||
pub struct AFPluginEvent(String);
|
||||
|
||||
impl<T: Display + Eq + Hash + Debug + Clone> std::convert::From<T> for Event {
|
||||
impl<T: Display + Eq + Hash + Debug + Clone> std::convert::From<T> for AFPluginEvent {
|
||||
fn from(t: T) -> Self {
|
||||
Event(format!("{}", t))
|
||||
AFPluginEvent(format!("{}", t))
|
||||
}
|
||||
}
|
||||
|
||||
pub type EventServiceFactory = BoxServiceFactory<(), ServiceRequest, ServiceResponse, DispatchError>;
|
||||
|
||||
pub struct Module {
|
||||
pub struct AFPlugin {
|
||||
pub name: String,
|
||||
module_data: Arc<ModuleDataMap>,
|
||||
service_map: Arc<HashMap<Event, EventServiceFactory>>,
|
||||
states: Arc<AFPluginStateMap>,
|
||||
event_service_factory:
|
||||
Arc<HashMap<AFPluginEvent, BoxServiceFactory<(), ServiceRequest, ServiceResponse, DispatchError>>>,
|
||||
}
|
||||
|
||||
impl std::default::Default for Module {
|
||||
impl std::default::Default for AFPlugin {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "".to_owned(),
|
||||
module_data: Arc::new(ModuleDataMap::new()),
|
||||
service_map: Arc::new(HashMap::new()),
|
||||
states: Arc::new(AFPluginStateMap::new()),
|
||||
event_service_factory: Arc::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Module {
|
||||
impl AFPlugin {
|
||||
pub fn new() -> Self {
|
||||
Module::default()
|
||||
AFPlugin::default()
|
||||
}
|
||||
|
||||
pub fn name(mut self, s: &str) -> Self {
|
||||
@ -75,48 +72,48 @@ impl Module {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn data<D: 'static + Send + Sync>(mut self, data: D) -> Self {
|
||||
Arc::get_mut(&mut self.module_data).unwrap().insert(AppData::new(data));
|
||||
pub fn state<D: 'static + Send + Sync>(mut self, data: D) -> Self {
|
||||
Arc::get_mut(&mut self.states).unwrap().insert(AFPluginState::new(data));
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub fn event<E, H, T, R>(mut self, event: E, handler: H) -> Self
|
||||
where
|
||||
H: Handler<T, R>,
|
||||
T: FromRequest + 'static + Send + Sync,
|
||||
<T as FromRequest>::Future: Sync + Send,
|
||||
H: AFPluginHandler<T, R>,
|
||||
T: FromAFPluginRequest + 'static + Send + Sync,
|
||||
<T as FromAFPluginRequest>::Future: Sync + Send,
|
||||
R: Future + 'static + Send + Sync,
|
||||
R::Output: Responder + 'static,
|
||||
R::Output: AFPluginResponder + 'static,
|
||||
E: Eq + Hash + Debug + Clone + Display,
|
||||
{
|
||||
let event: Event = event.into();
|
||||
if self.service_map.contains_key(&event) {
|
||||
let event: AFPluginEvent = event.into();
|
||||
if self.event_service_factory.contains_key(&event) {
|
||||
log::error!("Duplicate Event: {:?}", &event);
|
||||
}
|
||||
|
||||
Arc::get_mut(&mut self.service_map)
|
||||
Arc::get_mut(&mut self.event_service_factory)
|
||||
.unwrap()
|
||||
.insert(event, factory(HandlerService::new(handler)));
|
||||
.insert(event, factory(AFPluginHandlerService::new(handler)));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn events(&self) -> Vec<Event> {
|
||||
self.service_map.keys().cloned().collect::<Vec<_>>()
|
||||
pub fn events(&self) -> Vec<AFPluginEvent> {
|
||||
self.event_service_factory.keys().cloned().collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ModuleRequest {
|
||||
pub struct AFPluginRequest {
|
||||
pub id: String,
|
||||
pub event: Event,
|
||||
pub event: AFPluginEvent,
|
||||
pub(crate) payload: Payload,
|
||||
}
|
||||
|
||||
impl ModuleRequest {
|
||||
impl AFPluginRequest {
|
||||
pub fn new<E>(event: E) -> Self
|
||||
where
|
||||
E: Into<Event>,
|
||||
E: Into<AFPluginEvent>,
|
||||
{
|
||||
Self {
|
||||
id: nanoid!(6),
|
||||
@ -134,52 +131,48 @@ impl ModuleRequest {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ModuleRequest {
|
||||
impl std::fmt::Display for AFPluginRequest {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}:{:?}", self.id, self.event)
|
||||
}
|
||||
}
|
||||
|
||||
impl ServiceFactory<ModuleRequest> for Module {
|
||||
impl AFPluginServiceFactory<AFPluginRequest> for AFPlugin {
|
||||
type Response = EventResponse;
|
||||
type Error = DispatchError;
|
||||
type Service = BoxService<ModuleRequest, Self::Response, Self::Error>;
|
||||
type Service = BoxService<AFPluginRequest, Self::Response, Self::Error>;
|
||||
type Context = ();
|
||||
type Future = BoxFuture<'static, Result<Self::Service, Self::Error>>;
|
||||
|
||||
fn new_service(&self, _cfg: Self::Context) -> Self::Future {
|
||||
let service_map = self.service_map.clone();
|
||||
let module_data = self.module_data.clone();
|
||||
let services = self.event_service_factory.clone();
|
||||
let states = self.states.clone();
|
||||
Box::pin(async move {
|
||||
let service = ModuleService {
|
||||
service_map,
|
||||
module_data,
|
||||
};
|
||||
let module_service = Box::new(service) as Self::Service;
|
||||
Ok(module_service)
|
||||
let service = AFPluginService { services, states };
|
||||
Ok(Box::new(service) as Self::Service)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ModuleService {
|
||||
service_map: Arc<HashMap<Event, EventServiceFactory>>,
|
||||
module_data: Arc<ModuleDataMap>,
|
||||
pub struct AFPluginService {
|
||||
services: Arc<HashMap<AFPluginEvent, BoxServiceFactory<(), ServiceRequest, ServiceResponse, DispatchError>>>,
|
||||
states: Arc<AFPluginStateMap>,
|
||||
}
|
||||
|
||||
impl Service<ModuleRequest> for ModuleService {
|
||||
impl Service<AFPluginRequest> for AFPluginService {
|
||||
type Response = EventResponse;
|
||||
type Error = DispatchError;
|
||||
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
||||
fn call(&self, request: ModuleRequest) -> Self::Future {
|
||||
let ModuleRequest { id, event, payload } = request;
|
||||
let module_data = self.module_data.clone();
|
||||
let request = EventRequest::new(id, event, module_data);
|
||||
fn call(&self, request: AFPluginRequest) -> Self::Future {
|
||||
let AFPluginRequest { id, event, payload } = request;
|
||||
let states = self.states.clone();
|
||||
let request = AFPluginEventRequest::new(id, event, states);
|
||||
|
||||
match self.service_map.get(&request.event) {
|
||||
match self.services.get(&request.event) {
|
||||
Some(factory) => {
|
||||
let service_fut = factory.new_service(());
|
||||
let fut = ModuleServiceFuture {
|
||||
let fut = AFPluginServiceFuture {
|
||||
fut: Box::pin(async {
|
||||
let service = service_fut.await?;
|
||||
let service_req = ServiceRequest::new(request, payload);
|
||||
@ -197,12 +190,12 @@ impl Service<ModuleRequest> for ModuleService {
|
||||
}
|
||||
|
||||
#[pin_project]
|
||||
pub struct ModuleServiceFuture {
|
||||
pub struct AFPluginServiceFuture {
|
||||
#[pin]
|
||||
fut: BoxFuture<'static, Result<ServiceResponse, DispatchError>>,
|
||||
}
|
||||
|
||||
impl Future for ModuleServiceFuture {
|
||||
impl Future for AFPluginServiceFuture {
|
||||
type Output = Result<EventResponse, DispatchError>;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
@ -210,36 +203,3 @@ impl Future for ModuleServiceFuture {
|
||||
Poll::Ready(Ok(response))
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::*;
|
||||
// use crate::rt::Runtime;
|
||||
// use futures_util::{future, pin_mut};
|
||||
// use tokio::sync::mpsc::unbounded_channel;
|
||||
// pub async fn hello_service() -> String { "hello".to_string() }
|
||||
// #[test]
|
||||
// fn test() {
|
||||
// let runtime = Runtime::new().unwrap();
|
||||
// runtime.block_on(async {
|
||||
// let (sys_tx, mut sys_rx) = unbounded_channel::<SystemCommand>();
|
||||
// let event = "hello".to_string();
|
||||
// let module = Module::new(sys_tx).event(event.clone(),
|
||||
// hello_service); let req_tx = module.req_tx();
|
||||
// let event = async move {
|
||||
// let request = EventRequest::new(event.clone());
|
||||
// req_tx.send(request).unwrap();
|
||||
//
|
||||
// match sys_rx.recv().await {
|
||||
// Some(cmd) => {
|
||||
// tracing::info!("{:?}", cmd);
|
||||
// },
|
||||
// None => panic!(""),
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// pin_mut!(module, event);
|
||||
// future::select(module, event).await;
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
@ -2,7 +2,7 @@ use std::future::Future;
|
||||
|
||||
use crate::{
|
||||
errors::{DispatchError, InternalError},
|
||||
module::{Event, ModuleDataMap},
|
||||
module::{AFPluginEvent, AFPluginStateMap},
|
||||
request::payload::Payload,
|
||||
util::ready::{ready, Ready},
|
||||
};
|
||||
@ -16,31 +16,31 @@ use std::{
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Derivative)]
|
||||
pub struct EventRequest {
|
||||
pub struct AFPluginEventRequest {
|
||||
#[allow(dead_code)]
|
||||
pub(crate) id: String,
|
||||
pub(crate) event: Event,
|
||||
pub(crate) event: AFPluginEvent,
|
||||
#[derivative(Debug = "ignore")]
|
||||
pub(crate) module_data: Arc<ModuleDataMap>,
|
||||
pub(crate) states: Arc<AFPluginStateMap>,
|
||||
}
|
||||
|
||||
impl EventRequest {
|
||||
pub fn new<E>(id: String, event: E, module_data: Arc<ModuleDataMap>) -> EventRequest
|
||||
impl AFPluginEventRequest {
|
||||
pub fn new<E>(id: String, event: E, module_data: Arc<AFPluginStateMap>) -> AFPluginEventRequest
|
||||
where
|
||||
E: Into<Event>,
|
||||
E: Into<AFPluginEvent>,
|
||||
{
|
||||
Self {
|
||||
id,
|
||||
event: event.into(),
|
||||
module_data,
|
||||
states: module_data,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn module_data<T: 'static>(&self) -> Option<&T>
|
||||
pub fn get_state<T: 'static>(&self) -> Option<&T>
|
||||
where
|
||||
T: Send + Sync,
|
||||
{
|
||||
if let Some(data) = self.module_data.get::<T>() {
|
||||
if let Some(data) = self.states.get::<T>() {
|
||||
return Some(data);
|
||||
}
|
||||
|
||||
@ -48,29 +48,29 @@ impl EventRequest {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FromRequest: Sized {
|
||||
pub trait FromAFPluginRequest: Sized {
|
||||
type Error: Into<DispatchError>;
|
||||
type Future: Future<Output = Result<Self, Self::Error>>;
|
||||
|
||||
fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future;
|
||||
fn from_request(req: &AFPluginEventRequest, payload: &mut Payload) -> Self::Future;
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
impl FromRequest for () {
|
||||
impl FromAFPluginRequest for () {
|
||||
type Error = DispatchError;
|
||||
type Future = Ready<Result<(), DispatchError>>;
|
||||
|
||||
fn from_request(_req: &EventRequest, _payload: &mut Payload) -> Self::Future {
|
||||
fn from_request(_req: &AFPluginEventRequest, _payload: &mut Payload) -> Self::Future {
|
||||
ready(Ok(()))
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
impl FromRequest for String {
|
||||
impl FromAFPluginRequest for String {
|
||||
type Error = DispatchError;
|
||||
type Future = Ready<Result<Self, Self::Error>>;
|
||||
|
||||
fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
|
||||
fn from_request(req: &AFPluginEventRequest, payload: &mut Payload) -> Self::Future {
|
||||
match &payload {
|
||||
Payload::None => ready(Err(unexpected_none_payload(req))),
|
||||
Payload::Bytes(buf) => ready(Ok(String::from_utf8_lossy(buf).into_owned())),
|
||||
@ -78,20 +78,20 @@ impl FromRequest for String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unexpected_none_payload(request: &EventRequest) -> DispatchError {
|
||||
pub fn unexpected_none_payload(request: &AFPluginEventRequest) -> DispatchError {
|
||||
log::warn!("{:?} expected payload", &request.event);
|
||||
InternalError::UnexpectedNone("Expected payload".to_string()).into()
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
impl<T> FromRequest for Result<T, T::Error>
|
||||
impl<T> FromAFPluginRequest for Result<T, T::Error>
|
||||
where
|
||||
T: FromRequest,
|
||||
T: FromAFPluginRequest,
|
||||
{
|
||||
type Error = DispatchError;
|
||||
type Future = FromRequestFuture<T::Future>;
|
||||
|
||||
fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
|
||||
fn from_request(req: &AFPluginEventRequest, payload: &mut Payload) -> Self::Future {
|
||||
FromRequestFuture {
|
||||
fut: T::from_request(req, payload),
|
||||
}
|
||||
|
@ -1,19 +1,19 @@
|
||||
#[allow(unused_imports)]
|
||||
use crate::errors::{DispatchError, InternalError};
|
||||
use crate::{
|
||||
request::EventRequest,
|
||||
request::AFPluginEventRequest,
|
||||
response::{EventResponse, ResponseBuilder},
|
||||
};
|
||||
use bytes::Bytes;
|
||||
|
||||
pub trait Responder {
|
||||
fn respond_to(self, req: &EventRequest) -> EventResponse;
|
||||
pub trait AFPluginResponder {
|
||||
fn respond_to(self, req: &AFPluginEventRequest) -> EventResponse;
|
||||
}
|
||||
|
||||
macro_rules! impl_responder {
|
||||
($res: ty) => {
|
||||
impl Responder for $res {
|
||||
fn respond_to(self, _: &EventRequest) -> EventResponse {
|
||||
impl AFPluginResponder for $res {
|
||||
fn respond_to(self, _: &AFPluginEventRequest) -> EventResponse {
|
||||
ResponseBuilder::Ok().data(self).build()
|
||||
}
|
||||
}
|
||||
@ -27,12 +27,12 @@ impl_responder!(Bytes);
|
||||
impl_responder!(());
|
||||
impl_responder!(Vec<u8>);
|
||||
|
||||
impl<T, E> Responder for Result<T, E>
|
||||
impl<T, E> AFPluginResponder for Result<T, E>
|
||||
where
|
||||
T: Responder,
|
||||
T: AFPluginResponder,
|
||||
E: Into<DispatchError>,
|
||||
{
|
||||
fn respond_to(self, request: &EventRequest) -> EventResponse {
|
||||
fn respond_to(self, request: &AFPluginEventRequest) -> EventResponse {
|
||||
match self {
|
||||
Ok(val) => val.respond_to(request),
|
||||
Err(e) => e.into().into(),
|
||||
|
@ -1,9 +1,9 @@
|
||||
use crate::{
|
||||
byte_trait::FromBytes,
|
||||
data::Data,
|
||||
byte_trait::AFPluginFromBytes,
|
||||
data::AFPluginData,
|
||||
errors::DispatchError,
|
||||
request::{EventRequest, Payload},
|
||||
response::Responder,
|
||||
request::{AFPluginEventRequest, Payload},
|
||||
response::AFPluginResponder,
|
||||
};
|
||||
use derivative::*;
|
||||
use std::{convert::TryFrom, fmt, fmt::Formatter};
|
||||
@ -35,16 +35,16 @@ impl EventResponse {
|
||||
|
||||
pub fn parse<T, E>(self) -> Result<Result<T, E>, DispatchError>
|
||||
where
|
||||
T: FromBytes,
|
||||
E: FromBytes,
|
||||
T: AFPluginFromBytes,
|
||||
E: AFPluginFromBytes,
|
||||
{
|
||||
match self.status_code {
|
||||
StatusCode::Ok => {
|
||||
let data = <Data<T>>::try_from(self.payload)?;
|
||||
let data = <AFPluginData<T>>::try_from(self.payload)?;
|
||||
Ok(Ok(data.into_inner()))
|
||||
}
|
||||
StatusCode::Err | StatusCode::Internal => {
|
||||
let err = <Data<E>>::try_from(self.payload)?;
|
||||
let err = <AFPluginData<E>>::try_from(self.payload)?;
|
||||
Ok(Err(err.into_inner()))
|
||||
}
|
||||
}
|
||||
@ -64,18 +64,18 @@ impl std::fmt::Display for EventResponse {
|
||||
}
|
||||
}
|
||||
|
||||
impl Responder for EventResponse {
|
||||
impl AFPluginResponder for EventResponse {
|
||||
#[inline]
|
||||
fn respond_to(self, _: &EventRequest) -> EventResponse {
|
||||
fn respond_to(self, _: &AFPluginEventRequest) -> EventResponse {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub type DataResult<T, E> = std::result::Result<Data<T>, E>;
|
||||
pub type DataResult<T, E> = std::result::Result<AFPluginData<T>, E>;
|
||||
|
||||
pub fn data_result<T, E>(data: T) -> Result<Data<T>, E>
|
||||
pub fn data_result<T, E>(data: T) -> Result<AFPluginData<T>, E>
|
||||
where
|
||||
E: Into<DispatchError>,
|
||||
{
|
||||
Ok(Data(data))
|
||||
Ok(AFPluginData(data))
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
use std::{io, thread};
|
||||
use tokio::runtime;
|
||||
|
||||
pub type FlowyRuntime = tokio::runtime::Runtime;
|
||||
pub type AFPluginRuntime = tokio::runtime::Runtime;
|
||||
|
||||
pub fn tokio_default_runtime() -> io::Result<FlowyRuntime> {
|
||||
pub fn tokio_default_runtime() -> io::Result<AFPluginRuntime> {
|
||||
runtime::Builder::new_multi_thread()
|
||||
.thread_name("dispatch-rt")
|
||||
.enable_io()
|
||||
|
@ -1,23 +1,23 @@
|
||||
use crate::service::{Service, ServiceFactory};
|
||||
use crate::service::{AFPluginServiceFactory, Service};
|
||||
use futures_core::future::BoxFuture;
|
||||
|
||||
pub fn factory<SF, Req>(factory: SF) -> BoxServiceFactory<SF::Context, Req, SF::Response, SF::Error>
|
||||
where
|
||||
SF: ServiceFactory<Req> + 'static + Sync + Send,
|
||||
SF: AFPluginServiceFactory<Req> + 'static + Sync + Send,
|
||||
Req: 'static,
|
||||
SF::Response: 'static,
|
||||
SF::Service: 'static,
|
||||
SF::Future: 'static,
|
||||
SF::Error: 'static + Send + Sync,
|
||||
<SF as ServiceFactory<Req>>::Service: Sync + Send,
|
||||
<<SF as ServiceFactory<Req>>::Service as Service<Req>>::Future: Send + Sync,
|
||||
<SF as ServiceFactory<Req>>::Future: Send + Sync,
|
||||
<SF as AFPluginServiceFactory<Req>>::Service: Sync + Send,
|
||||
<<SF as AFPluginServiceFactory<Req>>::Service as Service<Req>>::Future: Send + Sync,
|
||||
<SF as AFPluginServiceFactory<Req>>::Future: Send + Sync,
|
||||
{
|
||||
BoxServiceFactory(Box::new(FactoryWrapper(factory)))
|
||||
}
|
||||
|
||||
type Inner<Cfg, Req, Res, Err> = Box<
|
||||
dyn ServiceFactory<
|
||||
dyn AFPluginServiceFactory<
|
||||
Req,
|
||||
Context = Cfg,
|
||||
Response = Res,
|
||||
@ -29,7 +29,7 @@ type Inner<Cfg, Req, Res, Err> = Box<
|
||||
>;
|
||||
|
||||
pub struct BoxServiceFactory<Cfg, Req, Res, Err>(Inner<Cfg, Req, Res, Err>);
|
||||
impl<Cfg, Req, Res, Err> ServiceFactory<Req> for BoxServiceFactory<Cfg, Req, Res, Err>
|
||||
impl<Cfg, Req, Res, Err> AFPluginServiceFactory<Req> for BoxServiceFactory<Cfg, Req, Res, Err>
|
||||
where
|
||||
Req: 'static,
|
||||
Res: 'static,
|
||||
@ -98,16 +98,16 @@ where
|
||||
|
||||
struct FactoryWrapper<SF>(SF);
|
||||
|
||||
impl<SF, Req, Cfg, Res, Err> ServiceFactory<Req> for FactoryWrapper<SF>
|
||||
impl<SF, Req, Cfg, Res, Err> AFPluginServiceFactory<Req> for FactoryWrapper<SF>
|
||||
where
|
||||
Req: 'static,
|
||||
Res: 'static,
|
||||
Err: 'static,
|
||||
SF: ServiceFactory<Req, Context = Cfg, Response = Res, Error = Err>,
|
||||
SF: AFPluginServiceFactory<Req, Context = Cfg, Response = Res, Error = Err>,
|
||||
SF::Future: 'static,
|
||||
SF::Service: 'static + Send + Sync,
|
||||
<<SF as ServiceFactory<Req>>::Service as Service<Req>>::Future: Send + Sync + 'static,
|
||||
<SF as ServiceFactory<Req>>::Future: Send + Sync,
|
||||
<<SF as AFPluginServiceFactory<Req>>::Service as Service<Req>>::Future: Send + Sync + 'static,
|
||||
<SF as AFPluginServiceFactory<Req>>::Future: Send + Sync,
|
||||
{
|
||||
type Response = Res;
|
||||
type Error = Err;
|
||||
|
@ -10,37 +10,37 @@ use pin_project::pin_project;
|
||||
|
||||
use crate::{
|
||||
errors::DispatchError,
|
||||
request::{payload::Payload, EventRequest, FromRequest},
|
||||
response::{EventResponse, Responder},
|
||||
service::{Service, ServiceFactory, ServiceRequest, ServiceResponse},
|
||||
request::{payload::Payload, AFPluginEventRequest, FromAFPluginRequest},
|
||||
response::{AFPluginResponder, EventResponse},
|
||||
service::{AFPluginServiceFactory, Service, ServiceRequest, ServiceResponse},
|
||||
util::ready::*,
|
||||
};
|
||||
|
||||
pub trait Handler<T, R>: Clone + 'static + Sync + Send
|
||||
pub trait AFPluginHandler<T, R>: Clone + 'static + Sync + Send
|
||||
where
|
||||
R: Future + Send + Sync,
|
||||
R::Output: Responder,
|
||||
R::Output: AFPluginResponder,
|
||||
{
|
||||
fn call(&self, param: T) -> R;
|
||||
}
|
||||
|
||||
pub struct HandlerService<H, T, R>
|
||||
pub struct AFPluginHandlerService<H, T, R>
|
||||
where
|
||||
H: Handler<T, R>,
|
||||
T: FromRequest,
|
||||
H: AFPluginHandler<T, R>,
|
||||
T: FromAFPluginRequest,
|
||||
R: Future + Sync + Send,
|
||||
R::Output: Responder,
|
||||
R::Output: AFPluginResponder,
|
||||
{
|
||||
handler: H,
|
||||
_phantom: PhantomData<(T, R)>,
|
||||
}
|
||||
|
||||
impl<H, T, R> HandlerService<H, T, R>
|
||||
impl<H, T, R> AFPluginHandlerService<H, T, R>
|
||||
where
|
||||
H: Handler<T, R>,
|
||||
T: FromRequest,
|
||||
H: AFPluginHandler<T, R>,
|
||||
T: FromAFPluginRequest,
|
||||
R: Future + Sync + Send,
|
||||
R::Output: Responder,
|
||||
R::Output: AFPluginResponder,
|
||||
{
|
||||
pub fn new(handler: H) -> Self {
|
||||
Self {
|
||||
@ -50,12 +50,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<H, T, R> Clone for HandlerService<H, T, R>
|
||||
impl<H, T, R> Clone for AFPluginHandlerService<H, T, R>
|
||||
where
|
||||
H: Handler<T, R>,
|
||||
T: FromRequest,
|
||||
H: AFPluginHandler<T, R>,
|
||||
T: FromAFPluginRequest,
|
||||
R: Future + Sync + Send,
|
||||
R::Output: Responder,
|
||||
R::Output: AFPluginResponder,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
@ -65,12 +65,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T, R> ServiceFactory<ServiceRequest> for HandlerService<F, T, R>
|
||||
impl<F, T, R> AFPluginServiceFactory<ServiceRequest> for AFPluginHandlerService<F, T, R>
|
||||
where
|
||||
F: Handler<T, R>,
|
||||
T: FromRequest,
|
||||
F: AFPluginHandler<T, R>,
|
||||
T: FromAFPluginRequest,
|
||||
R: Future + Send + Sync,
|
||||
R::Output: Responder,
|
||||
R::Output: AFPluginResponder,
|
||||
{
|
||||
type Response = ServiceResponse;
|
||||
type Error = DispatchError;
|
||||
@ -83,12 +83,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<H, T, R> Service<ServiceRequest> for HandlerService<H, T, R>
|
||||
impl<H, T, R> Service<ServiceRequest> for AFPluginHandlerService<H, T, R>
|
||||
where
|
||||
H: Handler<T, R>,
|
||||
T: FromRequest,
|
||||
H: AFPluginHandler<T, R>,
|
||||
T: FromAFPluginRequest,
|
||||
R: Future + Sync + Send,
|
||||
R::Output: Responder,
|
||||
R::Output: AFPluginResponder,
|
||||
{
|
||||
type Response = ServiceResponse;
|
||||
type Error = DispatchError;
|
||||
@ -104,21 +104,21 @@ where
|
||||
#[pin_project(project = HandlerServiceProj)]
|
||||
pub enum HandlerServiceFuture<H, T, R>
|
||||
where
|
||||
H: Handler<T, R>,
|
||||
T: FromRequest,
|
||||
H: AFPluginHandler<T, R>,
|
||||
T: FromAFPluginRequest,
|
||||
R: Future + Sync + Send,
|
||||
R::Output: Responder,
|
||||
R::Output: AFPluginResponder,
|
||||
{
|
||||
Extract(#[pin] T::Future, Option<EventRequest>, H),
|
||||
Handle(#[pin] R, Option<EventRequest>),
|
||||
Extract(#[pin] T::Future, Option<AFPluginEventRequest>, H),
|
||||
Handle(#[pin] R, Option<AFPluginEventRequest>),
|
||||
}
|
||||
|
||||
impl<F, T, R> Future for HandlerServiceFuture<F, T, R>
|
||||
where
|
||||
F: Handler<T, R>,
|
||||
T: FromRequest,
|
||||
F: AFPluginHandler<T, R>,
|
||||
T: FromAFPluginRequest,
|
||||
R: Future + Sync + Send,
|
||||
R::Output: Responder,
|
||||
R::Output: AFPluginResponder,
|
||||
{
|
||||
type Output = Result<ServiceResponse, DispatchError>;
|
||||
|
||||
@ -152,10 +152,10 @@ where
|
||||
}
|
||||
|
||||
macro_rules! factory_tuple ({ $($param:ident)* } => {
|
||||
impl<Func, $($param,)* Res> Handler<($($param,)*), Res> for Func
|
||||
impl<Func, $($param,)* Res> AFPluginHandler<($($param,)*), Res> for Func
|
||||
where Func: Fn($($param),*) -> Res + Clone + 'static + Sync + Send,
|
||||
Res: Future + Sync + Send,
|
||||
Res::Output: Responder,
|
||||
Res::Output: AFPluginResponder,
|
||||
{
|
||||
#[allow(non_snake_case)]
|
||||
fn call(&self, ($($param,)*): ($($param,)*)) -> Res {
|
||||
@ -170,17 +170,17 @@ macro_rules! tuple_from_req ({$tuple_type:ident, $(($n:tt, $T:ident)),+} => {
|
||||
use super::*;
|
||||
|
||||
#[pin_project::pin_project]
|
||||
struct FromRequestFutures<$($T: FromRequest),+>($(#[pin] $T::Future),+);
|
||||
struct FromRequestFutures<$($T: FromAFPluginRequest),+>($(#[pin] $T::Future),+);
|
||||
|
||||
/// FromRequest implementation for tuple
|
||||
#[doc(hidden)]
|
||||
#[allow(unused_parens)]
|
||||
impl<$($T: FromRequest + 'static),+> FromRequest for ($($T,)+)
|
||||
impl<$($T: FromAFPluginRequest + 'static),+> FromAFPluginRequest for ($($T,)+)
|
||||
{
|
||||
type Error = DispatchError;
|
||||
type Future = $tuple_type<$($T),+>;
|
||||
|
||||
fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
|
||||
fn from_request(req: &AFPluginEventRequest, payload: &mut Payload) -> Self::Future {
|
||||
$tuple_type {
|
||||
items: <($(Option<$T>,)+)>::default(),
|
||||
futs: FromRequestFutures($($T::from_request(req, payload),)+),
|
||||
@ -190,13 +190,13 @@ macro_rules! tuple_from_req ({$tuple_type:ident, $(($n:tt, $T:ident)),+} => {
|
||||
|
||||
#[doc(hidden)]
|
||||
#[pin_project::pin_project]
|
||||
pub struct $tuple_type<$($T: FromRequest),+> {
|
||||
pub struct $tuple_type<$($T: FromAFPluginRequest),+> {
|
||||
items: ($(Option<$T>,)+),
|
||||
#[pin]
|
||||
futs: FromRequestFutures<$($T,)+>,
|
||||
}
|
||||
|
||||
impl<$($T: FromRequest),+> Future for $tuple_type<$($T),+>
|
||||
impl<$($T: FromAFPluginRequest),+> Future for $tuple_type<$($T),+>
|
||||
{
|
||||
type Output = Result<($($T,)+), DispatchError>;
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::future::Future;
|
||||
|
||||
use crate::{
|
||||
request::{payload::Payload, EventRequest},
|
||||
request::{payload::Payload, AFPluginEventRequest},
|
||||
response::EventResponse,
|
||||
};
|
||||
|
||||
@ -13,7 +13,7 @@ pub trait Service<Request> {
|
||||
fn call(&self, req: Request) -> Self::Future;
|
||||
}
|
||||
|
||||
pub trait ServiceFactory<Request> {
|
||||
pub trait AFPluginServiceFactory<Request> {
|
||||
type Response;
|
||||
type Error;
|
||||
type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
|
||||
@ -23,33 +23,33 @@ pub trait ServiceFactory<Request> {
|
||||
fn new_service(&self, cfg: Self::Context) -> Self::Future;
|
||||
}
|
||||
|
||||
pub struct ServiceRequest {
|
||||
req: EventRequest,
|
||||
pub(crate) struct ServiceRequest {
|
||||
req: AFPluginEventRequest,
|
||||
payload: Payload,
|
||||
}
|
||||
|
||||
impl ServiceRequest {
|
||||
pub fn new(req: EventRequest, payload: Payload) -> Self {
|
||||
pub(crate) fn new(req: AFPluginEventRequest, payload: Payload) -> Self {
|
||||
Self { req, payload }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn into_parts(self) -> (EventRequest, Payload) {
|
||||
pub(crate) fn into_parts(self) -> (AFPluginEventRequest, Payload) {
|
||||
(self.req, self.payload)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ServiceResponse {
|
||||
request: EventRequest,
|
||||
request: AFPluginEventRequest,
|
||||
response: EventResponse,
|
||||
}
|
||||
|
||||
impl ServiceResponse {
|
||||
pub fn new(request: EventRequest, response: EventResponse) -> Self {
|
||||
pub fn new(request: AFPluginEventRequest, response: EventResponse) -> Self {
|
||||
ServiceResponse { request, response }
|
||||
}
|
||||
|
||||
pub fn into_parts(self) -> (EventRequest, EventResponse) {
|
||||
pub fn into_parts(self) -> (AFPluginEventRequest, EventResponse) {
|
||||
(self.request, self.response)
|
||||
}
|
||||
}
|
||||
|
@ -12,11 +12,11 @@ async fn test() {
|
||||
|
||||
let event = "1";
|
||||
let runtime = tokio_default_runtime().unwrap();
|
||||
let dispatch = Arc::new(EventDispatcher::construct(runtime, || {
|
||||
vec![Module::new().event(event, hello)]
|
||||
let dispatch = Arc::new(AFPluginDispatcher::construct(runtime, || {
|
||||
vec![AFPlugin::new().event(event, hello)]
|
||||
}));
|
||||
let request = ModuleRequest::new(event);
|
||||
let _ = EventDispatcher::async_send_with_callback(dispatch.clone(), request, |resp| {
|
||||
let request = AFPluginRequest::new(event);
|
||||
let _ = AFPluginDispatcher::async_send_with_callback(dispatch.clone(), request, |resp| {
|
||||
Box::pin(async move {
|
||||
dbg!(&resp);
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user