AppFlowy/frontend/rust-lib/lib-infra/src/isolate_stream.rs
Nathan.fooo e9b2cbb317
chore: observe file upload state (#6172)
* chore: observe file upload state

* chore: observe file upload state

* chore: upgrade client api

* Update frontend/appflowy_flutter/lib/startup/tasks/file_storage_task.dart

Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io>

* chore: fix tauri build

---------

Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io>
2024-09-04 07:56:08 +08:00

47 lines
1.0 KiB
Rust

use allo_isolate::{IntoDart, Isolate};
use anyhow::anyhow;
use futures::Sink;
pub use futures_util::sink::SinkExt;
use pin_project::pin_project;
use std::pin::Pin;
use std::task::{Context, Poll};
#[pin_project]
pub struct IsolateSink {
isolate: Isolate,
}
impl IsolateSink {
pub fn new(isolate: Isolate) -> Self {
Self { isolate }
}
}
impl<T> Sink<T> for IsolateSink
where
T: IntoDart,
{
type Error = anyhow::Error;
fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
let this = self.project();
if this.isolate.post(item) {
Ok(())
} else {
Err(anyhow!("failed to post message"))
}
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
}