Skip to content

Commit

Permalink
Fix #260
Browse files Browse the repository at this point in the history
Co-authored-by: campbellcole <[email protected]>
  • Loading branch information
oscartbeaumont and campbellcole committed Mar 17, 2024
1 parent a0262d3 commit d636266
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/integrations/tauri.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{collections::HashMap, sync::Arc};
use std::{borrow::Borrow, collections::HashMap, sync::Arc};

use tauri::{
plugin::{Builder, TauriPlugin},
Manager, Runtime,
};
use tokio::sync::mpsc;
use tokio::sync::{mpsc, Mutex};

use crate::{
internal::jsonrpc::{self, handle_json_rpc, Sender, SubscriptionMap},
Expand All @@ -22,19 +22,26 @@ where
Builder::new("rspc")
.setup(|app_handle| {
let (tx, mut rx) = mpsc::unbounded_channel::<jsonrpc::Request>();
let (mut resp_tx, mut resp_rx) = mpsc::unbounded_channel::<jsonrpc::Response>();
let mut subscriptions = HashMap::new();
let (resp_tx, mut resp_rx) = mpsc::unbounded_channel::<jsonrpc::Response>();
// TODO: Don't keep using a tokio mutex. We don't need to hold it over the await point.
let subscriptions = Arc::new(Mutex::new(HashMap::new()));

tokio::spawn(async move {
while let Some(req) = rx.recv().await {
handle_json_rpc(
ctx_fn(),
req,
&router,
&mut Sender::ResponseChannel(&mut resp_tx),
&mut SubscriptionMap::Ref(&mut subscriptions),
)
.await;
let ctx = ctx_fn();
let router = router.clone();
let mut resp_tx = resp_tx.clone();
let subscriptions = subscriptions.clone();
tokio::spawn(async move {
handle_json_rpc(
ctx,
req,
&router,
&mut Sender::ResponseChannel(&mut resp_tx),
&mut SubscriptionMap::Mutex(subscriptions.borrow()),
)
.await;
});
}
});

Expand Down

0 comments on commit d636266

Please sign in to comment.