refactored frontend events for code conciseness

This commit is contained in:
2026-03-08 16:15:32 +08:00
parent 291e0311e8
commit 23c778a0bb
7 changed files with 139 additions and 236 deletions

View File

@@ -5,11 +5,7 @@ import {
type DollDto,
type OutgoingFriendCursorPayload,
} from "$lib/bindings";
import {
createListenersSubscription,
removeFromStore,
setupHmrCleanup,
} from "./listener-utils";
import { createEventSource, removeFromStore } from "./listener-utils";
type FriendCursorData = {
position: CursorPositions;
@@ -21,87 +17,65 @@ export const friendsCursorPositions = writable<Record<string, CursorPositions>>(
);
export const friendsActiveDolls = writable<Record<string, DollDto | null>>({});
const subscription = createListenersSubscription();
export const {
start: startFriendCursorTracking,
stop: stopFriendCursorTracking,
} = createEventSource(async (addEventListener) => {
let friendCursorState: Record<string, FriendCursorData> = {};
addEventListener(
await events.friendCursorPositionUpdated.listen((event) => {
const data: OutgoingFriendCursorPayload = event.payload;
let friendCursorState: Record<string, FriendCursorData> = {};
friendCursorState[data.userId] = {
position: data.position,
lastUpdated: Date.now(),
};
/**
* Starts listening for friend cursor position and active doll changes.
* Also handles friend disconnection events.
*/
export async function startFriendCursorTracking() {
if (subscription.isListening()) return;
try {
// TODO: Add initial sync for existing friends' cursors and dolls if needed
const unlistenFriendCursor =
await events.friendCursorPositionUpdated.listen((event) => {
const data: OutgoingFriendCursorPayload = event.payload;
friendCursorState[data.userId] = {
position: data.position,
lastUpdated: Date.now(),
friendsCursorPositions.update((current) => {
return {
...current,
[data.userId]: data.position,
};
friendsCursorPositions.update((current) => {
return {
...current,
[data.userId]: data.position,
};
});
});
subscription.addUnlisten(unlistenFriendCursor);
}),
);
const unlistenFriendDisconnected = await events.friendDisconnected.listen(
(event) => {
const data = event.payload;
addEventListener(
await events.friendDisconnected.listen((event) => {
const data = event.payload;
if (friendCursorState[data.userId]) {
delete friendCursorState[data.userId];
}
if (friendCursorState[data.userId]) {
delete friendCursorState[data.userId];
}
friendsCursorPositions.update((current) =>
removeFromStore(current, data.userId),
);
}),
);
addEventListener(
await events.friendActiveDollChanged.listen((event) => {
const payload = event.payload;
if (!payload.doll) {
friendsActiveDolls.update((current) => {
const next = { ...current };
next[payload.friendId] = null;
return next;
});
friendsCursorPositions.update((current) =>
removeFromStore(current, data.userId),
removeFromStore(current, payload.friendId),
);
},
);
subscription.addUnlisten(unlistenFriendDisconnected);
const unlistenFriendActiveDollChanged =
await events.friendActiveDollChanged.listen((event) => {
const payload = event.payload;
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,
};
});
}
});
subscription.addUnlisten(unlistenFriendActiveDollChanged);
subscription.setListening(true);
} catch (err) {
console.error("Failed to initialize friend cursor tracking:", err);
throw err;
}
}
export function stopFriendCursorTracking() {
subscription.stop();
}
setupHmrCleanup(stopFriendCursorTracking);
} else {
friendsActiveDolls.update((current) => {
return {
...current,
[payload.friendId]: payload.doll,
};
});
}
}),
);
});