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};
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Clone, Serialize, TS, ToSchema)]
 | 
			
		||||
#[ts(export)]
 | 
			
		||||
#[derive(Debug, Clone, Serialize, ToSchema)]
 | 
			
		||||
pub struct DataHubStream {
 | 
			
		||||
    /// Unique identifier for the data update event
 | 
			
		||||
    id: String,
 | 
			
		||||
    /// Event Type
 | 
			
		||||
    event: EventType,
 | 
			
		||||
    /// Event Type (always 'Message')
 | 
			
		||||
    event: String,
 | 
			
		||||
    /// Data content
 | 
			
		||||
    data: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Clone, Serialize, TS, ToSchema, strum::Display)]
 | 
			
		||||
#[ts(export)]
 | 
			
		||||
pub enum EventType {
 | 
			
		||||
    Units,
 | 
			
		||||
    Vendors,
 | 
			
		||||
    data: DataHubData,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[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 {
 | 
			
		||||
    Units(Vec<Unit>),
 | 
			
		||||
    Vendors(Vec<Vendor>),
 | 
			
		||||
@ -49,19 +43,8 @@ pub enum DataHubData {
 | 
			
		||||
 | 
			
		||||
impl From<DataHubData> for sse::Event {
 | 
			
		||||
    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()
 | 
			
		||||
            .event(event_type.to_string())
 | 
			
		||||
            .data(data)
 | 
			
		||||
            // .event(event_type.to_string())
 | 
			
		||||
            .data(serde_json::to_string(&value).unwrap_or_default())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user