diff --git a/.gitignore b/.gitignore
index 884eddcb82..088ba6ba7d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,6 +8,3 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
-
-
-/rust-lib/flowy-derive
\ No newline at end of file
diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml
index 1e952e4d5f..88a96cd453 100644
--- a/.idea/libraries/Dart_Packages.xml
+++ b/.idea/libraries/Dart_Packages.xml
@@ -524,14 +524,6 @@
-
-
-
-
-
-
-
-
@@ -841,8 +833,6 @@
-
-
diff --git a/app_flowy/.vscode/launch.json b/app_flowy/.vscode/launch.json
index 5e7d5eef3c..836c8b2753 100644
--- a/app_flowy/.vscode/launch.json
+++ b/app_flowy/.vscode/launch.json
@@ -9,6 +9,7 @@
"request": "launch",
"program": "${workspaceRoot}/lib/main.dart",
"type": "dart",
+ // "preLaunchTask": "build rust sdk"
},
{
"name": "app_flowy (profile mode)",
diff --git a/app_flowy/lib/startup/startup.dart b/app_flowy/lib/startup/startup.dart
index f990c67fff..31a75755a9 100644
--- a/app_flowy/lib/startup/startup.dart
+++ b/app_flowy/lib/startup/startup.dart
@@ -25,7 +25,7 @@ class App {
resolveDependencies(env);
// add task
- // getIt().addTask(RustSDKInitTask());
+ getIt().addTask(RustSDKInitTask());
getIt().addTask(AppWidgetTask());
// execute the tasks
diff --git a/app_flowy/packages/flowy_sdk/example/pubspec.lock b/app_flowy/packages/flowy_sdk/example/pubspec.lock
index 8043273325..66181a88e0 100644
--- a/app_flowy/packages/flowy_sdk/example/pubspec.lock
+++ b/app_flowy/packages/flowy_sdk/example/pubspec.lock
@@ -92,13 +92,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.1"
- fixnum:
- dependency: transitive
- description:
- name: fixnum
- url: "https://pub.dartlang.org"
- source: hosted
- version: "0.10.11"
flowy_logger:
dependency: transitive
description:
@@ -106,13 +99,6 @@ packages:
relative: true
source: path
version: "0.0.1"
- flowy_protobuf:
- dependency: transitive
- description:
- path: "../../flowy_protobuf"
- relative: true
- source: path
- version: "0.0.1"
flowy_sdk:
dependency: "direct main"
description:
@@ -215,13 +201,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.1"
- protobuf:
- dependency: transitive
- description:
- name: protobuf
- url: "https://pub.dartlang.org"
- source: hosted
- version: "1.1.0"
sky_engine:
dependency: transitive
description: flutter
diff --git a/app_flowy/packages/flowy_sdk/lib/ffi/adaptor.dart b/app_flowy/packages/flowy_sdk/lib/ffi/adaptor.dart
index 26face2e2d..3ee9fdac20 100644
--- a/app_flowy/packages/flowy_sdk/lib/ffi/adaptor.dart
+++ b/app_flowy/packages/flowy_sdk/lib/ffi/adaptor.dart
@@ -1,3 +1,4 @@
+import 'dart:convert';
import 'dart:ffi';
// ignore: import_of_legacy_library_into_null_safe
import 'package:isolates/isolates.dart';
@@ -5,7 +6,7 @@ import 'package:isolates/isolates.dart';
import 'package:isolates/ports.dart';
import 'package:ffi/ffi.dart';
-import 'package:flowy_protobuf/model/grpc.pb.dart';
+// ignore: unused_import
import 'package:flutter/services.dart';
import 'dart:async';
import 'dart:typed_data';
@@ -22,9 +23,25 @@ class FFIAdaptorException implements Exception {
FFIAdaptorException(this.type);
}
+class FFICommand {
+ final String event;
+ final Uint8List payload;
+ FFICommand(this.event, this.payload);
+
+ Map toJson() => {
+ 'event': event,
+ 'payload': payload,
+ };
+}
+
class FFIAdaptor {
- static Completer asyncRequest(RequestPacket request) {
- Uint8List bytes = request.writeToBuffer();
+ static Completer asyncRequest() {
+ // final command = FFICommand(
+ // "AuthCheck", Uint8List.fromList(utf8.encode("this is payload")));
+
+ final command = FFICommand("AuthCheck", Uint8List(0));
+
+ Uint8List bytes = Uint8List.fromList(utf8.encode(jsonEncode(command)));
assert(bytes.isEmpty == false);
if (bytes.isEmpty) {
@@ -43,4 +60,3 @@ class FFIAdaptor {
return completer;
}
}
-
diff --git a/app_flowy/packages/flowy_sdk/lib/ffi/ffi.dart b/app_flowy/packages/flowy_sdk/lib/ffi/ffi.dart
index 7dc5fa8e2f..df3ff0de61 100644
--- a/app_flowy/packages/flowy_sdk/lib/ffi/ffi.dart
+++ b/app_flowy/packages/flowy_sdk/lib/ffi/ffi.dart
@@ -45,25 +45,24 @@ typedef _invoke_async_Dart = void Function(
int len,
);
-
/// C function `command_sync`.
Pointer sync_command(
- Pointer input,
- int len,
- ) {
+ Pointer input,
+ int len,
+) {
return _invoke_sync(input, len);
}
final _invoke_sync_Dart _invoke_sync =
-_dl.lookupFunction<_invoke_sync_C, _invoke_sync_Dart>('sync_command');
+ _dl.lookupFunction<_invoke_sync_C, _invoke_sync_Dart>('sync_command');
typedef _invoke_sync_C = Pointer Function(
- Pointer input,
- Uint64 len,
- );
+ Pointer input,
+ Uint64 len,
+);
typedef _invoke_sync_Dart = Pointer Function(
- Pointer input,
- int len,
- );
+ Pointer input,
+ int len,
+);
/// C function `init_sdk`.
int init_sdk(
@@ -111,11 +110,12 @@ typedef _store_dart_post_cobject_Dart = void Function(
bool is_tester() {
if (Foundation.kDebugMode) {
// ignore: unnecessary_null_comparison
- if (Platform.executable == null) {
- return false;
- } else {
- return Platform.executable.contains("tester");
- }
+ // if (Platform.executable.isEmpty) {
+ // return false;
+ // } else {
+ // return Platform.executable.contains("tester");
+ // }
+ return false;
} else {
return false;
}
diff --git a/app_flowy/packages/flowy_sdk/lib/flowy_sdk.dart b/app_flowy/packages/flowy_sdk/lib/flowy_sdk.dart
index 5abe02caea..6b2ee9265b 100644
--- a/app_flowy/packages/flowy_sdk/lib/flowy_sdk.dart
+++ b/app_flowy/packages/flowy_sdk/lib/flowy_sdk.dart
@@ -4,6 +4,7 @@ import 'dart:io';
import 'dart:async';
import 'package:flutter/services.dart';
import 'dart:ffi';
+import 'ffi/adaptor.dart';
import 'ffi/ffi.dart' as ffi;
import 'package:ffi/ffi.dart';
@@ -16,12 +17,13 @@ class FlowySDK {
const FlowySDK();
- void dispose() {
-
- }
+ void dispose() {}
Future init(Directory sdkDir) async {
ffi.store_dart_post_cobject(NativeApi.postCObject);
+
ffi.init_sdk(sdkDir.path.toNativeUtf8());
+ final resp = await FFIAdaptor.asyncRequest();
+ print(resp);
}
}
diff --git a/app_flowy/packages/flowy_sdk/macos/Classes/binding.h b/app_flowy/packages/flowy_sdk/macos/Classes/binding.h
index ac6bcf50e4..0455d23a96 100644
--- a/app_flowy/packages/flowy_sdk/macos/Classes/binding.h
+++ b/app_flowy/packages/flowy_sdk/macos/Classes/binding.h
@@ -8,4 +8,6 @@ int64_t init_sdk(char *path);
void async_command(int64_t port, const uint8_t *input, uintptr_t len);
+const uint8_t *sync_command(const uint8_t *input, uintptr_t len);
+
void link_me_please(void);
\ No newline at end of file
diff --git a/app_flowy/packages/flowy_sdk/pubspec.lock b/app_flowy/packages/flowy_sdk/pubspec.lock
index 29eacfab55..226a179f9b 100644
--- a/app_flowy/packages/flowy_sdk/pubspec.lock
+++ b/app_flowy/packages/flowy_sdk/pubspec.lock
@@ -204,13 +204,6 @@ packages:
relative: true
source: path
version: "0.0.1"
- flowy_protobuf:
- dependency: "direct main"
- description:
- path: "../flowy_protobuf"
- relative: true
- source: path
- version: "0.0.1"
flutter:
dependency: "direct main"
description: flutter
@@ -354,13 +347,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
- protobuf:
- dependency: transitive
- description:
- name: protobuf
- url: "https://pub.dartlang.org"
- source: hosted
- version: "1.1.0"
pub_semver:
dependency: transitive
description:
diff --git a/app_flowy/packages/flowy_sdk/pubspec.yaml b/app_flowy/packages/flowy_sdk/pubspec.yaml
index e90d63afb0..0f75d84953 100644
--- a/app_flowy/packages/flowy_sdk/pubspec.yaml
+++ b/app_flowy/packages/flowy_sdk/pubspec.yaml
@@ -16,8 +16,6 @@ dependencies:
isolates: ^3.0.3+8
flowy_logger:
path: ../flowy_logger
- flowy_protobuf:
- path: ../flowy_protobuf
infra:
path: ../infra
dartz: '0.10.0-nullsafety.2'
diff --git a/app_flowy/pubspec.lock b/app_flowy/pubspec.lock
index 4caf2001f8..c3225910e2 100644
--- a/app_flowy/pubspec.lock
+++ b/app_flowy/pubspec.lock
@@ -232,13 +232,6 @@ packages:
relative: true
source: path
version: "0.0.1"
- flowy_protobuf:
- dependency: "direct main"
- description:
- path: "packages/flowy_protobuf"
- relative: true
- source: path
- version: "0.0.1"
flowy_sdk:
dependency: "direct main"
description:
@@ -501,13 +494,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.1"
- protobuf:
- dependency: transitive
- description:
- name: protobuf
- url: "https://pub.dartlang.org"
- source: hosted
- version: "2.0.0"
provider:
dependency: transitive
description:
diff --git a/app_flowy/pubspec.yaml b/app_flowy/pubspec.yaml
index 72d88d79bd..9d276bda02 100644
--- a/app_flowy/pubspec.yaml
+++ b/app_flowy/pubspec.yaml
@@ -31,8 +31,6 @@ dependencies:
sdk: flutter
flowy_sdk:
path: packages/flowy_sdk
- flowy_protobuf:
- path: packages/flowy_protobuf
flowy_style:
path: packages/flowy_style
diff --git a/rust-lib/dart-ffi/Cargo.toml b/rust-lib/dart-ffi/Cargo.toml
index 5d7006663a..42b0f6ce86 100644
--- a/rust-lib/dart-ffi/Cargo.toml
+++ b/rust-lib/dart-ffi/Cargo.toml
@@ -20,7 +20,7 @@ byteorder = {version = "1.3.4"}
ffi-support = {version = "0.4.2"}
protobuf = {version = "2.20.0"}
lazy_static = {version = "1.4.0"}
-tokio = { version = "1", features = ["sync"] }
+tokio = { version = "1", features = ["rt", "rt-multi-thread"] }
log = "0.4.14"
serde = { version = "1.0", features = ["derive"] }
serde_json = {version = "1.0"}
diff --git a/rust-lib/dart-ffi/src/lib.rs b/rust-lib/dart-ffi/src/lib.rs
index a8e90739f4..95ce9aa534 100644
--- a/rust-lib/dart-ffi/src/lib.rs
+++ b/rust-lib/dart-ffi/src/lib.rs
@@ -3,50 +3,50 @@ mod c;
use crate::c::forget_rust;
use flowy_sdk::*;
use flowy_sys::prelude::*;
+use lazy_static::lazy_static;
use std::{cell::RefCell, ffi::CStr, future::Future, os::raw::c_char};
+lazy_static! {
+ pub static ref FFI_RUNTIME: tokio::runtime::Runtime =
+ tokio::runtime::Builder::new_current_thread()
+ .thread_name("flowy-dart-ffi")
+ .build()
+ .unwrap();
+}
+
#[no_mangle]
pub extern "C" fn init_sdk(path: *mut c_char) -> i64 {
let c_str: &CStr = unsafe { CStr::from_ptr(path) };
let path: &str = c_str.to_str().unwrap();
- FlowySDK::init_log();
+ FlowySDK::init_log(path);
FlowySDK::init(path);
return 1;
}
-#[derive(serde::Deserialize)]
-pub struct FFICommand {
- event: String,
- payload: Vec,
-}
-
-impl FFICommand {
- pub fn from_bytes(bytes: Vec) -> Self {
- let command: FFICommand = serde_json::from_slice(&bytes).unwrap();
- command
- }
-
- pub fn from_u8_pointer(pointer: *const u8, len: usize) -> Self {
- let bytes = unsafe { std::slice::from_raw_parts(pointer, len) }.to_vec();
- FFICommand::from_bytes(bytes)
- }
-}
-
#[no_mangle]
pub extern "C" fn async_command(port: i64, input: *const u8, len: usize) {
let FFICommand { event, payload } = FFICommand::from_u8_pointer(input, len);
- log::info!("Event: {:?}", event);
-
- let mut request = DispatchRequest::new(port, event).callback(|_, resp| {
- log::info!("async resp: {:?}", resp);
- });
-
+ let mut request = DispatchRequest::new(event);
+ log::trace!(
+ "[FFI]: {} Async Event: {:?} with {} port",
+ &request.id,
+ &request.event,
+ port
+ );
if !payload.is_empty() {
request = request.payload(Payload::Bytes(payload));
}
- async_send(request);
- spawn_future(async { vec![] }, 123);
+ request = request.callback(Box::new(move |resp: EventResponse| {
+ let bytes = match resp.data {
+ ResponseData::Bytes(bytes) => bytes,
+ ResponseData::None => vec![],
+ };
+ log::trace!("[FFI]: Post data to dart through {} port", port);
+ Box::pin(spawn_future(async { bytes }, port))
+ }));
+
+ let _ = EventDispatch::async_send(request);
}
#[no_mangle]
@@ -56,19 +56,36 @@ pub extern "C" fn sync_command(input: *const u8, len: usize) -> *const u8 { unim
#[no_mangle]
pub extern "C" fn link_me_please() {}
+#[derive(serde::Deserialize)]
+pub struct FFICommand {
+ event: String,
+ payload: Vec,
+}
+
+impl FFICommand {
+ pub fn from_u8_pointer(pointer: *const u8, len: usize) -> Self {
+ let bytes = unsafe { std::slice::from_raw_parts(pointer, len) }.to_vec();
+ let command: FFICommand = serde_json::from_slice(&bytes).unwrap();
+ command
+ }
+}
+
#[inline(always)]
-fn spawn_future(future: F, port: i64)
+async fn spawn_future(future: F, port: i64)
where
F: Future