frontend events system refactor

This commit is contained in:
2026-03-07 01:14:41 +08:00
parent 59253d286c
commit 93e33e8d64
14 changed files with 322 additions and 224 deletions

View File

@@ -15,6 +15,16 @@ pub enum AppEvents {
EditDoll,
CreateDoll,
UserStatusChanged,
FriendCursorPosition,
FriendDisconnected,
FriendActiveDollChanged,
FriendUserStatus,
InteractionReceived,
InteractionDeliveryFailed,
FriendRequestReceived,
FriendRequestAccepted,
FriendRequestDenied,
Unfriended,
}
impl AppEvents {
@@ -27,6 +37,16 @@ impl AppEvents {
AppEvents::EditDoll => "edit-doll",
AppEvents::CreateDoll => "create-doll",
AppEvents::UserStatusChanged => "user-status-changed",
AppEvents::FriendCursorPosition => "friend-cursor-position",
AppEvents::FriendDisconnected => "friend-disconnected",
AppEvents::FriendActiveDollChanged => "friend-active-doll-changed",
AppEvents::FriendUserStatus => "friend-user-status",
AppEvents::InteractionReceived => "interaction-received",
AppEvents::InteractionDeliveryFailed => "interaction-delivery-failed",
AppEvents::FriendRequestReceived => "friend-request-received",
AppEvents::FriendRequestAccepted => "friend-request-accepted",
AppEvents::FriendRequestDenied => "friend-request-denied",
AppEvents::Unfriended => "unfriended",
}
}
}

View File

@@ -1,26 +1,27 @@
use rust_socketio::{Payload, RawClient};
use tracing::info;
use crate::services::app_events::AppEvents;
use crate::services::cursor::{normalized_to_absolute, CursorPositions};
use crate::state::AppDataRefreshScope;
use super::{
emitter, refresh,
types::{IncomingFriendCursorPayload, OutgoingFriendCursorPayload, WS_EVENT},
types::{IncomingFriendCursorPayload, OutgoingFriendCursorPayload},
utils,
};
/// Handler for friend-request-received event
pub fn on_friend_request_received(payload: Payload, _socket: RawClient) {
if let Ok(value) = utils::extract_text_value(payload, "friend-request-received") {
emitter::emit_to_frontend(WS_EVENT::FRIEND_REQUEST_RECEIVED, value);
emitter::emit_to_frontend(AppEvents::FriendRequestReceived.as_str(), value);
}
}
/// Handler for friend-request-accepted event
pub fn on_friend_request_accepted(payload: Payload, _socket: RawClient) {
if let Ok(value) = utils::extract_text_value(payload, "friend-request-accepted") {
emitter::emit_to_frontend(WS_EVENT::FRIEND_REQUEST_ACCEPTED, value);
emitter::emit_to_frontend(AppEvents::FriendRequestAccepted.as_str(), value);
refresh::refresh_app_data(AppDataRefreshScope::Friends);
}
}
@@ -28,14 +29,14 @@ pub fn on_friend_request_accepted(payload: Payload, _socket: RawClient) {
/// Handler for friend-request-denied event
pub fn on_friend_request_denied(payload: Payload, _socket: RawClient) {
if let Ok(value) = utils::extract_text_value(payload, "friend-request-denied") {
emitter::emit_to_frontend(WS_EVENT::FRIEND_REQUEST_DENIED, value);
emitter::emit_to_frontend(AppEvents::FriendRequestDenied.as_str(), value);
}
}
/// Handler for unfriended event
pub fn on_unfriended(payload: Payload, _socket: RawClient) {
if let Ok(value) = utils::extract_text_value(payload, "unfriended") {
emitter::emit_to_frontend(WS_EVENT::UNFRIENDED, value);
emitter::emit_to_frontend(AppEvents::Unfriended.as_str(), value);
refresh::refresh_app_data(AppDataRefreshScope::Friends);
}
}
@@ -56,14 +57,14 @@ pub fn on_friend_cursor_position(payload: Payload, _socket: RawClient) {
},
};
emitter::emit_to_frontend(WS_EVENT::FRIEND_CURSOR_POSITION, outgoing_payload);
emitter::emit_to_frontend(AppEvents::FriendCursorPosition.as_str(), outgoing_payload);
}
}
/// Handler for friend-disconnected event
pub fn on_friend_disconnected(payload: Payload, _socket: RawClient) {
if let Ok(value) = utils::extract_text_value(payload, "friend-disconnected") {
emitter::emit_to_frontend(WS_EVENT::FRIEND_DISCONNECTED, value);
emitter::emit_to_frontend(AppEvents::FriendDisconnected.as_str(), value);
}
}
@@ -93,7 +94,7 @@ fn handle_friend_doll_change(event_name: &str, payload: Payload) {
/// Handler for friend-active-doll-changed event
pub fn on_friend_active_doll_changed(payload: Payload, _socket: RawClient) {
if let Ok(value) = utils::extract_text_value(payload, "friend-active-doll-changed") {
emitter::emit_to_frontend(WS_EVENT::FRIEND_ACTIVE_DOLL_CHANGED, value);
emitter::emit_to_frontend(AppEvents::FriendActiveDollChanged.as_str(), value);
refresh::refresh_app_data(AppDataRefreshScope::Friends);
}
}
@@ -101,6 +102,6 @@ pub fn on_friend_active_doll_changed(payload: Payload, _socket: RawClient) {
/// Handler for friend-user-status event
pub fn on_friend_user_status(payload: Payload, _socket: RawClient) {
if let Ok(value) = utils::extract_text_value(payload, "friend-user-status") {
emitter::emit_to_frontend(WS_EVENT::FRIEND_USER_STATUS, value);
emitter::emit_to_frontend(AppEvents::FriendUserStatus.as_str(), value);
}
}

View File

@@ -1,15 +1,16 @@
use rust_socketio::{Payload, RawClient};
use crate::models::interaction::{InteractionDeliveryFailedDto, InteractionPayloadDto};
use crate::services::app_events::AppEvents;
use super::{emitter, types::WS_EVENT, utils};
use super::{emitter, utils};
/// Handler for interaction-received event
pub fn on_interaction_received(payload: Payload, _socket: RawClient) {
if let Ok(data) =
utils::extract_and_parse::<InteractionPayloadDto>(payload, "interaction-received")
{
emitter::emit_to_frontend(WS_EVENT::INTERACTION_RECEIVED, data);
emitter::emit_to_frontend(AppEvents::InteractionReceived.as_str(), data);
}
}
@@ -19,6 +20,6 @@ pub fn on_interaction_delivery_failed(payload: Payload, _socket: RawClient) {
payload,
"interaction-delivery-failed",
) {
emitter::emit_to_frontend(WS_EVENT::INTERACTION_DELIVERY_FAILED, data);
emitter::emit_to_frontend(AppEvents::InteractionDeliveryFailed.as_str(), data);
}
}