rename flowy-sys to flowy-dispatch

This commit is contained in:
appflowy 2021-07-08 21:23:44 +08:00
parent 717d53412b
commit 53a041b6cd
50 changed files with 105 additions and 267 deletions

View File

@ -5,8 +5,8 @@
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-ast/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-derive/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-derive/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-sys/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-sys/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-dispatch/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-dispatch/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/dart-ffi/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-log/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-sdk/src" isTestSource="false" />

View File

@ -1,46 +1,48 @@
/// Auto gen code from rust ast, do not edit
part of 'dispatch.dart';
class UserEventAuthCheck {
UserSignInParams params;
UserEventAuthCheck(this.params);
UserSignInParams params;
UserEventAuthCheck(this.params);
Future<Either<UserSignInResult, FlowyError>> send() {
Future<Either<UserSignInResult, FlowyError>> send() {
return paramsToBytes(params).fold(
(bytes) {
final request = FFIRequest.create()
..event = UserEvent.AuthCheck.toString()
..payload = bytes;
(bytes) {
final request = FFIRequest.create()
..event = UserEvent.AuthCheck.toString()
..payload = bytes;
return Dispatch.asyncRequest(request)
.then((bytesResult) => bytesResult.fold(
(bytes) => left(UserSignInResult.fromBuffer(bytes)),
(error) => right(error),
));
},
(err) => Future(() => right(err)),
);
}
return Dispatch.asyncRequest(request)
.then((bytesResult) => bytesResult.fold(
(bytes) => left(UserSignInResult.fromBuffer(bytes)),
(error) => right(error),
));
},
(err) => Future(() => right(err)),
);
}
}
class UserEventSignIn {
UserSignInParams params;
UserEventSignIn(this.params);
UserSignInParams params;
UserEventSignIn(this.params);
Future<Either<UserSignInResult, FlowyError>> send() {
Future<Either<UserSignInResult, FlowyError>> send() {
return paramsToBytes(params).fold(
(bytes) {
final request = FFIRequest.create()
..event = UserEvent.SignIn.toString()
..payload = bytes;
(bytes) {
final request = FFIRequest.create()
..event = UserEvent.SignIn.toString()
..payload = bytes;
return Dispatch.asyncRequest(request)
.then((bytesResult) => bytesResult.fold(
(bytes) => left(UserSignInResult.fromBuffer(bytes)),
(error) => right(error),
));
},
(err) => Future(() => right(err)),
);
}
return Dispatch.asyncRequest(request)
.then((bytesResult) => bytesResult.fold(
(bytes) => left(UserSignInResult.fromBuffer(bytes)),
(error) => right(error),
));
},
(err) => Future(() => right(err)),
);
}
}

View File

@ -1,7 +1,5 @@
import 'dart:ffi';
import 'package:dartz/dartz.dart';
import 'package:flowy_logger/flowy_logger.dart';
import 'package:flowy_sdk/dispatch/flowy_error.dart';
import 'package:flowy_sdk/protobuf/ffi_response.pb.dart';
import 'package:isolates/isolates.dart';
import 'package:isolates/ports.dart';
@ -14,6 +12,8 @@ import 'package:flowy_sdk/ffi/ffi.dart' as ffi;
import 'package:flowy_sdk/protobuf.dart';
import 'package:protobuf/protobuf.dart';
import 'error.dart';
part 'code_gen.dart';
enum FFIException {
@ -98,29 +98,3 @@ Either<Uint8List, FlowyError> paramsToBytes<T extends GeneratedMessage>(
return right(FlowyError.fromError('${e.runtimeType}. Stack trace: $s'));
}
}
class StackTraceError {
Object error;
StackTrace trace;
StackTraceError(
this.error,
this.trace,
);
FlowyError toFlowyError() {
Log.error('${error.runtimeType}\n');
Log.error('Stack trace \n $trace');
return FlowyError.fromError('${error.runtimeType}. Stack trace: $trace');
}
String toString() {
return '${error.runtimeType}. Stack trace: $trace';
}
}
FFIResponse error_response(FFIRequest request, StackTraceError error) {
var response = FFIResponse();
response.code = FFIStatusCode.Err;
response.error = error.toString();
return response;
}

View File

