Skip to content

Commit 55e2213

Browse files
committed
refactor: enhance state retrieval logic in StateService by consolidating state fetching and improving error handling for better clarity
1 parent ce3bb5a commit 55e2213

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

packages/federation-sdk/src/services/state.service.ts

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -250,36 +250,44 @@ export class StateService {
250250
// Retrieves a snapshot of a room's state at a given event, in the form of event IDs. This performs the same function as calling /state/{roomId}, however this returns just the event IDs rather than the full events.
251251
const { stateId } = event;
252252

253-
const { delta: lastStateDelta, prevStateIds = [] } =
254-
(await this.stateRepository.getStateById(stateId)) ?? {};
255-
256-
this.logger.debug({ delta: lastStateDelta, prevStateIds }, 'last state');
253+
const storedState = await this.stateRepository.getStateById(stateId);
257254

258-
if (!lastStateDelta) {
255+
if (!storedState) {
259256
this.logger.error(eventId, 'last state delta not found');
260257
throw new Error(`State at event ${eventId} not found`);
261258
}
262259

260+
this.logger.debug(
261+
{ delta: storedState?.delta, prevStateIds: storedState.prevStateIds },
262+
'last state',
263+
);
264+
263265
const state = new Map<StateMapKey, PersistentEventBase>();
264266

265-
if (prevStateIds.length === 0) {
267+
// TODO: not sure which case is this
268+
if (storedState.prevStateIds.length === 0) {
266269
const previous = await this.eventRepository.findById(
267-
lastStateDelta.eventId,
270+
storedState.delta.eventId,
268271
);
269272
if (!previous) {
270-
throw new Error(`Event ${lastStateDelta.eventId} not found`);
273+
throw new Error(`Event ${eventId} not found`);
271274
}
272275

273276
state.set(
274-
lastStateDelta.identifier,
277+
storedState.delta.identifier,
275278
PersistentEventFactory.createFromRawEvent(previous.event, roomVersion),
276279
);
277280

278281
return state;
279282
}
280283

284+
const eventsToFetch = [
285+
includeEvent && eventId,
286+
...storedState.prevStateIds,
287+
].filter(Boolean) as string[];
288+
281289
const stateMappings = await this.stateRepository
282-
.getStateMappingsByStateIdsOrdered(prevStateIds)
290+
.getStateMappingsByStateIdsOrdered(eventsToFetch)
283291
.toArray();
284292

285293
for await (const { delta } of stateMappings) {
@@ -295,26 +303,6 @@ export class StateService {
295303
);
296304
}
297305

298-
if (!includeEvent) {
299-
return state;
300-
}
301-
302-
this.logger.debug({ eventId }, 'including event in state');
303-
304-
// update the last state
305-
const { identifier: lastStateKey, eventId: lastStateEventId } =
306-
lastStateDelta;
307-
308-
const lastEvent = await this.eventRepository.findById(lastStateEventId);
309-
if (!lastEvent) {
310-
throw new Error(`Event ${lastStateEventId} not found`);
311-
}
312-
313-
state.set(
314-
lastStateKey,
315-
PersistentEventFactory.createFromRawEvent(lastEvent.event, roomVersion),
316-
);
317-
318306
return state;
319307
}
320308

0 commit comments

Comments
 (0)