Skip to content

Commit fd05c79

Browse files
authored
Merge pull request #2200 from cardstack/event-processing-restartability
Update event loading logic in RoomResource to work better with restartable tasks
2 parents 4ecd379 + b9fe465 commit fd05c79

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

packages/host/app/resources/room.ts

+23-25
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,29 @@ export class RoomResource extends Resource<Args> {
103103
if (!memberIds || !memberIds.includes(this.matrixService.aiBotUserId)) {
104104
return;
105105
}
106-
await this.loadFromEvents(roomId);
106+
107+
let index = this._messageCache.size;
108+
// This is brought up to this level so if the
109+
// load task is rerun we can stop processing
110+
for (let event of this.sortedEvents) {
111+
switch (event.type) {
112+
case 'm.room.member':
113+
await this.loadRoomMemberEvent(roomId, event);
114+
break;
115+
case 'm.room.message':
116+
await this.loadRoomMessage({ roomId, event, index });
117+
break;
118+
case APP_BOXEL_COMMAND_RESULT_EVENT_TYPE:
119+
this.updateMessageCommandResult({ roomId, event, index });
120+
break;
121+
case 'm.room.create':
122+
await this.loadRoomCreateEvent(event);
123+
break;
124+
case 'm.room.name':
125+
await this.loadRoomNameEvent(event);
126+
break;
127+
}
128+
}
107129
} catch (e) {
108130
throw new Error(`Error loading room ${e}`);
109131
}
@@ -280,30 +302,6 @@ export class RoomResource extends Resource<Args> {
280302
}
281303
});
282304

283-
private async loadFromEvents(roomId: string) {
284-
let index = this._messageCache.size;
285-
286-
for (let event of this.sortedEvents) {
287-
switch (event.type) {
288-
case 'm.room.member':
289-
await this.loadRoomMemberEvent(roomId, event);
290-
break;
291-
case 'm.room.message':
292-
this.loadRoomMessage({ roomId, event, index });
293-
break;
294-
case APP_BOXEL_COMMAND_RESULT_EVENT_TYPE:
295-
this.updateMessageCommandResult({ roomId, event, index });
296-
break;
297-
case 'm.room.create':
298-
await this.loadRoomCreateEvent(event);
299-
break;
300-
case 'm.room.name':
301-
await this.loadRoomNameEvent(event);
302-
break;
303-
}
304-
}
305-
}
306-
307305
private async loadRoomMemberEvent(
308306
roomId: string,
309307
event: InviteEvent | JoinEvent | LeaveEvent,

0 commit comments

Comments
 (0)