Skip to content

Commit d55e2f8

Browse files
Cy-Teksokra
andauthored
cleanup(turbopack): Embed Global vs Specific channel type in the Rust type system (#79291)
## Refactor CompilationEventQueue to use enum for event channel types ### What? Introduces a new `EventChannelType` enum to replace string-based event channel identification in the `CompilationEventQueue`. ### Why? This change improves type safety by replacing string-based channel identification with a proper enum. Using an enum instead of string literals makes the code more robust and more self-evident in what it's trying to accomplish. ### How? - Created a new `EventChannelType` enum with two variants: `Global` and `Type(String)` - Updated the `subscribers` map to use `EventChannelType` as keys instead of strings - Replaced string literals like `"*"` with the appropriate enum variant (`EventChannelType::Global`) - Modified the event subscription and publishing logic to work with the new enum-based approach --------- Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
1 parent 9ba8c16 commit d55e2f8

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

turbopack/crates/turbo-tasks/src/message_queue.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,24 @@ const MAX_QUEUE_SIZE: usize = 256;
1515
type ArcMx<T> = Arc<Mutex<T>>;
1616
type CompilationEventChannel = mpsc::Sender<Arc<dyn CompilationEvent>>;
1717

18+
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
19+
enum EventChannelType {
20+
Global,
21+
Type(String),
22+
}
23+
1824
pub struct CompilationEventQueue {
1925
event_history: ArcMx<VecDeque<Arc<dyn CompilationEvent>>>,
20-
subscribers: Arc<DashMap<String, Vec<CompilationEventChannel>>>,
26+
subscribers: Arc<DashMap<EventChannelType, Vec<CompilationEventChannel>>>,
2127
}
2228

2329
impl Default for CompilationEventQueue {
2430
fn default() -> Self {
2531
let subscribers = DashMap::new();
26-
subscribers.insert("*".to_owned(), Vec::<CompilationEventChannel>::new());
32+
subscribers.insert(
33+
EventChannelType::Global,
34+
Vec::<CompilationEventChannel>::new(),
35+
);
2736

2837
Self {
2938
event_history: Arc::new(Mutex::new(VecDeque::with_capacity(MAX_QUEUE_SIZE))),
@@ -51,7 +60,9 @@ impl CompilationEventQueue {
5160
history.push_back(message_clone.clone());
5261

5362
// Send to all active receivers of the same message type
54-
if let Some(mut type_subscribers) = subscribers.get_mut(message_clone.type_name()) {
63+
if let Some(mut type_subscribers) = subscribers.get_mut(&EventChannelType::Type(
64+
message_clone.type_name().to_owned(),
65+
)) {
5566
let mut removal_indices = Vec::new();
5667
for (ix, sender) in type_subscribers.iter().enumerate() {
5768
if sender.send(message_clone.clone()).await.is_err() {
@@ -65,7 +76,7 @@ impl CompilationEventQueue {
6576
}
6677

6778
// Send to all global message subscribers
68-
let mut all_channel = subscribers.get_mut("*").unwrap();
79+
let mut all_channel = subscribers.get_mut(&EventChannelType::Global).unwrap();
6980
let mut removal_indices = Vec::new();
7081
for (ix, sender) in all_channel.iter_mut().enumerate() {
7182
if sender.send(message_clone.clone()).await.is_err() {
@@ -95,7 +106,9 @@ impl CompilationEventQueue {
95106
// Store the sender
96107
if let Some(event_types) = event_types {
97108
for event_type in event_types.iter() {
98-
let mut type_subscribers = subscribers.entry(event_type.clone()).or_default();
109+
let mut type_subscribers = subscribers
110+
.entry(EventChannelType::Type(event_type.clone()))
111+
.or_default();
99112
type_subscribers.push(tx_clone.clone());
100113
}
101114

@@ -105,7 +118,8 @@ impl CompilationEventQueue {
105118
}
106119
}
107120
} else {
108-
let mut global_subscribers = subscribers.entry("*".to_string()).or_default();
121+
let mut global_subscribers =
122+
subscribers.entry(EventChannelType::Global).or_default();
109123
global_subscribers.push(tx_clone.clone());
110124

111125
for event in event_history.lock().await.iter() {

0 commit comments

Comments
 (0)