sse update
This commit is contained in:
		
							parent
							
								
									62521be363
								
							
						
					
					
						commit
						7d933e647b
					
				@ -6,22 +6,14 @@ use utoipa::ToSchema;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use crate::api::routes::{units::models::Unit, vendors::models::Vendor};
 | 
					use crate::api::routes::{units::models::Unit, vendors::models::Vendor};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Clone, Serialize, TS, ToSchema)]
 | 
					#[derive(Debug, Clone, Serialize, ToSchema)]
 | 
				
			||||||
#[ts(export)]
 | 
					 | 
				
			||||||
pub struct DataHubStream {
 | 
					pub struct DataHubStream {
 | 
				
			||||||
    /// Unique identifier for the data update event
 | 
					    /// Unique identifier for the data update event
 | 
				
			||||||
    id: String,
 | 
					    id: String,
 | 
				
			||||||
    /// Event Type
 | 
					    /// Event Type (always 'Message')
 | 
				
			||||||
    event: EventType,
 | 
					    event: String,
 | 
				
			||||||
    /// Data content
 | 
					    /// Data content
 | 
				
			||||||
    data: String,
 | 
					    data: DataHubData,
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#[derive(Debug, Clone, Serialize, TS, ToSchema, strum::Display)]
 | 
					 | 
				
			||||||
#[ts(export)]
 | 
					 | 
				
			||||||
pub enum EventType {
 | 
					 | 
				
			||||||
    Units,
 | 
					 | 
				
			||||||
    Vendors,
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug)]
 | 
				
			||||||
@ -41,7 +33,9 @@ impl DataHub {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Clone, Serialize)]
 | 
					#[derive(Debug, Clone, Serialize, TS, ToSchema)]
 | 
				
			||||||
 | 
					#[ts(export)]
 | 
				
			||||||
 | 
					#[serde(tag = "type", content = "data")]
 | 
				
			||||||
pub enum DataHubData {
 | 
					pub enum DataHubData {
 | 
				
			||||||
    Units(Vec<Unit>),
 | 
					    Units(Vec<Unit>),
 | 
				
			||||||
    Vendors(Vec<Vendor>),
 | 
					    Vendors(Vec<Vendor>),
 | 
				
			||||||
@ -49,19 +43,8 @@ pub enum DataHubData {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
impl From<DataHubData> for sse::Event {
 | 
					impl From<DataHubData> for sse::Event {
 | 
				
			||||||
    fn from(value: DataHubData) -> Self {
 | 
					    fn from(value: DataHubData) -> Self {
 | 
				
			||||||
        let (event_type, data) = match value {
 | 
					 | 
				
			||||||
            DataHubData::Units(units) => (
 | 
					 | 
				
			||||||
                EventType::Units,
 | 
					 | 
				
			||||||
                serde_json::to_string(&units).unwrap_or_default(),
 | 
					 | 
				
			||||||
            ),
 | 
					 | 
				
			||||||
            DataHubData::Vendors(vendors) => (
 | 
					 | 
				
			||||||
                EventType::Vendors,
 | 
					 | 
				
			||||||
                serde_json::to_string(&vendors).unwrap_or_default(),
 | 
					 | 
				
			||||||
            ),
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        sse::Event::default()
 | 
					        sse::Event::default()
 | 
				
			||||||
            .event(event_type.to_string())
 | 
					            // .event(event_type.to_string())
 | 
				
			||||||
            .data(data)
 | 
					            .data(serde_json::to_string(&value).unwrap_or_default())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user