incyclist-services 1.9.8 → 1.9.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -99,6 +99,8 @@ let ActiveRidesService = (() => {
99
99
  isStarted = false;
100
100
  routeListObserver;
101
101
  routesStats = [];
102
+ routeListSessions = new Map();
103
+ isSubscribedRouteListEvents = false;
102
104
  activityEventHandlers = {
103
105
  update: this.onActivityUpdateEvent.bind(this),
104
106
  start: this.onActivityStartEvent.bind(this),
@@ -111,6 +113,7 @@ let ActiveRidesService = (() => {
111
113
  routeList.on('opened', this.onRoutePageOpened.bind(this));
112
114
  routeList.on('closed', () => {
113
115
  delete this.routeListObserver;
116
+ this.unsubscribeRouteListEvents();
114
117
  });
115
118
  }
116
119
  init(session, maxLength = 10) {
@@ -182,7 +185,10 @@ let ActiveRidesService = (() => {
182
185
  }
183
186
  async onRoutePageOpened(observer) {
184
187
  this.routeListObserver = observer;
185
- await this.getActiveRideCounts();
188
+ if (!this.isSubscribedRouteListEvents) {
189
+ await this.getActiveRideCounts();
190
+ this.subscribeRouteListEvents();
191
+ }
186
192
  this.routeListObserver.emit('stats-update', this.routesStats);
187
193
  }
188
194
  async getActiveRideCounts() {
@@ -191,19 +197,17 @@ let ActiveRidesService = (() => {
191
197
  if (!isOnline)
192
198
  return;
193
199
  const activeRides = (await this.getApi().getAll()).filter(ar => ar.ride !== undefined && ar.ride !== null);
194
- const routes = this.getRouteList().getAllRoutes();
195
- const counts = [];
196
- routes.forEach(route => {
197
- const routeRides = activeRides.filter(ar => ar.ride?.routeHash && ar.ride?.routeHash === route.description?.routeHash) ?? [];
198
- const count = routeRides.length;
199
- counts.push({
200
- count,
201
- routeId: route.description.id,
202
- routeHash: route.description.routeHash
203
- });
200
+ this.routeListSessions = new Map();
201
+ activeRides.forEach(ar => {
202
+ const hash = ar.ride?.routeHash;
203
+ if (!hash)
204
+ return;
205
+ if (!this.routeListSessions.has(hash))
206
+ this.routeListSessions.set(hash, new Set());
207
+ this.routeListSessions.get(hash).add(ar.sessionId ?? ar.id);
204
208
  });
205
- this.routesStats = counts;
206
- return counts;
209
+ this.routesStats = this.buildRouteListStats();
210
+ return this.routesStats;
207
211
  }
208
212
  catch (err) {
209
213
  const isOnline = this.getOnlineStatusMonitoring().onlineStatus;
@@ -212,6 +216,73 @@ let ActiveRidesService = (() => {
212
216
  }
213
217
  return [];
214
218
  }
219
+ buildRouteListStats() {
220
+ const routes = this.getRouteList().getAllRoutes();
221
+ return routes.map(route => ({
222
+ count: this.routeListSessions.get(route.description.routeHash)?.size ?? 0,
223
+ routeId: route.description.id,
224
+ routeHash: route.description.routeHash
225
+ }));
226
+ }
227
+ subscribeRouteListEvents() {
228
+ if (this.isSubscribedRouteListEvents)
229
+ return;
230
+ this.getMessageQueue()?.subscribe('incyclist/activity/+/+/+', this.onRouteListActivityEvent.bind(this), 'incyclist/activity');
231
+ this.isSubscribedRouteListEvents = true;
232
+ }
233
+ unsubscribeRouteListEvents() {
234
+ if (!this.isSubscribedRouteListEvents)
235
+ return;
236
+ this.getMessageQueue()?.unsubscribe('incyclist/activity/+/+/+');
237
+ this.isSubscribedRouteListEvents = false;
238
+ }
239
+ onRouteListActivityEvent(topic) {
240
+ const keys = topic.split('/');
241
+ const session = keys[2];
242
+ const routeHash = keys[3];
243
+ const event = keys[4];
244
+ if (!routeHash || !session)
245
+ return;
246
+ if (event === 'start')
247
+ this.addRouteListSession(routeHash, session);
248
+ else if (event === 'stop')
249
+ this.removeRouteListSession(routeHash, session);
250
+ }
251
+ addRouteListSession(routeHash, session) {
252
+ if (!this.routeListSessions.has(routeHash))
253
+ this.routeListSessions.set(routeHash, new Set());
254
+ const sessions = this.routeListSessions.get(routeHash);
255
+ if (sessions.has(session))
256
+ return;
257
+ sessions.add(session);
258
+ this.emitRouteListStatUpdate(routeHash);
259
+ }
260
+ removeRouteListSession(routeHash, session) {
261
+ const sessions = this.routeListSessions.get(routeHash);
262
+ if (!sessions?.has(session))
263
+ return;
264
+ sessions.delete(session);
265
+ this.emitRouteListStatUpdate(routeHash);
266
+ }
267
+ emitRouteListStatUpdate(routeHash) {
268
+ const route = this.getRouteList().getAllRoutes().find(r => r.description.routeHash === routeHash);
269
+ if (!route)
270
+ return;
271
+ const stat = {
272
+ count: this.routeListSessions.get(routeHash)?.size ?? 0,
273
+ routeId: route.description.id,
274
+ routeHash
275
+ };
276
+ const idx = this.routesStats?.findIndex(s => s.routeHash === routeHash) ?? -1;
277
+ if (idx >= 0) {
278
+ this.routesStats[idx] = stat;
279
+ }
280
+ else {
281
+ this.routesStats ??= [];
282
+ this.routesStats.push(stat);
283
+ }
284
+ this.routeListObserver?.emit('stats-update', [stat]);
285
+ }
215
286
  start(session) {
216
287
  if (this.onlineStatusHandler || this.isOnline) {
217
288
  this.initList().catch(err => { this.logError(err, 'start'); });
@@ -600,6 +671,7 @@ let ActiveRidesService = (() => {
600
671
  onDisconnect() {
601
672
  this.getMessageQueue().onDisconnect();
602
673
  this.isSubscribed = false;
674
+ this.isSubscribedRouteListEvents = false;
603
675
  }
604
676
  async onConnect(props) {
605
677
  const { initial = false } = props ?? {};
@@ -607,6 +679,8 @@ let ActiveRidesService = (() => {
607
679
  this.getMessageQueue().onConnect();
608
680
  if (!initial && this.current && !this.isSubscribed)
609
681
  this.subscribeActivityEvents().catch(err => { this.logError(err, 'onConnect'); });
682
+ if (!initial && this.routeListObserver && !this.isSubscribedRouteListEvents)
683
+ this.subscribeRouteListEvents();
610
684
  }
611
685
  async subscribeActivityEvents() {
612
686
  const hash = this.current?.ride.routeHash;
@@ -625,8 +699,11 @@ let ActiveRidesService = (() => {
625
699
  onActivityEvent(topic, payload) {
626
700
  const keys = topic.split('/');
627
701
  const session = keys[2];
702
+ const routeHash = keys[3];
628
703
  if (session === this.current.sessionId)
629
704
  return;
705
+ if (routeHash !== this.current?.ride?.routeHash)
706
+ return;
630
707
  const event = keys[4];
631
708
  const handler = this.activityEventHandlers[event];
632
709
  if (handler)
@@ -143,13 +143,13 @@ let RoutesApiLoader = (() => {
143
143
  await this.processRouteFromApi(p.value[j], loadDetailRequired);
144
144
  }
145
145
  }
146
- this.loadObserver.emit('loaded');
146
+ this.loadObserver?.emit('loaded');
147
147
  if (loadDetailRequired.length > 0) {
148
148
  await this.loadDetails(loadDetailRequired, true);
149
149
  }
150
- this.loadObserver.emit('done');
150
+ this.loadObserver?.emit('done');
151
151
  await (0, sleep_1.sleep)(1);
152
- this.loadObserver.stop();
152
+ this.loadObserver?.stop();
153
153
  delete this.loadObserver;
154
154
  }
155
155
  async processRouteFromApi(descr, items) {
@@ -488,6 +488,7 @@ let UserInterfaceServcie = (() => {
488
488
  const freeRide = (0, routes_1.useFreeRideService)();
489
489
  const workouts = (0, workouts_1.useWorkoutList)();
490
490
  const activities = (0, activities_1.useActivityList)();
491
+ (0, activities_1.useActiveRides)();
491
492
  const promises = [
492
493
  routes.preload().wait(),
493
494
  freeRide.init(),
@@ -96,6 +96,8 @@ let ActiveRidesService = (() => {
96
96
  isStarted = false;
97
97
  routeListObserver;
98
98
  routesStats = [];
99
+ routeListSessions = new Map();
100
+ isSubscribedRouteListEvents = false;
99
101
  activityEventHandlers = {
100
102
  update: this.onActivityUpdateEvent.bind(this),
101
103
  start: this.onActivityStartEvent.bind(this),
@@ -108,6 +110,7 @@ let ActiveRidesService = (() => {
108
110
  routeList.on('opened', this.onRoutePageOpened.bind(this));
109
111
  routeList.on('closed', () => {
110
112
  delete this.routeListObserver;
113
+ this.unsubscribeRouteListEvents();
111
114
  });
112
115
  }
113
116
  init(session, maxLength = 10) {
@@ -179,7 +182,10 @@ let ActiveRidesService = (() => {
179
182
  }
180
183
  async onRoutePageOpened(observer) {
181
184
  this.routeListObserver = observer;
182
- await this.getActiveRideCounts();
185
+ if (!this.isSubscribedRouteListEvents) {
186
+ await this.getActiveRideCounts();
187
+ this.subscribeRouteListEvents();
188
+ }
183
189
  this.routeListObserver.emit('stats-update', this.routesStats);
184
190
  }
185
191
  async getActiveRideCounts() {
@@ -188,19 +194,17 @@ let ActiveRidesService = (() => {
188
194
  if (!isOnline)
189
195
  return;
190
196
  const activeRides = (await this.getApi().getAll()).filter(ar => ar.ride !== undefined && ar.ride !== null);
191
- const routes = this.getRouteList().getAllRoutes();
192
- const counts = [];
193
- routes.forEach(route => {
194
- const routeRides = activeRides.filter(ar => ar.ride?.routeHash && ar.ride?.routeHash === route.description?.routeHash) ?? [];
195
- const count = routeRides.length;
196
- counts.push({
197
- count,
198
- routeId: route.description.id,
199
- routeHash: route.description.routeHash
200
- });
197
+ this.routeListSessions = new Map();
198
+ activeRides.forEach(ar => {
199
+ const hash = ar.ride?.routeHash;
200
+ if (!hash)
201
+ return;
202
+ if (!this.routeListSessions.has(hash))
203
+ this.routeListSessions.set(hash, new Set());
204
+ this.routeListSessions.get(hash).add(ar.sessionId ?? ar.id);
201
205
  });
202
- this.routesStats = counts;
203
- return counts;
206
+ this.routesStats = this.buildRouteListStats();
207
+ return this.routesStats;
204
208
  }
205
209
  catch (err) {
206
210
  const isOnline = this.getOnlineStatusMonitoring().onlineStatus;
@@ -209,6 +213,73 @@ let ActiveRidesService = (() => {
209
213
  }
210
214
  return [];
211
215
  }
216
+ buildRouteListStats() {
217
+ const routes = this.getRouteList().getAllRoutes();
218
+ return routes.map(route => ({
219
+ count: this.routeListSessions.get(route.description.routeHash)?.size ?? 0,
220
+ routeId: route.description.id,
221
+ routeHash: route.description.routeHash
222
+ }));
223
+ }
224
+ subscribeRouteListEvents() {
225
+ if (this.isSubscribedRouteListEvents)
226
+ return;
227
+ this.getMessageQueue()?.subscribe('incyclist/activity/+/+/+', this.onRouteListActivityEvent.bind(this), 'incyclist/activity');
228
+ this.isSubscribedRouteListEvents = true;
229
+ }
230
+ unsubscribeRouteListEvents() {
231
+ if (!this.isSubscribedRouteListEvents)
232
+ return;
233
+ this.getMessageQueue()?.unsubscribe('incyclist/activity/+/+/+');
234
+ this.isSubscribedRouteListEvents = false;
235
+ }
236
+ onRouteListActivityEvent(topic) {
237
+ const keys = topic.split('/');
238
+ const session = keys[2];
239
+ const routeHash = keys[3];
240
+ const event = keys[4];
241
+ if (!routeHash || !session)
242
+ return;
243
+ if (event === 'start')
244
+ this.addRouteListSession(routeHash, session);
245
+ else if (event === 'stop')
246
+ this.removeRouteListSession(routeHash, session);
247
+ }
248
+ addRouteListSession(routeHash, session) {
249
+ if (!this.routeListSessions.has(routeHash))
250
+ this.routeListSessions.set(routeHash, new Set());
251
+ const sessions = this.routeListSessions.get(routeHash);
252
+ if (sessions.has(session))
253
+ return;
254
+ sessions.add(session);
255
+ this.emitRouteListStatUpdate(routeHash);
256
+ }
257
+ removeRouteListSession(routeHash, session) {
258
+ const sessions = this.routeListSessions.get(routeHash);
259
+ if (!sessions?.has(session))
260
+ return;
261
+ sessions.delete(session);
262
+ this.emitRouteListStatUpdate(routeHash);
263
+ }
264
+ emitRouteListStatUpdate(routeHash) {
265
+ const route = this.getRouteList().getAllRoutes().find(r => r.description.routeHash === routeHash);
266
+ if (!route)
267
+ return;
268
+ const stat = {
269
+ count: this.routeListSessions.get(routeHash)?.size ?? 0,
270
+ routeId: route.description.id,
271
+ routeHash
272
+ };
273
+ const idx = this.routesStats?.findIndex(s => s.routeHash === routeHash) ?? -1;
274
+ if (idx >= 0) {
275
+ this.routesStats[idx] = stat;
276
+ }
277
+ else {
278
+ this.routesStats ??= [];
279
+ this.routesStats.push(stat);
280
+ }
281
+ this.routeListObserver?.emit('stats-update', [stat]);
282
+ }
212
283
  start(session) {
213
284
  if (this.onlineStatusHandler || this.isOnline) {
214
285
  this.initList().catch(err => { this.logError(err, 'start'); });
@@ -597,6 +668,7 @@ let ActiveRidesService = (() => {
597
668
  onDisconnect() {
598
669
  this.getMessageQueue().onDisconnect();
599
670
  this.isSubscribed = false;
671
+ this.isSubscribedRouteListEvents = false;
600
672
  }
601
673
  async onConnect(props) {
602
674
  const { initial = false } = props ?? {};
@@ -604,6 +676,8 @@ let ActiveRidesService = (() => {
604
676
  this.getMessageQueue().onConnect();
605
677
  if (!initial && this.current && !this.isSubscribed)
606
678
  this.subscribeActivityEvents().catch(err => { this.logError(err, 'onConnect'); });
679
+ if (!initial && this.routeListObserver && !this.isSubscribedRouteListEvents)
680
+ this.subscribeRouteListEvents();
607
681
  }
608
682
  async subscribeActivityEvents() {
609
683
  const hash = this.current?.ride.routeHash;
@@ -622,8 +696,11 @@ let ActiveRidesService = (() => {
622
696
  onActivityEvent(topic, payload) {
623
697
  const keys = topic.split('/');
624
698
  const session = keys[2];
699
+ const routeHash = keys[3];
625
700
  if (session === this.current.sessionId)
626
701
  return;
702
+ if (routeHash !== this.current?.ride?.routeHash)
703
+ return;
627
704
  const event = keys[4];
628
705
  const handler = this.activityEventHandlers[event];
629
706
  if (handler)
@@ -140,13 +140,13 @@ let RoutesApiLoader = (() => {
140
140
  await this.processRouteFromApi(p.value[j], loadDetailRequired);
141
141
  }
142
142
  }
143
- this.loadObserver.emit('loaded');
143
+ this.loadObserver?.emit('loaded');
144
144
  if (loadDetailRequired.length > 0) {
145
145
  await this.loadDetails(loadDetailRequired, true);
146
146
  }
147
- this.loadObserver.emit('done');
147
+ this.loadObserver?.emit('done');
148
148
  await sleep(1);
149
- this.loadObserver.stop();
149
+ this.loadObserver?.stop();
150
150
  delete this.loadObserver;
151
151
  }
152
152
  async processRouteFromApi(descr, items) {
@@ -45,7 +45,7 @@ import { useDevicePairing } from "../devices/pairing";
45
45
  import { initPairingVisitTracker, usePairingVisitTracker } from "../devices/page/base/visit-log-factory";
46
46
  import { useFreeRideService, useRouteList } from "../routes";
47
47
  import { useWorkoutList } from "../workouts";
48
- import { useActivityList } from "../activities";
48
+ import { useActiveRides, useActivityList } from "../activities";
49
49
  import { useOnlineStatusMonitoring } from "../monitoring";
50
50
  import { useAppState, useFeatureToggleSync } from "../appstate";
51
51
  import { IncyclistPageService } from "../base/pages";
@@ -485,6 +485,7 @@ let UserInterfaceServcie = (() => {
485
485
  const freeRide = useFreeRideService();
486
486
  const workouts = useWorkoutList();
487
487
  const activities = useActivityList();
488
+ useActiveRides();
488
489
  const promises = [
489
490
  routes.preload().wait(),
490
491
  freeRide.init(),
@@ -29,6 +29,8 @@ export declare class ActiveRidesService extends IncyclistService {
29
29
  protected isStarted: boolean;
30
30
  protected routeListObserver: Observer;
31
31
  protected routesStats: ActiveRideCount[];
32
+ protected routeListSessions: Map<string, Set<string>>;
33
+ protected isSubscribedRouteListEvents: boolean;
32
34
  protected readonly activityEventHandlers: {
33
35
  update: any;
34
36
  start: any;
@@ -43,6 +45,13 @@ export declare class ActiveRidesService extends IncyclistService {
43
45
  protected countRidesByRoute(): Map<string, number>;
44
46
  protected onRoutePageOpened(observer: Observer): Promise<void>;
45
47
  protected getActiveRideCounts(): Promise<ActiveRideCount[]>;
48
+ protected buildRouteListStats(): ActiveRideCount[];
49
+ protected subscribeRouteListEvents(): void;
50
+ protected unsubscribeRouteListEvents(): void;
51
+ protected onRouteListActivityEvent(topic: string): void;
52
+ protected addRouteListSession(routeHash: string, session: string): void;
53
+ protected removeRouteListSession(routeHash: string, session: string): void;
54
+ protected emitRouteListStatUpdate(routeHash: string): void;
46
55
  protected start(session: string): void;
47
56
  protected initList(): Promise<void>;
48
57
  protected onActivityUpdated(): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-services",
3
- "version": "1.9.8",
3
+ "version": "1.9.9",
4
4
  "peerDependencies": {
5
5
  "gd-eventlog": "^0.1.27"
6
6
  },