@@ -62,7 +62,8 @@ export const initBikeTagStore = () => {
6262
6363export const useBikeTagStore = defineStore ( BikeTagDefaults . store , {
6464 state : ( ) : BikeTagStoreState => ( {
65- dataLoaded : false ,
65+ fetchingData : false ,
66+ dataFetched : false ,
6667 gameName,
6768 gameNameProper : gameName ?. length ? gameName [ 0 ] . toUpperCase ( ) + gameName . slice ( 1 ) : '' ,
6869 game : { } as Game ,
@@ -83,6 +84,22 @@ export const useBikeTagStore = defineStore(BikeTagDefaults.store, {
8384 } ) ,
8485
8586 actions : {
87+ async isReady ( ) {
88+ if ( this . dataFetched ) {
89+ return Promise . resolve ( ) // Data is already loaded, resolve immediately
90+ }
91+
92+ return new Promise ( ( resolve ) => {
93+ const checkIsDataLoaded = ( ) => {
94+ if ( this . dataFetched ) {
95+ clearInterval ( intervalId ) // Stop checking when isDataLoaded becomes true
96+ resolve ( true )
97+ }
98+ }
99+ const intervalId = setInterval ( checkIsDataLoaded , 100 ) // Check every 100 milliseconds (adjust as needed)
100+ checkIsDataLoaded ( ) // Check immediately
101+ } )
102+ } ,
86103 // eslint-disable-next-line no-empty-pattern
87104 async getRegionPolygon ( region : any ) {
88105 try {
@@ -162,7 +179,7 @@ export const useBikeTagStore = defineStore(BikeTagDefaults.store, {
162179 async setGame ( newGameName ?: string ) {
163180 newGameName = newGameName ?? this . gameName
164181 if ( this . game ?. name !== newGameName || ! this . game ?. mainhash ) {
165- this . dataLoaded = false
182+ this . fetchingData = false
166183 return client . getGame ( { game : newGameName } , biketagGameOpts as any ) . then ( async ( r ) => {
167184 if ( r . success ) {
168185 const game = r . data as Game
@@ -204,8 +221,65 @@ export const useBikeTagStore = defineStore(BikeTagDefaults.store, {
204221 this . credentialsFetched = true
205222 }
206223 } ,
224+ async FetchAllData (
225+ opts : {
226+ currentBikeTagSync ?: boolean
227+ skipCurrentBikeTag ?: boolean
228+ tagsSync ?: boolean
229+ skipTags ?: boolean
230+ playersSync ?: boolean
231+ skipPlayers ?: boolean
232+ leaderboardSync ?: boolean
233+ skipLeaderboard ?: boolean
234+ credentialsSync ?: boolean
235+ skipCredentials ?: boolean
236+ queuedTagsSync ?: boolean
237+ skipQueuedTags ?: boolean
238+ allGamesSync ?: boolean
239+ skipAllGames ?: boolean
240+ } = { } ,
241+ ) {
242+ const initResults : any [ ] = [ ]
243+ this . fetchingData = true
244+
245+ if ( ! opts . skipCurrentBikeTag ) {
246+ if ( opts . currentBikeTagSync ) initResults . push ( await this . fetchCurrentBikeTag ( ) )
247+ else initResults . push ( this . fetchCurrentBikeTag ( ) )
248+ }
249+ if ( ! opts . skipTags ) {
250+ if ( opts . tagsSync ) initResults . push ( await this . fetchTags ( ) )
251+ else initResults . push ( this . fetchTags ( ) )
252+ }
253+ if ( ! opts . skipPlayers ) {
254+ if ( opts . playersSync ) initResults . push ( await this . fetchPlayers ( ) )
255+ else initResults . push ( this . fetchPlayers ( ) )
256+ }
257+ if ( ! opts . skipLeaderboard ) {
258+ if ( opts . leaderboardSync ) initResults . push ( await this . fetchLeaderboard ( ) )
259+ else initResults . push ( this . fetchLeaderboard ( ) )
260+ }
261+ if ( ! opts . skipCredentials ) {
262+ if ( opts . credentialsSync ) initResults . push ( await this . fetchCredentials ( ) )
263+ else initResults . push ( await this . fetchCredentials ( ) )
264+ }
265+ if ( ! opts . skipQueuedTags ) {
266+ if ( opts . queuedTagsSync ) initResults . push ( await this . fetchQueuedTags ( ) )
267+ else initResults . push ( this . fetchQueuedTags ( ) )
268+ }
269+ if ( ! opts . skipAllGames ) {
270+ if ( opts . allGamesSync ) initResults . push ( await this . fetchAllGames ( ) )
271+ else initResults . push ( this . fetchAllGames ( ) )
272+ }
273+
274+ Promise . allSettled ( initResults ) . then ( ( results ) => {
275+ this . dataFetched = true
276+ this . fetchingData = false
277+ } )
278+
279+ return initResults
280+ } ,
207281 fetchAllGames ( cached = true ) {
208- this . dataLoaded = false
282+ this . fetchingData = false
209283 const biketagClient = new BikeTagClient ( { ...biketagClientOpts , game : undefined , cached } )
210284 return biketagClient
211285 . getGame (
@@ -572,7 +646,7 @@ export const useBikeTagStore = defineStore(BikeTagDefaults.store, {
572646 SET_GAME ( game : any ) {
573647 const oldState = this . game
574648 this . game = game
575- this . dataLoaded = true
649+ this . fetchingData = true
576650
577651 if ( oldState ?. name !== game ?. name ) {
578652 debug ( `${ BikeTagDefaults . store } ::game` , { game } )
@@ -583,7 +657,7 @@ export const useBikeTagStore = defineStore(BikeTagDefaults.store, {
583657 SET_ALL_GAMES ( allGames : any ) {
584658 const oldState = this . allGames
585659 this . allGames = allGames
586- this . dataLoaded = true
660+ this . fetchingData = true
587661
588662 if ( oldState ?. length !== allGames ?. length ) {
589663 debug ( `${ BikeTagDefaults . store } ::allGames` , { allGames } )
@@ -849,9 +923,6 @@ export const useBikeTagStore = defineStore(BikeTagDefaults.store, {
849923 getQueuedTagState : ( state ) => {
850924 return getQueuedTagState ( state . playerTag )
851925 } ,
852- getDataLoaded ( state ) {
853- return state . dataLoaded
854- } ,
855926 getGame ( state ) {
856927 return state . game
857928 } ,
0 commit comments