consolidated event subscription listeners into one
This commit is contained in:
@@ -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<UserData | null>(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);
|
||||
|
||||
@@ -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<CursorPositions>({
|
||||
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);
|
||||
|
||||
@@ -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<Record<string, CursorPositions>>(
|
||||
);
|
||||
export const friendsActiveDolls = writable<Record<string, DollDto | null>>({});
|
||||
|
||||
const subscription = createMultiListenerSubscription();
|
||||
const subscription = createListenersSubscription();
|
||||
|
||||
let friendCursorState: Record<string, FriendCursorData> = {};
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<Map<string, InteractionPayloadDto>>(
|
||||
new Map(),
|
||||
);
|
||||
export const receivedInteractions = writable<
|
||||
Map<string, InteractionPayloadDto>
|
||||
>(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(
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<boolean>(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);
|
||||
|
||||
@@ -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<UserStatusPayload | null>(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(
|
||||
|
||||
Reference in New Issue
Block a user