@@ -48,12 +48,6 @@ import type CardService from '../services/card-service';
48
48
import type CommandService from '../services/command-service' ;
49
49
import type MatrixService from '../services/matrix-service' ;
50
50
51
- interface SkillId {
52
- skillCardId : string ;
53
- skillEventId : string ;
54
- isActive : boolean ;
55
- }
56
-
57
51
interface Args {
58
52
named : {
59
53
roomId : string | undefined ;
@@ -64,6 +58,8 @@ interface Args {
64
58
export class RoomResource extends Resource < Args > {
65
59
private _messageCache : TrackedMap < string , Message > = new TrackedMap ( ) ;
66
60
private _skillCardsCache : TrackedMap < string , SkillCard > = new TrackedMap ( ) ;
61
+ private _skillEventIdToCardIdCache : TrackedMap < string , string > =
62
+ new TrackedMap ( ) ;
67
63
private _nameEventsCache : TrackedMap < string , RoomNameEvent > =
68
64
new TrackedMap ( ) ;
69
65
@tracked private _createEvent : RoomCreateEvent | undefined ;
@@ -196,61 +192,38 @@ export class RoomResource extends Resource<Args> {
196
192
return this . events . sort ( ( a , b ) => a . origin_server_ts - b . origin_server_ts ) ;
197
193
}
198
194
199
- @cached
200
- get skillIds ( ) : SkillId [ ] {
195
+ private get allSkillEventIds ( ) : Set < string > {
201
196
let skillsConfig = this . matrixRoom ?. skillsConfig ;
202
197
if ( ! skillsConfig ) {
203
- return [ ] ;
198
+ return new Set ( ) ;
204
199
}
205
- let result : SkillId [ ] = [ ] ;
206
- for ( let eventId of [
200
+ return new Set ( [
207
201
...skillsConfig . enabledEventIds ,
208
202
...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 ) {
215
211
continue ;
216
212
}
217
- if ( ! cardDoc . data . id ) {
213
+ let skillCard = this . _skillCardsCache . get ( cardId ) ;
214
+ if ( ! skillCard ) {
218
215
continue ;
219
216
}
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
- }
228
217
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 ) ,
232
222
} ) ;
233
223
}
234
224
return result ;
235
225
}
236
226
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
-
254
227
@cached
255
228
get created ( ) {
256
229
if ( this . _createEvent ) {
@@ -348,6 +321,34 @@ export class RoomResource extends Resource<Args> {
348
321
) {
349
322
let eventId = event . event_id ;
350
323
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 ) ;
351
352
}
352
353
353
354
private loadRoomMessage ( {
0 commit comments