forked from wangziqi/gongxue-base
fix: resolve 9 P0+P1 review findings (bills decorators, campus isolation bypass, ding raw crash, batch import counter, SSE multi-tab, interval leak, dept permissions, cycle prevention, tree depth)
This commit is contained in:
@@ -9,6 +9,7 @@ import { CreateNotificationDto } from './dto/notification.dto';
|
||||
@Injectable()
|
||||
export class NotificationsService {
|
||||
private subjects = new Map<number, Subject<Notification>>();
|
||||
private subscriberCounts = new Map<number, number>();
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Notification)
|
||||
@@ -76,11 +77,20 @@ export class NotificationsService {
|
||||
subscribe(userId: number): Observable<Notification> {
|
||||
if (!this.subjects.has(userId)) {
|
||||
this.subjects.set(userId, new Subject<Notification>());
|
||||
this.subscriberCounts.set(userId, 0);
|
||||
}
|
||||
this.subscriberCounts.set(userId, (this.subscriberCounts.get(userId) ?? 0) + 1);
|
||||
return this.subjects.get(userId)!.asObservable();
|
||||
}
|
||||
|
||||
unsubscribe(userId: number): void {
|
||||
const count = (this.subscriberCounts.get(userId) ?? 0) - 1;
|
||||
if (count > 0) {
|
||||
this.subscriberCounts.set(userId, count);
|
||||
return;
|
||||
}
|
||||
// Last subscriber gone → complete + clean up
|
||||
this.subscriberCounts.delete(userId);
|
||||
const subj = this.subjects.get(userId);
|
||||
if (subj) {
|
||||
subj.complete();
|
||||
|
||||
Reference in New Issue
Block a user