Skip to content

Commit

Permalink
Remove dependency on tokio for src-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
1whatleytay committed Aug 26, 2024
1 parent a27f2ad commit 48753ba
Show file tree
Hide file tree
Showing 19 changed files with 558 additions and 342 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"type": "module",
"author": "Taylor Whatley",
"scripts": {
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"wasm": "wasm-pack build ./src-wasm --out-dir ../src/utils/mips/wasm",
"dev": "yarn wasm && vite",
"build": "yarn wasm && vue-tsc --noEmit && vite build",
"preview": "vite preview",
"tauri": "tauri"
},
Expand Down Expand Up @@ -34,6 +35,7 @@
"vite": "^5.1.1",
"vite-plugin-top-level-await": "^1.4.1",
"vite-plugin-wasm": "^3.3.0",
"vue-tsc": "^1.0.0"
"vue-tsc": "^1.0.0",
"wasm-pack": "^0.13.0"
}
}
196 changes: 90 additions & 106 deletions src-backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ rust-version = "1.66"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.33.0", features = ["rt", "time", "macros"] }
futures = "0.3.29"

num = "0.4.1"
byteorder = "1.5.0"
base64 = "0.21.7"
rand = "0.8.5"
rand_chacha = "0.3.1"
tokio-util = "0.7.10"
async-trait = "0.1.77"

titan = { git = "https://github.com/1whatleytay/titan.git", branch = "main" }
18 changes: 10 additions & 8 deletions src-backend/src/channels.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::cmp::min;
use std::sync::Mutex;
use tokio::sync::mpsc;
use tokio::sync::mpsc::error::TrySendError::Full;
use tokio::sync::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};
use futures::channel::mpsc;
use futures::lock::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};
use futures::StreamExt;

struct ByteChannelReceiver {
receiver: mpsc::Receiver<Vec<u8>>, // this might panic in WASM
Expand Down Expand Up @@ -59,8 +59,8 @@ impl ByteChannelConsumption {
}
}

impl ByteChannel {
pub fn new() -> ByteChannel {
impl Default for ByteChannel {
fn default() -> Self {
// Not using unbounded channels out of lack of trust
let (sender, receiver) = mpsc::channel(12);

Expand All @@ -69,12 +69,14 @@ impl ByteChannel {
receiver: AsyncMutex::new(ByteChannelReceiver::new(receiver)),
}
}
}

impl ByteChannel {
pub fn send(&self, data: Vec<u8>) {
let mut state = self.sender.lock().unwrap();

if let Err(Full(data)) = state.sender.try_send(data) {
state.cache.push(data)
if let Err(err) = state.sender.try_send(data) {
state.cache.push(err.into_inner())
}
}

Expand All @@ -95,7 +97,7 @@ impl ByteChannel {
if let Some(value) = state.peek.take() {
Some(value)
} else {
let result = state.receiver.recv().await;
let result = state.receiver.next().await;

result.or_else(|| self.pop_cache()).map(|x| (0, x))
}
Expand Down
Loading

0 comments on commit 48753ba

Please sign in to comment.