Advertisement
Griffi

Untitled

Nov 10th, 2022
1,610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.29 KB | None | 0 0
  1. //todo this is garbage.
  2.  
  3. use bevy::prelude::*;
  4. use crossbeam_channel::{unbounded, Sender, Receiver};
  5. use opgd_gd_api::{client::GdClient, level::GdLevel};
  6.  
  7. enum Request {
  8.   PleaseDie,
  9.   DownloadLevel(i32)
  10. }
  11.  
  12. enum Response {
  13.   DownloadLevel(anyhow::Result<GdLevel>)
  14. }
  15.  
  16. struct GdClientTaskManager {
  17.   pub req: Sender<Request>,
  18.   pub res: Receiver<Response>
  19. }
  20. impl Drop for GdClientTaskManager {
  21.   fn drop(&mut self) {
  22.     info!("gd client task manager dropped, shutting down");
  23.     self.req.send(Request::PleaseDie).unwrap();
  24.   }
  25. }
  26.  
  27. fn setup(
  28.   mut commands: Commands
  29. ) {
  30.   let (req_tx, req_rx) = unbounded::<Request>();
  31.   let (res_tx, res_rx) = unbounded::<Response>();
  32.   tokio::spawn(async move {
  33.     let client = GdClient::new();
  34.     loop {
  35.       if let Some(response) = match req_rx.recv().unwrap() {
  36.         Request::PleaseDie => break,
  37.         Request::DownloadLevel(id) => Some(Response::DownloadLevel(client.download_level(id).await))
  38.       } {
  39.         res_tx.send(response).expect("Failed to send the response");
  40.       }
  41.     }
  42.   });
  43.   commands.insert_resource(GdClientTaskManager {
  44.     req: req_tx,
  45.     res: res_rx,
  46.   });
  47. }
  48.  
  49. pub struct GdClientPlugin;
  50. impl Plugin for GdClientPlugin {
  51.   fn build(&self, app: &mut App) {
  52.     app.add_startup_system(setup);
  53.   }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement