Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection #650

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions apps/spa/hooks/useConnectionEffect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isUuid } from '@boluo/utils';
import { PING, PONG } from '../const';
import { chatAtom, type ChatDispatch, connectionStateAtom } from '../state/chat.atoms';
import { type ConnectionState } from '../state/connection.reducer';
import { recordError } from '../error';

let lastPongTime = Date.now();
const RELOAD_TIMEOUT = 1000 * 60 * 30;
Expand Down Expand Up @@ -70,11 +71,23 @@ const connect = (
return;
}
if (!isServerEvent(event)) return;
dispatch({ type: 'eventFromServer', payload: event });
// The `event.id.node` field is currently unused, so we can ignore it.
if (event.id.timestamp < after.timestamp) return;
if (event.id.timestamp === after.timestamp && event.id.seq <= after.seq) return;
onEvent(event);
let eventList: ServerEvent[];
if (event.body.type === 'BATCH') {
eventList = event.body.encodedEvents.flatMap((encodedEvent) => {
try {
return [JSON.parse(encodedEvent) as ServerEvent];
} catch {
recordError('Failed to parse event', { event: encodedEvent });
return [];
}
});
} else {
eventList = [event];
}
for (const event of eventList) {
dispatch({ type: 'eventFromServer', payload: event });
onEvent(event);
}
};
return newConnection;
};
Expand Down
25 changes: 3 additions & 22 deletions apps/spa/state/chat.reducer.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { EventId, ServerEvent } from '@boluo/api';
import type { EventId } from '@boluo/api';
import type { Reducer } from 'react';
import { eventIdCompare } from '../sort';
import type { ChannelState } from './channel.reducer';
import { channelReducer, makeInitialChannelState } from './channel.reducer';
import { type ChatAction, type ChatActionUnion, eventToChatAction } from './chat.actions';
import type { ConnectionState } from './connection.reducer';
import { connectionReducer, initialConnectionState } from './connection.reducer';
import { recordError } from '../error';

export interface ChatReducerContext {
spaceId: string;
Expand Down Expand Up @@ -105,26 +104,8 @@ const handleEventFromServer = (
{ payload: event }: ChatAction<'eventFromServer'>,
): ChatSpaceState => {
if (event.body.type === 'BATCH') {
const { encodedEvents } = event.body;
const events: Array<ServerEvent | null> = encodedEvents.map((encodedEvent) => {
try {
return JSON.parse(encodedEvent) as ServerEvent;
} catch {
recordError('Failed to parse event', { event: encodedEvent });
return null;
}
});
let nextState = state;
let lastEventId = state.lastEventId;
for (const event of events) {
if (event === null) continue;
if (eventIdCompare(event.id, state.lastEventId) <= 0) continue;
const chatAction = eventToChatAction(event);
if (chatAction === null) continue;
nextState = chatReducer(nextState, chatAction);
lastEventId = event.id;
}
return { ...nextState, lastEventId };
// We do not handle batch events here
return state;
}
if (eventIdCompare(event.id, state.lastEventId) <= 0) return state;
const lastEventId = event.id;
Expand Down