@@ -41,6 +41,7 @@ class Collector {
4141 this . appManaging = appManaging ;
4242 this . #streamData = streamData ;
4343 this . #cache = {
44+ initialized : false ,
4445 invites : { } ,
4546 invitesInitialized : false ,
4647 invitesInitializing : false
@@ -71,9 +72,11 @@ class Collector {
7172 /**
7273 * Fetch online data
7374 */
74- async init ( ) {
75- await this . checkStreamStructure ( ) ;
76- await this . #getStatus( ) ;
75+ async init ( forceRefresh = false ) {
76+ if ( ! forceRefresh && this . #cache. initialized ) return ;
77+ await this . checkStreamStructure ( forceRefresh ) ;
78+ await this . #getStatus( forceRefresh ) ;
79+ this . #cache. initialized = true ;
7780 }
7881
7982 /**
@@ -149,15 +152,30 @@ class Collector {
149152 return this . #cache. invites [ key ] ;
150153 }
151154
155+ /**
156+ * Retrieve an invite by its key
157+ */
158+ async getInviteByKey ( key ) {
159+ await this . init ( ) ; // do not forceRefresh on Init();
160+ await this . #initInvites( ) ;
161+ return this . #cache. invites [ key ] ;
162+ }
163+
152164 /**
153165 * Retreive all invites
154166 * @param {boolean } [forceRefresh]
155167 * @returns {Array<CollectorInvite> }
156168 */
157169 async getInvites ( forceRefresh = false ) {
170+ await this . init ( ) ; // do not forceRefresh on Init();
171+ await this . #initInvites( forceRefresh ) ;
172+ return Object . values ( this . #cache. invites ) ;
173+ }
174+
175+ async #initInvites ( forceRefresh ) {
158176 while ( this . #cache. invitesInitializing ) ( await new Promise ( ( resolve ) => { setTimeout ( resolve , 100 ) ; } ) ) ;
159177 this . #cache. invitesInitializing = true ;
160- if ( ! forceRefresh && this . #cache. invitesInitialized ) return Object . values ( this . #cache . invites ) ;
178+ if ( ! forceRefresh && this . #cache. invitesInitialized ) return ;
161179 const queryParams = { types : [ 'invite/collector-v1' ] , streams : [ this . streamId ] , fromTime : 0 , toTime : 8640000000000000 , limit : 10000 } ;
162180 try {
163181 await this . appManaging . connection . getEventsStreamed ( queryParams , ( eventData ) => {
@@ -170,7 +188,6 @@ class Collector {
170188 }
171189 this . #cache. invitesInitialized = true ;
172190 this . #cache. invitesInitializing = false ;
173- return Object . values ( this . #cache. invites ) ;
174191 }
175192
176193 async checkInbox ( ) {
0 commit comments