@ -1,35 +0,0 @@
import '../protobuf/ffi_response.pb.dart';
class FlowyError {
late FFIStatusCode _statusCode;
late String _error;
FFIStatusCode get statusCode {
return _statusCode;
}
String get error {
return _error;
}
bool get has_error {
return _statusCode != FFIStatusCode.Ok;
}
String toString() {
return "$_statusCode: $_error";
}
FlowyError({required FFIStatusCode statusCode, required String error}) {
_statusCode = statusCode;
_error = error;
}
factory FlowyError.from(FFIResponse resp) {
return FlowyError(statusCode: resp.code, error: resp.error);
}
factory FlowyError.fromError(String error) {
return FlowyError(statusCode: FFIStatusCode.Err, error: error);
}
}

View File

@ -3,9 +3,9 @@ export 'package:async/async.dart';
import 'dart:io';
import 'dart:async';
import 'package:dartz/dartz.dart';
import 'package:flowy_sdk/dispatch/flowy_error.dart';
import 'package:flutter/services.dart';
import 'dart:ffi';
import 'dispatch/error.dart';
import 'ffi/ffi.dart' as ffi;
import 'package:ffi/ffi.dart';

View File

@ -7,8 +7,12 @@
## 🎯 Goals of the System
## 🤔 Some Design Considerations
## 📜 High Level Design
## 📚 Component Design
### 📕 Component 1

View File

