Skip to content

Commit

Permalink
refactor(spa): expand the BATCH event
Browse files Browse the repository at this point in the history
  • Loading branch information
uonr committed Sep 10, 2024
1 parent 1ea14b5 commit bcfd058
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
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

0 comments on commit bcfd058

Please sign in to comment.