-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(torii-core): optimistically broadcast entity update #2466
Changes from 1 commit
5550d4b
7f43a45
1094542
7e3fe7c
597f38e
0f0567c
37b5f60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ use tracing::{debug, error}; | |
use crate::simple_broker::SimpleBroker; | ||
use crate::types::{ | ||
Entity as EntityUpdated, Event as EventEmitted, EventMessage as EventMessageUpdated, | ||
Model as ModelRegistered, | ||
Model as ModelRegistered, OptimisticEntity, OptimisticEventMessage, | ||
}; | ||
|
||
pub(crate) const LOG_TARGET: &str = "torii_core::executor"; | ||
|
@@ -185,6 +185,19 @@ impl<'c> Executor<'c> { | |
let mut entity_updated = EntityUpdated::from_row(&row)?; | ||
entity_updated.updated_model = Some(entity); | ||
entity_updated.deleted = false; | ||
|
||
let optimistic_entity = OptimisticEntity { | ||
id: entity_updated.id.clone(), | ||
keys: entity_updated.keys.clone(), | ||
event_id: entity_updated.event_id.clone(), | ||
executed_at: entity_updated.executed_at.clone(), | ||
created_at: entity_updated.created_at.clone(), | ||
updated_at: entity_updated.updated_at.clone(), | ||
updated_model: entity_updated.updated_model.clone(), | ||
deleted: entity_updated.deleted, | ||
}; | ||
SimpleBroker::publish(optimistic_entity); | ||
|
||
let broker_message = BrokerMessage::EntityUpdated(entity_updated); | ||
self.publish_queue.push(broker_message); | ||
} | ||
|
@@ -225,6 +238,17 @@ impl<'c> Executor<'c> { | |
entity_updated.deleted = true; | ||
} | ||
|
||
let optimistic_entity = OptimisticEntity { | ||
id: entity_updated.id.clone(), | ||
keys: entity_updated.keys.clone(), | ||
event_id: entity_updated.event_id.clone(), | ||
executed_at: entity_updated.executed_at.clone(), | ||
created_at: entity_updated.created_at.clone(), | ||
updated_at: entity_updated.updated_at.clone(), | ||
updated_model: entity_updated.updated_model.clone(), | ||
deleted: entity_updated.deleted, | ||
}; | ||
SimpleBroker::publish(optimistic_entity); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider refactoring to reduce code duplication when creating This block repeats the same cloning logic as before. Using the |
||
let broker_message = BrokerMessage::EntityUpdated(entity_updated); | ||
self.publish_queue.push(broker_message); | ||
} | ||
|
@@ -241,6 +265,18 @@ impl<'c> Executor<'c> { | |
})?; | ||
let mut event_message = EventMessageUpdated::from_row(&row)?; | ||
event_message.updated_model = Some(entity); | ||
|
||
let optimistic_event_message = OptimisticEventMessage { | ||
id: event_message.id.clone(), | ||
keys: event_message.keys.clone(), | ||
event_id: event_message.event_id.clone(), | ||
executed_at: event_message.executed_at.clone(), | ||
created_at: event_message.created_at.clone(), | ||
updated_at: event_message.updated_at.clone(), | ||
updated_model: event_message.updated_model.clone(), | ||
}; | ||
SimpleBroker::publish(optimistic_event_message); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider refactoring to reduce code duplication when creating This block duplicates field cloning from Implement the impl From<&EventMessageUpdated> for OptimisticEventMessage {
fn from(event_message: &EventMessageUpdated) -> Self {
Self {
id: event_message.id.clone(),
keys: event_message.keys.clone(),
event_id: event_message.event_id.clone(),
executed_at: event_message.executed_at.clone(),
created_at: event_message.created_at.clone(),
updated_at: event_message.updated_at.clone(),
updated_model: event_message.updated_model.clone(),
}
}
} Then, you can simplify the code to: let optimistic_event_message = OptimisticEventMessage::from(&event_message); |
||
let broker_message = BrokerMessage::EventMessageUpdated(event_message); | ||
self.publish_queue.push(broker_message); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,23 @@ pub struct Entity { | |
pub deleted: bool, | ||
} | ||
|
||
#[derive(FromRow, Deserialize, Debug, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct OptimisticEntity { | ||
pub id: String, | ||
pub keys: String, | ||
pub event_id: String, | ||
pub executed_at: DateTime<Utc>, | ||
pub created_at: DateTime<Utc>, | ||
pub updated_at: DateTime<Utc>, | ||
|
||
// this should never be None | ||
#[sqlx(skip)] | ||
pub updated_model: Option<Ty>, | ||
#[sqlx(skip)] | ||
pub deleted: bool, | ||
} | ||
Comment on lines
+49
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Ohayo, sensei! Consider refactoring to avoid duplication. The pub type OptimisticEntity = Entity; This approach would reduce code duplication and make maintenance easier. If there are specific reasons for keeping them separate, please add a comment explaining the rationale. Also, the pub updated_model: Ty, If |
||
|
||
#[derive(FromRow, Deserialize, Debug, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct EventMessage { | ||
|
@@ -61,6 +78,21 @@ pub struct EventMessage { | |
pub updated_model: Option<Ty>, | ||
} | ||
|
||
#[derive(FromRow, Deserialize, Debug, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct OptimisticEventMessage { | ||
pub id: String, | ||
pub keys: String, | ||
pub event_id: String, | ||
pub executed_at: DateTime<Utc>, | ||
pub created_at: DateTime<Utc>, | ||
pub updated_at: DateTime<Utc>, | ||
|
||
// this should never be None | ||
#[sqlx(skip)] | ||
pub updated_model: Option<Ty>, | ||
} | ||
Comment on lines
+81
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Ohayo again, sensei! Let's discuss the The
pub struct OptimisticEventMessageData {
pub event_message: EventMessage,
// Add any additional fields specific to OptimisticEventMessage
}
pub type OptimisticEventMessage = OptimisticEventMessageData;
#[sqlx(skip)]
pub deleted: bool,
These changes would improve code clarity and maintainability. What do you think, sensei? |
||
|
||
#[derive(FromRow, Deserialize, Debug, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Model { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider refactoring to reduce code duplication when creating
OptimisticEntity
This block duplicates field cloning from
entity_updated
to createOptimisticEntity
. To improve maintainability and reduce code duplication, consider implementing theFrom
trait to convertEntityUpdated
intoOptimisticEntity
.Implement the
From
trait forOptimisticEntity
:Then, you can simplify the code to: