fix: User presence is not updated when leaving a document

This commit is contained in:
Tom Moor
2023-05-22 21:05:40 -04:00
parent 4e75b4029a
commit 3317bf2396
5 changed files with 47 additions and 79 deletions

View File

@@ -15,6 +15,7 @@ import useIsMounted from "~/hooks/useIsMounted";
import usePageVisibility from "~/hooks/usePageVisibility";
import useStores from "~/hooks/useStores";
import useToasts from "~/hooks/useToasts";
import { AwarenessChangeEvent } from "~/types";
import Logger from "~/utils/Logger";
import { supportsPassiveListener } from "~/utils/browser";
import { homePath } from "~/utils/routeHelpers";
@@ -30,10 +31,6 @@ export type ConnectionStatus =
| "disconnected"
| void;
type AwarenessChangeEvent = {
states: { user: { id: string }; cursor: any; scrollY: number | undefined }[];
};
type ConnectionStatusEvent = { status: ConnectionStatus };
type MessageEvent = {
@@ -108,11 +105,11 @@ function MultiplayerEditor({ onSynced, ...props }: Props, ref: any) {
history.replace(homePath());
});
provider.on("awarenessChange", ({ states }: AwarenessChangeEvent) => {
states.forEach(({ user, cursor, scrollY }) => {
if (user) {
presence.touch(documentId, user.id, !!cursor);
provider.on("awarenessChange", (event: AwarenessChangeEvent) => {
presence.updateFromAwarenessChangeEvent(documentId, event);
event.states.forEach(({ user, scrollY }) => {
if (user) {
if (scrollY !== undefined && user.id === ui.observingUserId) {
window.scrollTo({
top: scrollY * window.innerHeight,