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

@@ -5,11 +5,12 @@
import YourDolls from "./tabs/your-dolls/index.svelte";
import { listen } from "@tauri-apps/api/event";
import { onMount } from "svelte";
import { AppEvents } from "../../types/bindings/AppEventsConstants";
let showInteractionOverlay = false;
onMount(() => {
const unlisten = listen("set-interaction-overlay", (event) => {
const unlisten = listen(AppEvents.SetInteractionOverlay, (event) => {
showInteractionOverlay = event.payload as boolean;
});

View File

@@ -3,6 +3,7 @@
import { listen } from "@tauri-apps/api/event";
import { invoke } from "@tauri-apps/api/core";
import { appData } from "../../../events/app-data";
import { AppEvents } from "../../../types/bindings/AppEventsConstants";
import type { FriendRequestResponseDto } from "../../../types/bindings/FriendRequestResponseDto.js";
import type { FriendshipResponseDto } from "../../../types/bindings/FriendshipResponseDto.js";
import type { UserBasicDto } from "../../../types/bindings/UserBasicDto.js";
@@ -50,26 +51,26 @@
refreshSent();
unlisteners.push(
await listen("friend-request-received", () => {
await listen(AppEvents.FriendRequestReceived, () => {
refreshReceived();
}),
);
unlisteners.push(
await listen("friend-request-accepted", () => {
await listen(AppEvents.FriendRequestAccepted, () => {
refreshSent();
invoke("refresh_app_data");
}),
);
unlisteners.push(
await listen("friend-request-denied", () => {
await listen(AppEvents.FriendRequestDenied, () => {
refreshSent();
}),
);
unlisteners.push(
await listen("unfriended", () => {
await listen(AppEvents.Unfriended, () => {
invoke("refresh_app_data");
}),
);

View File

@@ -13,9 +13,13 @@
import { invoke } from "@tauri-apps/api/core";
import DesktopPet from "./components/DesktopPet.svelte";
import FullscreenModal from "./components/FullscreenModal.svelte";
import { receivedInteractions, clearInteraction } from "$lib/stores/interaction-store";
import {
receivedInteractions,
clearInteraction,
} from "$lib/stores/interaction-store";
import { INTERACTION_TYPE_HEADPAT } from "$lib/constants/interaction";
import { listen } from "@tauri-apps/api/event";
import { AppEvents } from "../../types/bindings/AppEventsConstants";
import { getSpriteSheetUrl } from "$lib/utils/sprite-utils";
import { onMount } from "svelte";
import type { PresenceStatus } from "../../types/bindings/PresenceStatus";
@@ -61,7 +65,9 @@
let userPetpetGif = "";
if (userDoll) {
try {
const gifBase64 = await invoke<string>("encode_pet_doll_gif_base64", { doll: userDoll });
const gifBase64 = await invoke<string>("encode_pet_doll_gif_base64", {
doll: userDoll,
});
userPetpetGif = `data:image/gif;base64,${gifBase64}`;
} catch (e) {
console.error("Failed to generate user petpet:", e);
@@ -102,9 +108,14 @@
if (interaction.type === INTERACTION_TYPE_HEADPAT) {
if (showFullscreenModal) {
// Queue the headpat for later (deduplicate by replacing existing from same user)
const existingIndex = headpatQueue.findIndex((h) => h.userId === userId);
const existingIndex = headpatQueue.findIndex(
(h) => h.userId === userId,
);
if (existingIndex >= 0) {
headpatQueue[existingIndex] = { userId, content: interaction.content };
headpatQueue[existingIndex] = {
userId,
content: interaction.content,
};
} else {
headpatQueue.push({ userId, content: interaction.content });
}
@@ -165,10 +176,12 @@
let presenceStatus: PresenceStatus | null = $state(null);
onMount(() => {
const unlisten = listen<UserStatus>("user-status-changed", (event) => {
console.log("event received");
presenceStatus = event.payload.presenceStatus;
});
const unlisten = listen<UserStatus>(
AppEvents.UserStatusChanged,
(event) => {
presenceStatus = event.payload.presenceStatus;
},
);
return () => {
unlisten.then((u) => u());