Skip to content

Commit f65af06

Browse files
committed
Removed RefCell
1 parent 30ddc9c commit f65af06

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

crates/turborepo-ui/src/wui/server.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, sync::Arc};
1+
use std::sync::Arc;
22

33
use async_graphql::{
44
http::GraphiQLSource, EmptyMutation, EmptySubscription, Object, Schema, SimpleObject,
@@ -31,7 +31,6 @@ impl<'a> CurrentRun<'a> {
3131
self.state
3232
.lock()
3333
.await
34-
.borrow()
3534
.tasks()
3635
.iter()
3736
.map(|(task, state)| Task {
@@ -45,7 +44,7 @@ impl<'a> CurrentRun<'a> {
4544
/// We keep the state in a `Arc<Mutex<RefCell<T>>>` so both `Subscriber` and
4645
/// `Query` can access it, with `Subscriber` mutating it and `Query` only
4746
/// reading it.
48-
type SharedState = Arc<Mutex<RefCell<WebUIState>>>;
47+
pub(crate) type SharedState = Arc<Mutex<WebUIState>>;
4948

5049
pub struct Query {
5150
state: SharedState,
@@ -76,7 +75,7 @@ async fn graphiql() -> impl IntoResponse {
7675
pub async fn start_server(
7776
rx: tokio::sync::mpsc::UnboundedReceiver<WebUIEvent>,
7877
) -> Result<(), crate::Error> {
79-
let state = Arc::new(Mutex::new(RefCell::new(WebUIState::default())));
78+
let state = Arc::new(Mutex::new(WebUIState::default()));
8079
let subscriber = Subscriber::new(rx);
8180
tokio::spawn(subscriber.watch(state.clone()));
8281

crates/turborepo-ui/src/wui/subscriber.rs

+17-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::{cell::RefCell, collections::BTreeMap, sync::Arc};
1+
use std::{collections::BTreeMap, sync::Arc};
22

33
use async_graphql::{Enum, SimpleObject};
44
use serde::Serialize;
55
use tokio::sync::Mutex;
66

77
use crate::{
88
tui::event::{CacheResult, TaskResult},
9-
wui::event::WebUIEvent,
9+
wui::{event::WebUIEvent, server::SharedState},
1010
};
1111

1212
/// Subscribes to the Web UI events and updates the state
@@ -22,23 +22,23 @@ impl Subscriber {
2222
pub async fn watch(
2323
self,
2424
// We use a tokio::sync::Mutex here because we want this future to be Send.
25-
#[allow(clippy::type_complexity)] state: Arc<Mutex<RefCell<WebUIState>>>,
25+
#[allow(clippy::type_complexity)] state: SharedState,
2626
) {
2727
let mut rx = self.rx;
2828
while let Some(event) = rx.recv().await {
2929
Self::add_message(&state, event).await;
3030
}
3131
}
3232

33-
async fn add_message(state: &Arc<Mutex<RefCell<WebUIState>>>, event: WebUIEvent) {
34-
let state = state.lock().await;
33+
async fn add_message(state: &Arc<Mutex<WebUIState>>, event: WebUIEvent) {
34+
let mut state = state.lock().await;
3535

3636
match event {
3737
WebUIEvent::StartTask {
3838
task,
3939
output_logs: _,
4040
} => {
41-
state.borrow_mut().tasks.insert(
41+
state.tasks.insert(
4242
task,
4343
TaskState {
4444
output: Vec::new(),
@@ -49,35 +49,27 @@ impl Subscriber {
4949
);
5050
}
5151
WebUIEvent::TaskOutput { task, output } => {
52-
state
53-
.borrow_mut()
54-
.tasks
55-
.get_mut(&task)
56-
.unwrap()
57-
.output
58-
.extend(output);
52+
state.tasks.get_mut(&task).unwrap().output.extend(output);
5953
}
6054
WebUIEvent::EndTask { task, result } => {
61-
state.borrow_mut().tasks.get_mut(&task).unwrap().status = TaskStatus::from(result);
55+
state.tasks.get_mut(&task).unwrap().status = TaskStatus::from(result);
6256
}
6357
WebUIEvent::CacheStatus {
6458
task,
6559
result,
6660
message,
6761
} => {
68-
let mut state_ref = state.borrow_mut();
69-
7062
if result == CacheResult::Hit {
71-
state_ref.tasks.get_mut(&task).unwrap().status = TaskStatus::Cached;
63+
state.tasks.get_mut(&task).unwrap().status = TaskStatus::Cached;
7264
}
73-
state_ref.tasks.get_mut(&task).unwrap().cache_result = Some(result);
74-
state_ref.tasks.get_mut(&task).unwrap().cache_message = Some(message);
65+
state.tasks.get_mut(&task).unwrap().cache_result = Some(result);
66+
state.tasks.get_mut(&task).unwrap().cache_message = Some(message);
7567
}
7668
WebUIEvent::Stop => {
7769
// TODO: stop watching
7870
}
7971
WebUIEvent::UpdateTasks { tasks } => {
80-
state.borrow_mut().tasks = tasks
72+
state.tasks = tasks
8173
.into_iter()
8274
.map(|task| {
8375
(
@@ -93,7 +85,7 @@ impl Subscriber {
9385
.collect();
9486
}
9587
WebUIEvent::RestartTasks { tasks } => {
96-
state.borrow_mut().tasks = tasks
88+
state.tasks = tasks
9789
.into_iter()
9890
.map(|task| {
9991
(
@@ -164,7 +156,7 @@ mod test {
164156
#[tokio::test]
165157
async fn test_web_ui_state() -> Result<(), crate::Error> {
166158
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
167-
let state = Arc::new(Mutex::new(RefCell::new(WebUIState::default())));
159+
let state = Arc::new(Mutex::new(WebUIState::default()));
168160
let subscriber = Subscriber::new(rx);
169161

170162
let sender = WebUISender::new(tx);
@@ -188,7 +180,7 @@ mod test {
188180
// Run the subscriber blocking
189181
subscriber.watch(state.clone()).await;
190182

191-
let state_handle = state.lock().await.borrow().clone();
183+
let state_handle = state.lock().await.clone();
192184
assert_eq!(state_handle.tasks().len(), 3);
193185
assert_eq!(
194186
state_handle.tasks().get("task2").unwrap().status,
@@ -248,7 +240,7 @@ mod test {
248240
#[tokio::test]
249241
async fn test_restart_tasks() -> Result<(), crate::Error> {
250242
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
251-
let state = Arc::new(Mutex::new(RefCell::new(WebUIState::default())));
243+
let state = Arc::new(Mutex::new(WebUIState::default()));
252244
let subscriber = Subscriber::new(rx);
253245

254246
let sender = WebUISender::new(tx);
@@ -271,7 +263,7 @@ mod test {
271263
// Run the subscriber blocking
272264
subscriber.watch(state.clone()).await;
273265

274-
let state_handle = state.lock().await.borrow().clone();
266+
let state_handle = state.lock().await.clone();
275267
assert_eq!(state_handle.tasks().len(), 1);
276268
assert_eq!(
277269
state_handle.tasks().get("task").unwrap().status,

0 commit comments

Comments
 (0)