Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(frontend:users): prevent recursive Angular tick on socket disconnect

johaven5ef44cb2aa64
diff --git a/frontend/src/app/applications/users/user.service.ts b/frontend/src/app/applications/users/user.service.ts
index 4b68ff36..ac844f0f 100644
--- a/frontend/src/app/applications/users/user.service.ts
+++ b/frontend/src/app/applications/users/user.service.ts
@@ -81,18 +81,16 @@ export class UserService {
private readonly notifications = inject(NotificationsService)
constructor() {
- this.webSocket.fromEvent('connect').subscribe(() => this.notifications.checkUnreadNotifications())
- this.webSocket.fromEvent('disconnect').subscribe(() => this.store.onlineUsers.set([]))
- this.webSocket.fromEvent(NOTIFICATIONS_WS.EVENTS.NOTIFICATION).subscribe(() => this.notifications.checkUnreadNotifications(true))
- this.webSocket
- .fromEvent(USERS_WS.EVENTS.ONLINE_USER)
+ this.fromWebSocketEvent('connect').subscribe(() => this.notifications.checkUnreadNotifications())
+ this.fromWebSocketEvent('disconnect').subscribe(() => this.store.onlineUsers.set([]))
+ this.fromWebSocketEvent(NOTIFICATIONS_WS.EVENTS.NOTIFICATION).subscribe(() => this.notifications.checkUnreadNotifications(true))
+ this.fromWebSocketEvent<UserOnline>(USERS_WS.EVENTS.ONLINE_USER)
.pipe(map((u: UserOnline) => new UserOnlineModel(u)))
.subscribe((user) => this.newOnlineUser(user))
- this.webSocket
- .fromEvent(USERS_WS.EVENTS.ONLINE_USERS)
+ this.fromWebSocketEvent<UserOnline[]>(USERS_WS.EVENTS.ONLINE_USERS)
.pipe(map((users: UserOnline[]) => users.map((u) => new UserOnlineModel(u))))
.subscribe((users) => this.setOnlineUsers(users))
- this.webSocket.fromEvent(USERS_WS.EVENTS.ONLINE_STATUS).subscribe((event: EventUpdateOnlineStatus) => this.receiveOnlineStatus(event))
+ this.fromWebSocketEvent<EventUpdateOnlineStatus>(USERS_WS.EVENTS.ONLINE_STATUS).subscribe((event) => this.receiveOnlineStatus(event))
}
get user() {
@@ -131,6 +129,14 @@ export class UserService {
this.webSocket.disconnect()
}
+ private fromWebSocketEvent<T = unknown>(eventName: string): Observable<T> {
+ return new Observable<T>((subscriber) => {
+ const listener = (...args: unknown[]) => subscriber.next(args[0] as T)
+ this.webSocket.on(eventName, listener)
+ return () => this.webSocket.removeListener(eventName, listener)
+ })
+ }
+
setOnlineUsers(users: UserOnlineModel[]) {
this.store.onlineUsers.set(users)
}