diff --git a/src/events/app-data.ts b/src/events/app-data.ts index e05d0a4..a212245 100644 --- a/src/events/app-data.ts +++ b/src/events/app-data.ts @@ -1,10 +1,10 @@ import { writable } from "svelte/store"; import { commands, events, type UserData } from "$lib/bindings"; -import { createListenerSubscription, setupHmrCleanup } from "./listener-utils"; +import { createListenersSubscription, setupHmrCleanup } from "./listener-utils"; export const appData = writable(null); -const subscription = createListenerSubscription(); +const subscription = createListenersSubscription(); /** * Starts listening for app data refresh events. @@ -17,7 +17,7 @@ export async function startAppData() { const unlisten = await events.appDataRefreshed.listen((event) => { appData.set(event.payload); }); - subscription.setUnlisten(unlisten); + subscription.addUnlisten(unlisten); subscription.setListening(true); } catch (error) { console.error(error); diff --git a/src/events/cursor.ts b/src/events/cursor.ts index 448eecb..a4ca912 100644 --- a/src/events/cursor.ts +++ b/src/events/cursor.ts @@ -1,13 +1,13 @@ import { writable } from "svelte/store"; import { events, type CursorPositions } from "$lib/bindings"; -import { createListenerSubscription, setupHmrCleanup } from "./listener-utils"; +import { createListenersSubscription, setupHmrCleanup } from "./listener-utils"; export const cursorPositionOnScreen = writable({ raw: { x: 0, y: 0 }, mapped: { x: 0, y: 0 }, }); -const subscription = createListenerSubscription(); +const subscription = createListenersSubscription(); /** * Starts tracking the local cursor position. @@ -20,7 +20,7 @@ export async function startCursorTracking() { const unlisten = await events.cursorMoved.listen((event) => { cursorPositionOnScreen.set(event.payload); }); - subscription.setUnlisten(unlisten); + subscription.addUnlisten(unlisten); subscription.setListening(true); } catch (err) { console.error("Failed to initialize cursor tracking:", err); diff --git a/src/events/friend-cursor.ts b/src/events/friend-cursor.ts index 0f4fc29..340467f 100644 --- a/src/events/friend-cursor.ts +++ b/src/events/friend-cursor.ts @@ -3,12 +3,10 @@ import { events, type CursorPositions, type DollDto, - type FriendActiveDollChangedPayload, - type FriendDisconnectedPayload, type OutgoingFriendCursorPayload, } from "$lib/bindings"; import { - createMultiListenerSubscription, + createListenersSubscription, removeFromStore, setupHmrCleanup, } from "./listener-utils"; @@ -23,7 +21,7 @@ export const friendsCursorPositions = writable>( ); export const friendsActiveDolls = writable>({}); -const subscription = createMultiListenerSubscription(); +const subscription = createListenersSubscription(); let friendCursorState: Record = {}; @@ -37,8 +35,8 @@ export async function startFriendCursorTracking() { try { // TODO: Add initial sync for existing friends' cursors and dolls if needed - const unlistenFriendCursor = await events.friendCursorPositionUpdated.listen( - (event) => { + const unlistenFriendCursor = + await events.friendCursorPositionUpdated.listen((event) => { const data: OutgoingFriendCursorPayload = event.payload; friendCursorState[data.userId] = { @@ -52,8 +50,7 @@ export async function startFriendCursorTracking() { [data.userId]: data.position, }; }); - }, - ); + }); subscription.addUnlisten(unlistenFriendCursor); const unlistenFriendDisconnected = await events.friendDisconnected.listen( @@ -72,30 +69,28 @@ export async function startFriendCursorTracking() { subscription.addUnlisten(unlistenFriendDisconnected); const unlistenFriendActiveDollChanged = - await events.friendActiveDollChanged.listen( - (event) => { - const payload = event.payload; + await events.friendActiveDollChanged.listen((event) => { + const payload = event.payload; - if (!payload.doll) { - friendsActiveDolls.update((current) => { - const next = { ...current }; - next[payload.friendId] = null; - return next; - }); + if (!payload.doll) { + friendsActiveDolls.update((current) => { + const next = { ...current }; + next[payload.friendId] = null; + return next; + }); - friendsCursorPositions.update((current) => - removeFromStore(current, payload.friendId), - ); - } else { - friendsActiveDolls.update((current) => { - return { - ...current, - [payload.friendId]: payload.doll, - }; - }); - } - }, - ); + friendsCursorPositions.update((current) => + removeFromStore(current, payload.friendId), + ); + } else { + friendsActiveDolls.update((current) => { + return { + ...current, + [payload.friendId]: payload.doll, + }; + }); + } + }); subscription.addUnlisten(unlistenFriendActiveDollChanged); subscription.setListening(true); diff --git a/src/events/interaction.ts b/src/events/interaction.ts index 0cabc7b..7ccb24e 100644 --- a/src/events/interaction.ts +++ b/src/events/interaction.ts @@ -1,17 +1,10 @@ import { writable } from "svelte/store"; -import { - events, - type InteractionDeliveryFailedDto, - type InteractionPayloadDto, -} from "$lib/bindings"; -import { - createMultiListenerSubscription, - setupHmrCleanup, -} from "./listener-utils"; +import { events, type InteractionPayloadDto } from "$lib/bindings"; +import { createListenersSubscription, setupHmrCleanup } from "./listener-utils"; -export const receivedInteractions = writable>( - new Map(), -); +export const receivedInteractions = writable< + Map +>(new Map()); export function addInteraction(interaction: InteractionPayloadDto) { receivedInteractions.update((map) => { @@ -29,7 +22,7 @@ export function clearInteraction(userId: string) { }); } -const subscription = createMultiListenerSubscription(); +const subscription = createListenersSubscription(); /** * Starts listening for interaction events (received and delivery failed). @@ -38,9 +31,11 @@ export async function startInteraction() { if (subscription.isListening()) return; try { - const unlistenReceived = await events.interactionReceived.listen((event) => { - addInteraction(event.payload); - }); + const unlistenReceived = await events.interactionReceived.listen( + (event) => { + addInteraction(event.payload); + }, + ); subscription.addUnlisten(unlistenReceived); const unlistenFailed = await events.interactionDeliveryFailed.listen( diff --git a/src/events/listener-utils.ts b/src/events/listener-utils.ts index 3a087a3..a5dde4a 100644 --- a/src/events/listener-utils.ts +++ b/src/events/listener-utils.ts @@ -1,47 +1,15 @@ import type { UnlistenFn } from "@tauri-apps/api/event"; export type ListenerSubscription = { - stop: () => void; - isListening: () => boolean; - setListening: (value: boolean) => void; - setUnlisten: (unlisten: UnlistenFn | null) => void; -}; - -export type MultiListenerSubscription = { stop: () => void; isListening: () => boolean; setListening: (value: boolean) => void; addUnlisten: (unlisten: UnlistenFn | null) => void; }; -export function createListenerSubscription( +export function createListenersSubscription( stopFn: () => void = () => {}, ): ListenerSubscription { - let unlisten: UnlistenFn | null = null; - let listening = false; - - return { - stop: () => { - if (unlisten) { - unlisten(); - unlisten = null; - } - listening = false; - stopFn(); - }, - isListening: () => listening, - setListening: (value) => { - listening = value; - }, - setUnlisten: (next) => { - unlisten = next; - }, - }; -} - -export function createMultiListenerSubscription( - stopFn: () => void = () => {}, -): MultiListenerSubscription { let unlistens: UnlistenFn[] = []; let listening = false; diff --git a/src/events/scene-interactive.ts b/src/events/scene-interactive.ts index ebfef49..650ebb6 100644 --- a/src/events/scene-interactive.ts +++ b/src/events/scene-interactive.ts @@ -1,10 +1,10 @@ import { writable } from "svelte/store"; import { commands, events } from "$lib/bindings"; -import { createListenerSubscription, setupHmrCleanup } from "./listener-utils"; +import { createListenersSubscription, setupHmrCleanup } from "./listener-utils"; export const sceneInteractive = writable(false); -const subscription = createListenerSubscription(); +const subscription = createListenersSubscription(); /** * Starts listening for scene interactive state changes. @@ -18,7 +18,7 @@ export async function startSceneInteractive() { const unlisten = await events.sceneInteractiveChanged.listen((event) => { sceneInteractive.set(Boolean(event.payload)); }); - subscription.setUnlisten(unlisten); + subscription.addUnlisten(unlisten); subscription.setListening(true); } catch (error) { console.error("Failed to initialize scene interactive listener:", error); diff --git a/src/events/user-status.ts b/src/events/user-status.ts index 852a4e8..0f293d8 100644 --- a/src/events/user-status.ts +++ b/src/events/user-status.ts @@ -1,11 +1,7 @@ import { writable } from "svelte/store"; +import { events, type UserStatusPayload } from "$lib/bindings"; import { - events, - type FriendDisconnectedPayload, - type UserStatusPayload, -} from "$lib/bindings"; -import { - createMultiListenerSubscription, + createListenersSubscription, removeFromStore, setupHmrCleanup, } from "./listener-utils"; @@ -15,7 +11,7 @@ export const friendsPresenceStates = writable< >({}); export const currentPresenceState = writable(null); -const subscription = createMultiListenerSubscription(); +const subscription = createListenersSubscription(); /** * Starts listening for user status changes and friend status updates. @@ -24,21 +20,23 @@ export async function startUserStatus() { if (subscription.isListening()) return; try { - const unlistenStatus = await events.friendUserStatusChanged.listen((event) => { - const { userId, status } = event.payload; + const unlistenStatus = await events.friendUserStatusChanged.listen( + (event) => { + const { userId, status } = event.payload; - const hasValidName = - (typeof status.presenceStatus.title === "string" && - status.presenceStatus.title.trim() !== "") || - (typeof status.presenceStatus.subtitle === "string" && - status.presenceStatus.subtitle.trim() !== ""); - if (!hasValidName) return; + const hasValidName = + (typeof status.presenceStatus.title === "string" && + status.presenceStatus.title.trim() !== "") || + (typeof status.presenceStatus.subtitle === "string" && + status.presenceStatus.subtitle.trim() !== ""); + if (!hasValidName) return; - friendsPresenceStates.update((current) => ({ - ...current, - [userId]: status, - })); - }); + friendsPresenceStates.update((current) => ({ + ...current, + [userId]: status, + })); + }, + ); subscription.addUnlisten(unlistenStatus); const unlistenUserStatusChanged = await events.userStatusChanged.listen(