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

167 lines
4.5 KiB
Rust
Raw Normal View History

2022-02-07 02:18:42 +00:00
pub use allo_isolate::ffi::DartCObject;
pub use allo_isolate::IntoDart;
use allo_isolate::Isolate;
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-02-07 02:18:42 +00:00
const MESSAGE_STREAM_ABORT_JSON: i32 = 7;
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,
T: IntoDart,
E: Serialize,
{
async_std::task::spawn(async move {
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,
T: Serialize,
E: Serialize,
{
async_std::task::spawn(async move {
self.result_json(future.await);
});
}
2022-03-09 03:32:12 +00:00
pub fn result<T: IntoDart, E: Serialize>(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-03-09 03:32:12 +00:00
pub fn result_json<T: Serialize, E: Serialize>(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-03-09 03:32:12 +00:00
pub fn ok_json<T: Serialize>(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-03-09 03:32:12 +00:00
// pub fn err<E: IntoDart>(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-03-09 03:32:12 +00:00
pub fn err_json<E: Serialize>(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
pub fn item_json<T: Serialize>(&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-02-15 18:40:17 +00:00
// pub fn abort<E: IntoDart>(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
pub fn abort_json<E: Serialize>(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-07 02:18:42 +00:00
isolate.post(vec![
MESSAGE_STREAM_ABORT_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
])
} else {
false
}
}
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
}
}
}