add persistent state

This commit is contained in:
xenofem 2022-04-28 05:13:14 -04:00
parent 70384b04c3
commit 127d7e9c67
4 changed files with 139 additions and 12 deletions

View file

@ -61,7 +61,7 @@ pub struct Uploader {
}
impl Uploader {
pub fn new(app_data: super::AppData) -> Self {
pub(crate) fn new(app_data: super::AppData) -> Self {
Self {
writer: None,
storage_filename: String::new(),
@ -110,6 +110,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
Ok(m) => m,
Err(e) => {
error!("Websocket error: {}", e);
self.cleanup_after_error(ctx);
ctx.stop();
return;
}
@ -122,6 +123,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
code: e.close_code(),
description: Some(e.to_string()),
}));
self.cleanup_after_error(ctx);
ctx.stop();
}
Ok(true) => {
@ -134,9 +136,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
let data = self.app_data.clone();
let filename = self.storage_filename.clone();
ctx.wait(actix::fut::wrap_future(async move {
if let Some(f) = data.write().await.get_mut(&filename) {
f.uploader.take();
}
data.write().await.remove_uploader(&filename);
}));
ctx.stop();
}
@ -219,10 +219,10 @@ impl Uploader {
data
.write()
.await
.insert(
.add_file(
storage_filename_copy,
downloadable_file,
);
).await.unwrap();
}));
ctx.text(self.storage_filename.as_str());
}
@ -267,4 +267,15 @@ impl Uploader {
Err(Error::UnexpectedMessageType)
}
}
fn cleanup_after_error(&mut self, ctx: &mut <Self as Actor>::Context) {
let data = self.app_data.clone();
let filename = self.storage_filename.clone();
ctx.spawn(actix::fut::wrap_future(async move {
data
.write()
.await
.remove_file(&filename).await.unwrap();
}));
}
}