@@ -41,16 +41,18 @@ const loadedLocations = new Map<
4141 * it also caches the data, so if the same interval is requested again, it will not load it again.
4242 */
4343class LocationObservable {
44- constructor ( query : VehicleLocationQuery ) {
45- this . #loadData( query )
44+ static create ( query : VehicleLocationQuery ) {
45+ const instance = new LocationObservable ( )
46+ void instance . #loadData( query )
47+ return instance
4648 }
4749
4850 data : SiriVehicleLocationWithRelatedPydanticModel [ ] = [ ]
4951 loading = true
5052
5153 async #loadData( querys : VehicleLocationQuery ) {
5254 let offset = 0
53- for ( let i = 1 ; this . loading ; i ++ ) {
55+ while ( this . loading ) {
5456 const data = await fetchWithQueue ( querys , offset )
5557 if ( ! data || data . length === 0 ) {
5658 this . loading = false
@@ -139,7 +141,10 @@ function getLocations(
139141) {
140142 const key = `${ formatTime ( from ) } -${ formatTime ( to ) } -${ operatorRef } -${ lineRef } -${ vehicleRef } `
141143 if ( ! loadedLocations . has ( key ) ) {
142- loadedLocations . set ( key , new LocationObservable ( { from, to, lineRef, vehicleRef, operatorRef } ) )
144+ loadedLocations . set (
145+ key ,
146+ LocationObservable . create ( { from, to, lineRef, vehicleRef, operatorRef } ) ,
147+ )
143148 }
144149 const observable = loadedLocations . get ( key ) !
145150 return observable . observe ( onUpdate )
@@ -171,6 +176,24 @@ export default function useVehicleLocations({
171176} ) {
172177 const [ locations , setLocations ] = useState < SiriVehicleLocationWithRelatedPydanticModel [ ] > ( [ ] )
173178 const [ isLoading , setIsLoading ] = useState < boolean [ ] > ( [ ] )
179+
180+ const handleFinished = ( i : number ) => {
181+ setIsLoading ( ( prev ) => {
182+ const newIsLoading = [ ...prev ]
183+ newIsLoading [ i ] = false
184+ return newIsLoading
185+ } )
186+ }
187+
188+ const handleData = ( data : SiriVehicleLocationWithRelatedPydanticModel [ ] ) => {
189+ setLocations ( ( prev ) =>
190+ uniqBy < SiriVehicleLocationWithRelatedPydanticModel > (
191+ [ ...prev , ...data ] . sort ( ( a , b ) => ( a . id || 0 ) - ( b . id || 0 ) ) ,
192+ ( loc ) => loc . id ,
193+ ) ,
194+ )
195+ }
196+
174197 useEffect ( ( ) => {
175198 if ( pause ) return
176199 const range = split ? getMinutesInRange ( from , to , split ) : [ { from, to } ]
@@ -184,18 +207,9 @@ export default function useVehicleLocations({
184207 operatorRef,
185208 onUpdate : ( data ) => {
186209 if ( 'finished' in data ) {
187- setIsLoading ( ( prev ) => {
188- const newIsLoading = [ ...prev ]
189- newIsLoading [ i ] = false
190- return newIsLoading
191- } )
210+ handleFinished ( i )
192211 } else {
193- setLocations ( ( prev ) =>
194- uniqBy < SiriVehicleLocationWithRelatedPydanticModel > (
195- [ ...prev , ...data ] . sort ( ( a , b ) => ( a . id || 0 ) - ( b . id || 0 ) ) ,
196- ( loc ) => loc . id ,
197- ) ,
198- )
212+ handleData ( data )
199213 }
200214 } ,
201215 } ) ,
@@ -208,8 +222,6 @@ export default function useVehicleLocations({
208222 } , [ from , to , lineRef , vehicleRef , split ] )
209223 return {
210224 locations,
211- isLoading : isLoading . some ( ( loading ) => loading ) ,
225+ isLoading : isLoading . some ( Boolean ) ,
212226 }
213227}
214-
215- export { }
0 commit comments