Skip to content

Commit 3d257b9

Browse files
committed
Refactor skill management in RoomResource to improve event handling and caching
1 parent 4b4bce4 commit 3d257b9

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

packages/host/app/resources/room.ts

+47-46
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ import type CardService from '../services/card-service';
4848
import type CommandService from '../services/command-service';
4949
import type MatrixService from '../services/matrix-service';
5050

51-
interface SkillId {
52-
skillCardId: string;
53-
skillEventId: string;
54-
isActive: boolean;
55-
}
56-
5751
interface Args {
5852
named: {
5953
roomId: string | undefined;
@@ -64,6 +58,8 @@ interface Args {
6458
export class RoomResource extends Resource<Args> {
6559
private _messageCache: TrackedMap<string, Message> = new TrackedMap();
6660
private _skillCardsCache: TrackedMap<string, SkillCard> = new TrackedMap();
61+
private _skillEventIdToCardIdCache: TrackedMap<string, string> =
62+
new TrackedMap();
6763
private _nameEventsCache: TrackedMap<string, RoomNameEvent> =
6864
new TrackedMap();
6965
@tracked private _createEvent: RoomCreateEvent | undefined;
@@ -196,61 +192,38 @@ export class RoomResource extends Resource<Args> {
196192
return this.events.sort((a, b) => a.origin_server_ts - b.origin_server_ts);
197193
}
198194

199-
@cached
200-
get skillIds(): SkillId[] {
195+
private get allSkillEventIds(): Set<string> {
201196
let skillsConfig = this.matrixRoom?.skillsConfig;
202197
if (!skillsConfig) {
203-
return [];
198+
return new Set();
204199
}
205-
let result: SkillId[] = [];
206-
for (let eventId of [
200+
return new Set([
207201
...skillsConfig.enabledEventIds,
208202
...skillsConfig.disabledEventIds,
209-
]) {
210-
let cardDoc;
211-
try {
212-
cardDoc = this.serializedCardFromFragments(eventId);
213-
} catch {
214-
// the skill card fragments might not be loaded yet
203+
]);
204+
}
205+
206+
get skills(): Skill[] {
207+
let result: Skill[] = [];
208+
for (let skillEventId of this.allSkillEventIds) {
209+
let cardId = this._skillEventIdToCardIdCache.get(skillEventId);
210+
if (!cardId) {
215211
continue;
216212
}
217-
if (!cardDoc.data.id) {
213+
let skillCard = this._skillCardsCache.get(cardId);
214+
if (!skillCard) {
218215
continue;
219216
}
220-
let cardId = cardDoc.data.id;
221-
if (!this._skillCardsCache.has(cardId)) {
222-
this.cardService
223-
.createFromSerialized(cardDoc.data, cardDoc)
224-
.then((skillsCard) => {
225-
this._skillCardsCache.set(cardId, skillsCard as SkillCard);
226-
});
227-
}
228217
result.push({
229-
skillCardId: cardDoc.data.id,
230-
skillEventId: eventId,
231-
isActive: skillsConfig.enabledEventIds.includes(eventId),
218+
card: skillCard,
219+
skillEventId,
220+
isActive:
221+
this.matrixRoom?.skillsConfig.enabledEventIds.includes(skillEventId),
232222
});
233223
}
234224
return result;
235225
}
236226

237-
@cached
238-
get skills(): Skill[] {
239-
return this.skillIds
240-
.map(({ skillCardId, skillEventId, isActive }) => {
241-
let card = this._skillCardsCache.get(skillCardId);
242-
if (card) {
243-
return {
244-
card,
245-
skillEventId,
246-
isActive,
247-
};
248-
}
249-
return null;
250-
})
251-
.filter(Boolean) as Skill[];
252-
}
253-
254227
@cached
255228
get created() {
256229
if (this._createEvent) {
@@ -348,6 +321,34 @@ export class RoomResource extends Resource<Args> {
348321
) {
349322
let eventId = event.event_id;
350323
this._fragmentCache.set(eventId, event.content);
324+
325+
if (this.isSkillEventId(eventId)) {
326+
await this.ensureSkillCardCached(eventId);
327+
}
328+
}
329+
330+
private isSkillEventId(eventId: string) {
331+
return this.allSkillEventIds.has(eventId);
332+
}
333+
334+
private async ensureSkillCardCached(eventId: string) {
335+
if (this._skillEventIdToCardIdCache.has(eventId)) {
336+
return;
337+
}
338+
let cardDoc = this.serializedCardFromFragments(eventId);
339+
if (!cardDoc.data.id) {
340+
console.warn(
341+
`No card id found for skill event id ${eventId}, this should not happen, this can happen if you add a skill card to a room without saving it, and without giving it an ID`,
342+
);
343+
return;
344+
}
345+
let cardId = cardDoc.data.id;
346+
let skillCard = (await this.cardService.createFromSerialized(
347+
cardDoc.data,
348+
cardDoc,
349+
)) as SkillCard;
350+
this._skillCardsCache.set(cardId, skillCard);
351+
this._skillEventIdToCardIdCache.set(eventId, cardId);
351352
}
352353

353354
private loadRoomMessage({

0 commit comments

Comments
 (0)