Revert "feat: use once_cell for perf."

This commit is contained in:
Nathan.fooo 2022-01-30 09:10:26 +08:00 committed by GitHub
parent fccd8ad4b9
commit 195bb9edcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 23 deletions

View File

@ -16,12 +16,13 @@ allo-isolate = {version = "^0.1", features = ["catch-unwind",]}
byteorder = {version = "1.3.4"} byteorder = {version = "1.3.4"}
ffi-support = {version = "0.4.2"} ffi-support = {version = "0.4.2"}
protobuf = {version = "2.20.0"} protobuf = {version = "2.20.0"}
lazy_static = {version = "1.4.0"}
tokio = { version = "1", features = ["rt", "rt-multi-thread"] } tokio = { version = "1", features = ["rt", "rt-multi-thread"] }
log = "0.4.14" log = "0.4.14"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = {version = "1.0"} serde_json = {version = "1.0"}
bytes = { version = "1.0" } bytes = { version = "1.0" }
once_cell = "1" parking_lot = "0.11"
lib-dispatch = {path = "../lib-dispatch" } lib-dispatch = {path = "../lib-dispatch" }
flowy-sdk = {path = "../flowy-sdk"} flowy-sdk = {path = "../flowy-sdk"}

View File

@ -9,10 +9,18 @@ use crate::{
model::{FFIRequest, FFIResponse}, model::{FFIRequest, FFIResponse},
}; };
use flowy_sdk::*; use flowy_sdk::*;
use lazy_static::lazy_static;
use lib_dispatch::prelude::*; use lib_dispatch::prelude::*;
use std::{ffi::CStr, os::raw::c_char}; use parking_lot::RwLock;
use std::{ffi::CStr, os::raw::c_char, sync::Arc};
const FLOWY_SDK: once_cell::sync::OnceCell<FlowySDK> = once_cell::sync::OnceCell::new(); lazy_static! {
static ref FLOWY_SDK: RwLock<Option<Arc<FlowySDK>>> = RwLock::new(None);
}
fn dispatch() -> Arc<EventDispatcher> {
FLOWY_SDK.read().as_ref().unwrap().dispatcher()
}
#[no_mangle] #[no_mangle]
pub extern "C" fn init_sdk(path: *mut c_char) -> i64 { pub extern "C" fn init_sdk(path: *mut c_char) -> i64 {
@ -21,7 +29,7 @@ pub extern "C" fn init_sdk(path: *mut c_char) -> i64 {
let server_config = get_client_server_configuration().unwrap(); let server_config = get_client_server_configuration().unwrap();
let config = FlowySDKConfig::new(path, server_config, "appflowy").log_filter("debug"); let config = FlowySDKConfig::new(path, server_config, "appflowy").log_filter("debug");
FLOWY_SDK.get_or_init(|| FlowySDK::new(config)); *FLOWY_SDK.write() = Some(Arc::new(FlowySDK::new(config)));
0 0
} }
@ -36,15 +44,7 @@ pub extern "C" fn async_event(port: i64, input: *const u8, len: usize) {
port port
); );
let dispatcher = match FLOWY_SDK.get() { let _ = EventDispatcher::async_send_with_callback(dispatch(), request, move |resp: EventResponse| {
None => {
log::error!("sdk not init yet.");
return;
}
Some(e) => e.dispatcher.clone(),
};
let _ = EventDispatcher::async_send_with_callback(dispatcher, request, move |resp: EventResponse| {
log::trace!("[FFI]: Post data to dart through {} port", port); log::trace!("[FFI]: Post data to dart through {} port", port);
Box::pin(post_to_flutter(resp, port)) Box::pin(post_to_flutter(resp, port))
}); });
@ -54,16 +54,7 @@ pub extern "C" fn async_event(port: i64, input: *const u8, len: usize) {
pub extern "C" fn sync_event(input: *const u8, len: usize) -> *const u8 { 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: ModuleRequest = FFIRequest::from_u8_pointer(input, len).into();
log::trace!("[FFI]: {} Sync Event: {:?}", &request.id, &request.event,); log::trace!("[FFI]: {} Sync Event: {:?}", &request.id, &request.event,);
let _response = EventDispatcher::sync_send(dispatch(), request);
let dispatcher = match FLOWY_SDK.get() {
None => {
log::error!("sdk not init yet.");
return forget_rust(vec![]);
}
Some(e) => e.dispatcher.clone(),
};
let _response = EventDispatcher::sync_send(dispatcher, request);
// FFIResponse { } // FFIResponse { }
let response_bytes = vec![]; let response_bytes = vec![];