veilid/veilid-flutter/rust/src/dart_isolate_wrapper.rs

172 lines
4.7 KiB
Rust
Raw Normal View History

2022-06-29 17:39:54 +00:00
use crate::tools::*;
2022-02-07 02:18:42 +00:00
pub use allo_isolate::ffi::DartCObject;
pub use allo_isolate::IntoDart;
use allo_isolate::Isolate;
2022-06-10 21:07:10 +00:00
use core::fmt::Debug;
2022-02-07 02:18:42 +00:00
use core::future::Future;
use parking_lot::Mutex;
use serde::*;
use std::sync::Arc;
#[derive(Clone)]
pub struct DartIsolateWrapper {
isolate: Isolate,
}
const MESSAGE_OK: i32 = 0;
2022-02-15 18:40:17 +00:00
//const MESSAGE_ERR: i32 = 1;
2022-02-07 02:18:42 +00:00
const MESSAGE_OK_JSON: i32 = 2;
const MESSAGE_ERR_JSON: i32 = 3;
2022-02-15 18:40:17 +00:00
//const MESSAGE_STREAM_ITEM: i32 = 4;
2022-02-07 02:18:42 +00:00
const MESSAGE_STREAM_ITEM_JSON: i32 = 5;
2022-02-15 18:40:17 +00:00
//const MESSAGE_STREAM_ABORT: i32 = 6;
2022-09-09 20:27:13 +00:00
//const MESSAGE_STREAM_ABORT_JSON: i32 = 7;
2022-02-07 02:18:42 +00:00
const MESSAGE_STREAM_CLOSE: i32 = 8;
impl DartIsolateWrapper {
pub fn new(port: i64) -> Self {
DartIsolateWrapper {
isolate: Isolate::new(port),
}
}
2022-02-15 18:40:17 +00:00
pub fn spawn_result<F, T, E>(self, future: F)
where
F: Future<Output = Result<T, E>> + Send + 'static,
2022-06-10 21:07:10 +00:00
T: IntoDart + Debug,
E: Serialize + Debug,
2022-02-15 18:40:17 +00:00
{
2022-06-29 17:39:54 +00:00
spawn(async move {
2022-02-15 18:40:17 +00:00
self.result(future.await);
});
}
2022-02-07 02:18:42 +00:00
pub fn spawn_result_json<F, T, E>(self, future: F)
where
F: Future<Output = Result<T, E>> + Send + 'static,
2022-06-10 21:07:10 +00:00
T: Serialize + Debug,
E: Serialize + Debug,
2022-02-07 02:18:42 +00:00
{
2022-06-29 17:39:54 +00:00
spawn(async move {
2022-02-07 02:18:42 +00:00
self.result_json(future.await);
});
}
2022-06-10 21:07:10 +00:00
pub fn result<T: IntoDart + Debug, E: Serialize + Debug>(self, result: Result<T, E>) -> bool {
2022-02-07 02:18:42 +00:00
match result {
Ok(v) => self.ok(v),
2022-02-15 18:40:17 +00:00
Err(e) => self.err_json(e),
2022-02-07 02:18:42 +00:00
}
}
2022-06-10 21:07:10 +00:00
pub fn result_json<T: Serialize + Debug, E: Serialize + Debug>(
self,
result: Result<T, E>,
) -> bool {
2022-02-07 02:18:42 +00:00
match result {
Ok(v) => self.ok_json(v),
Err(e) => self.err_json(e),
}
}
2022-03-09 03:32:12 +00:00
pub fn ok<T: IntoDart>(self, value: T) -> bool {
2022-02-07 02:18:42 +00:00
self.isolate
.post(vec![MESSAGE_OK.into_dart(), value.into_dart()])
}
2022-06-10 21:07:10 +00:00
pub fn ok_json<T: Serialize + Debug>(self, value: T) -> bool {
2022-02-07 02:18:42 +00:00
self.isolate.post(vec![
MESSAGE_OK_JSON.into_dart(),
2022-06-08 01:31:05 +00:00
veilid_core::serialize_json(value).into_dart(),
2022-02-07 02:18:42 +00:00
])
}
2022-06-10 21:07:10 +00:00
// pub fn err<E: IntoDart + Debug>(self, error: E) -> bool {
2022-02-15 18:40:17 +00:00
// self.isolate
// .post(vec![MESSAGE_ERR.into_dart(), error.into_dart()])
// }
2022-02-07 02:18:42 +00:00
2022-06-10 21:07:10 +00:00
pub fn err_json<E: Serialize + Debug>(self, error: E) -> bool {
2022-02-07 02:18:42 +00:00
self.isolate.post(vec![
MESSAGE_ERR_JSON.into_dart(),
2022-06-08 01:31:05 +00:00
veilid_core::serialize_json(error).into_dart(),
2022-02-07 02:18:42 +00:00
])
}
}
2022-03-09 03:32:12 +00:00
struct DartIsolateStreamInner {
pub isolate: Option<Isolate>,
}
impl Drop for DartIsolateStreamInner {
fn drop(&mut self) {
if let Some(isolate) = self.isolate {
isolate.post(vec![MESSAGE_STREAM_CLOSE.into_dart()]);
}
}
}
2022-02-07 02:18:42 +00:00
#[derive(Clone)]
pub struct DartIsolateStream {
2022-03-09 03:32:12 +00:00
inner: Arc<Mutex<DartIsolateStreamInner>>,
2022-02-07 02:18:42 +00:00
}
impl DartIsolateStream {
pub fn new(port: i64) -> Self {
DartIsolateStream {
2022-03-09 03:32:12 +00:00
inner: Arc::new(Mutex::new(DartIsolateStreamInner {
isolate: Some(Isolate::new(port)),
})),
2022-02-07 02:18:42 +00:00
}
}
2022-02-15 18:40:17 +00:00
// pub fn item<T: IntoDart>(&self, value: T) -> bool {
2022-03-09 03:32:12 +00:00
// let mut inner = self.inner.lock();
// if let Some(isolate) = inner.isolate.take() {
2022-02-15 18:40:17 +00:00
// isolate.post(vec![MESSAGE_STREAM_ITEM.into_dart(), value.into_dart()])
// } else {
// false
// }
// }
2022-02-07 02:18:42 +00:00
2022-06-10 21:07:10 +00:00
pub fn item_json<T: Serialize + Debug>(&self, value: T) -> bool {
2022-03-09 03:32:12 +00:00
let inner = self.inner.lock();
if let Some(isolate) = &inner.isolate {
2022-02-07 02:18:42 +00:00
isolate.post(vec![
MESSAGE_STREAM_ITEM_JSON.into_dart(),
2022-06-08 01:31:05 +00:00
veilid_core::serialize_json(value).into_dart(),
2022-02-07 02:18:42 +00:00
])
} else {
false
}
}
2022-06-10 21:07:10 +00:00
// pub fn abort<E: IntoDart + Debug>(self, error: E) -> bool {
2022-03-09 03:32:12 +00:00
// let mut inner = self.inner.lock();
// if let Some(isolate) = inner.isolate.take() {
2022-02-15 18:40:17 +00:00
// isolate.post(vec![MESSAGE_STREAM_ABORT.into_dart(), error.into_dart()])
// } else {
// false
// }
// }
2022-02-07 02:18:42 +00:00
2022-09-09 20:27:13 +00:00
// pub fn abort_json<E: Serialize + Debug>(self, error: E) -> bool {
// let mut inner = self.inner.lock();
// if let Some(isolate) = inner.isolate.take() {
// isolate.post(vec![
// MESSAGE_STREAM_ABORT_JSON.into_dart(),
// veilid_core::serialize_json(error).into_dart(),
// ])
// } else {
// false
// }
// }
2022-02-07 02:18:42 +00:00
pub fn close(self) -> bool {
2022-03-09 03:32:12 +00:00
let mut inner = self.inner.lock();
if let Some(isolate) = inner.isolate.take() {
2022-02-07 02:18:42 +00:00
isolate.post(vec![MESSAGE_STREAM_CLOSE.into_dart()])
} else {
false
}
}
}