@ -1,6 +1,6 @@
[workspace]
members = [
"flowy-sys",
"flowy-dispatch",
"flowy-sdk",
"dart-ffi",
"flowy-log",

View File

@ -25,7 +25,7 @@ log = "0.4.14"
serde = { version = "1.0", features = ["derive"] }
serde_json = {version = "1.0"}
flowy-sys = {path = "../flowy-sys"}
flowy-dispatch = {path = "../flowy-dispatch"}
flowy-sdk = {path = "../flowy-sdk"}
flowy-derive = {path = "../flowy-derive"}

View File

@ -7,8 +7,8 @@ use crate::{
c::{extend_front_four_bytes_into_bytes, forget_rust},
model::{FFIRequest, FFIResponse},
};
use flowy_dispatch::prelude::*;
use flowy_sdk::*;
use flowy_sys::prelude::*;
use lazy_static::lazy_static;
use std::{ffi::CStr, os::raw::c_char};
@ -65,7 +65,7 @@ pub extern "C" fn sync_command(input: *const u8, len: usize) -> *const u8 {
#[no_mangle]
pub extern "C" fn link_me_please() {}
use flowy_sys::prelude::ToBytes;
use flowy_dispatch::prelude::ToBytes;
#[inline(always)]
async fn post_to_flutter(response: EventResponse, port: i64) {
let isolate = allo_isolate::Isolate::new(port);

View File

@ -1,5 +1,5 @@
use flowy_derive::ProtoBuf;
use flowy_sys::prelude::ModuleRequest;
use flowy_dispatch::prelude::ModuleRequest;
use std::convert::TryFrom;
#[derive(Default, ProtoBuf)]

View File

@ -1,5 +1,5 @@
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_sys::prelude::{EventResponse, Payload, StatusCode};
use flowy_dispatch::prelude::{EventResponse, Payload, StatusCode};
#[derive(ProtoBuf_Enum, Clone, Copy)]
pub enum FFIStatusCode {

View File

@ -1,5 +1,5 @@
[package]
name = "flowy-sys"
name = "flowy-dispatch"
version = "0.1.0"
edition = "2018"

View File

@ -28,7 +28,7 @@ impl EventDispatch {
F: FnOnce() -> Vec<Module>,
{
let modules = module_factory();
log::debug!("{}", module_info(&modules));
log::trace!("{}", module_info(&modules));
let module_map = as_module_map(modules);
let runtime = tokio_default_runtime().unwrap();
let dispatch = EventDispatch {
@ -50,11 +50,7 @@ impl EventDispatch {
let dispatch = dispatch.as_ref().unwrap();
let module_map = dispatch.module_map.clone();
let service = Box::new(DispatchService { module_map });
log::trace!(
"{}: dispatch {:?} to runtime",
&request.id(),
&request.event()
);
log::trace!("Async event: {:?}", &request.event());
let service_ctx = DispatchContext {
request,
callback: Some(Box::new(callback)),
@ -77,8 +73,8 @@ impl EventDispatch {
},
Err(e) => {
let msg = format!("Dispatch runtime error: {:?}", e);
log::trace!("{}", msg);
let msg = format!("EVENT_DISPATCH read failed. {:?}", e);
log::error!("{}", msg);
DispatchFuture {
fut: Box::pin(async { InternalError::new(msg).as_response() }),
}
@ -88,11 +84,7 @@ impl EventDispatch {
pub fn sync_send(request: ModuleRequest) -> EventResponse {
futures::executor::block_on(async {
EventDispatch::async_send(request, |response| {
dbg!(&response);
Box::pin(async {})
})
.await
EventDispatch::async_send(request, |_| Box::pin(async {})).await
})
}
}
@ -154,20 +146,11 @@ impl Service<DispatchContext> for DispatchService {
match module_map.get(&request.event()) {
Some(module) => {
let fut = module.new_service(());
log::trace!(
"{}: handle event: {:?} by {}",
request.id(),
request.event(),
module.name
);
let service_fut = fut.await?.call(request);
service_fut.await
},
None => {
let msg = format!(
"Can not find the module to handle the request:{:?}",
request
);
let msg = format!("Can not find the event handler. {:?}", request);
log::trace!("{}", msg);
Err(InternalError::new(msg).into())
},

View File

@ -4,7 +4,6 @@ mod error;
mod module;
mod request;
mod response;
mod rt;
mod service;
mod util;
@ -13,5 +12,5 @@ mod dispatch;
mod system;
pub mod prelude {
pub use crate::{data::*, dispatch::*, error::*, module::*, request::*, response::*, rt::*};
pub use crate::{data::*, dispatch::*, error::*, module::*, request::*, response::*};
}

View File

@ -1,7 +1,4 @@
use crate::{
module::{as_module_map, Module, ModuleMap},
rt::Runtime,
};
use crate::module::{as_module_map, Module, ModuleMap};
use futures_core::{ready, task::Context};
use std::{cell::RefCell, fmt::Debug, future::Future, io, sync::Arc};
use tokio::{
@ -127,3 +124,37 @@ impl SystemRunner {
self
}
}
use crate::util::tokio_default_runtime;
use tokio::{runtime, task::LocalSet};
#[derive(Debug)]
pub struct Runtime {
local: LocalSet,
rt: runtime::Runtime,
}
impl Runtime {
pub fn new() -> io::Result<Runtime> {
let rt = tokio_default_runtime()?;
Ok(Runtime {
rt,
local: LocalSet::new(),
})
}
pub fn spawn<F>(&self, future: F) -> &Self
where
F: Future<Output = ()> + 'static,
{
self.local.spawn_local(future);
self
}
pub fn block_on<F>(&self, f: F) -> F::Output
where
F: Future + 'static,
{
self.local.block_on(&self.rt, f)
}
}

View File

@ -1,11 +1,11 @@
use flowy_sys::prelude::*;
use flowy_dispatch::prelude::*;
use std::sync::Once;
#[allow(dead_code)]
pub fn setup_env() {
static INIT: Once = Once::new();
INIT.call_once(|| {
std::env::set_var("RUST_LOG", "flowy_sys=debug,debug");
std::env::set_var("RUST_LOG", "flowy_dispatch=debug,debug");
env_logger::init();
});
}

View File

@ -1,5 +1,5 @@
use crate::helper::*;
use flowy_sys::prelude::*;
use flowy_dispatch::prelude::*;
pub async fn hello() -> String { "say hello".to_string() }

View File

@ -6,7 +6,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
flowy-sys = { path = "../flowy-sys", features = ["use_tracing"]}
flowy-dispatch = { path = "../flowy-dispatch", features = ["use_tracing"]}
flowy-log = { path = "../flowy-log", features = ["use_bunyan"] }
flowy-user = { path = "../flowy-user" }
tracing = { version = "0.1" }

View File

@ -1,7 +1,7 @@
pub mod module;
pub use module::*;
use flowy_sys::prelude::*;
use flowy_dispatch::prelude::*;
use module::build_modules;
pub struct FlowySDK {}

View File

@ -1,3 +1,3 @@
use flowy_sys::prelude::Module;
use flowy_dispatch::prelude::Module;
pub fn build_modules() -> Vec<Module> { vec![flowy_user::module::create()] }

View File

@ -1,4 +0,0 @@
pub use runtime::*;
pub use crate::system::*;
pub mod runtime;

View File

@ -1,34 +0,0 @@
use crate::util::tokio_default_runtime;
use std::{future::Future, io};
use tokio::{runtime, task::LocalSet};
#[derive(Debug)]
pub struct Runtime {
local: LocalSet,
rt: runtime::Runtime,
}
impl Runtime {
pub fn new() -> io::Result<Runtime> {
let rt = tokio_default_runtime()?;
Ok(Runtime {
rt,
local: LocalSet::new(),
})
}
pub fn spawn<F>(&self, future: F) -> &Self
where
F: Future<Output = ()> + 'static,
{
self.local.spawn_local(future);
self
}
pub fn block_on<F>(&self, f: F) -> F::Output
where
F: Future + 'static,
{
self.local.block_on(&self.rt, f)
}
}

View File

@ -7,7 +7,7 @@ edition = "2018"
[dependencies]
flowy-sdk = { path = "../flowy-sdk"}
flowy-sys = { path = "../flowy-sys"}
flowy-dispatch = { path = "../flowy-dispatch"}
serde = { version = "1.0", features = ["derive"] }
bincode = { version = "1.3"}

View File

@ -1,5 +1,5 @@
use flowy_dispatch::prelude::*;
pub use flowy_sdk::*;
use flowy_sys::prelude::*;
use std::{
convert::TryFrom,
fmt::{Debug, Display},
@ -11,7 +11,7 @@ use std::{
pub mod prelude {
pub use crate::EventTester;
pub use flowy_sys::prelude::*;
pub use flowy_dispatch::prelude::*;
pub use std::convert::TryFrom;
}

View File

@ -7,7 +7,7 @@ edition = "2018"
[dependencies]
derive_more = {version = "0.99", features = ["display"]}
flowy-sys = { path = "../flowy-sys" }
flowy-dispatch = { path = "../flowy-dispatch" }
flowy-log = { path = "../flowy-log" }
flowy-derive = { path = "../flowy-derive" }
tracing = { version = "0.1", features = ["log"] }

View File

@ -1,5 +1,5 @@
use crate::domain::user::*;
use flowy_sys::prelude::*;
use flowy_dispatch::prelude::*;
use std::convert::TryInto;
// tracing instrument 👉🏻 https://docs.rs/tracing/0.1.26/tracing/attr.instrument.html

View File

@ -1,5 +1,5 @@
use crate::{domain::event::UserEvent, handlers::*};
use flowy_sys::prelude::*;
use flowy_dispatch::prelude::*;
pub fn create() -> Module {
Module::new()

View File

@ -16,7 +16,6 @@ condition = { env_true = ["RELEASE"] }
env = { DESKTOP_TARGET = "x86_64-apple-darwin" }
private = true
run_task = "desktop-build"
[tasks.desktop-build]
category = "Build"
condition = { platforms = ["mac"], env_true = ["DEV"] }

View File

@ -1,81 +0,0 @@
[tasks.rust_env_setup]
dependencies = ["rustup", "add_targets", "rust_tools"]
description = "Setup the rust env"
[tasks.rustup]
script = [
"""
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly -y
echo 'export PATH="$$HOME/.cargo/bin:$$PATH"' >> ~/.bash_profile
source ~/.bash_profile
""",
]
script_runner = "@shell"
[tasks.add_compile_targets]
script = [
"""
rustup target add aarch64-linux-android
rustup target add arm-linux-androideabi
rustup target add armv7-linux-androideabi
rustup target add i686-linux-android
rustup target add aarch64-apple-ios
rustup target add x86_64-apple-ios
rustup target add x86_64-apple-darwin
""",
]
script_runner = "@shell"
[tasks.rust_tools]
script = [
"""
# cargo install clog-cli --force
# cargo install cargo-cache
rustup component add rustfmt
cargo install ripgrep
cargo install rusty-hook
cargo install cargo-expand
""",
]
script_runner = "@shell"
[tasks.fmt]
args = ["fmt", "--", "--emit=files"]
command = "cargo"
install_crate = "rustfmt"
[tasks.rust_commit_hook]
script = [
"""
manifest_path=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/scripts/tool/rust-tool/Cargo.toml
cargo run --manifest-path ${manifest_path} hook --commit --path=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib
""",
]
script_runner = "@shell"
[tasks.reset_db]
script = [
"""
tool=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/scripts/tool/rust-tool/Cargo.toml
cargo run --manifest-path ${tool} db --reset
""",
]
script_runner = "@shell"
[tasks.plantuml]
script = ["""
brew cask install java
brew install graphviz
"""]
script_runner = "@shell"
# For the system Java wrappers to find this JDK, symlink it with
# sudo ln -sfn /Users/weidongfu/Documents/tools/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
# This is a beta version of openjdk for Apple Silicon
# (openjdk 16 preview).
# openjdk is keg-only, which means it was not symlinked into /Users/weidongfu/Documents/tools/homebrew,
# because it shadows the macOS `java` wrapper.
# If you need to have openjdk first in your PATH run:
# echo 'export PATH="/Users/weidongfu/Documents/tools/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zshrc