Skip to content

Commit bc26185

Browse files
committed
chore: simplify notification merge logic
1 parent 2bfffca commit bc26185

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

packages/live-status-gateway/src/collections/notifications/notificationsHandler.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { DBNotificationObj } from '@sofie-automation/corelib/dist/dataModel/Noti
88
import { PlaylistNotificationsHandler } from './playlistNotificationsHandler.js'
99
import { RundownNotificationsHandler } from './rundownNotificationsHandler.js'
1010
import _ from 'underscore'
11-
import { unprotectString } from '@sofie-automation/server-core-integration'
1211

1312
const THROTTLE_PERIOD_MS = 100
1413

@@ -59,33 +58,23 @@ export class NotificationsHandler extends PublicationCollection<
5958
}
6059

6160
private updateCollectionData(): boolean {
62-
const merged = new Map<string, DBNotificationObj>()
63-
64-
// Pull data from the playlist notifications handler's collection
65-
if (this._playlistNotificationsHandler) {
66-
const playlistDocs = this._playlistNotificationsHandler.getPublishedDocs()
67-
for (const d of playlistDocs) {
68-
if (d._id && !merged.has(unprotectString(d._id))) {
69-
merged.set(unprotectString(d._id), d)
70-
}
71-
}
72-
}
73-
74-
// Pull data from the rundown notifications handler's collection
75-
if (this._rundownNotificationsHandler) {
76-
const rundownDocs = this._rundownNotificationsHandler.getPublishedDocs()
77-
for (const d of rundownDocs) {
78-
if (d._id && !merged.has(unprotectString(d._id))) {
79-
merged.set(unprotectString(d._id), d)
80-
}
81-
}
61+
let merged: DBNotificationObj[] = []
62+
63+
if (this._playlistNotificationsHandler && this._rundownNotificationsHandler) {
64+
// deduplication of the array is needed as in some cases notifications are published by both topics.
65+
merged = _.uniq(
66+
[
67+
...this._playlistNotificationsHandler.getPublishedDocs(),
68+
...this._rundownNotificationsHandler.getPublishedDocs(),
69+
],
70+
false,
71+
(item) => item._id
72+
)
8273
}
8374

84-
const newNotifications = Array.from(merged.values())
85-
86-
const hasAnythingChanged = !_.isEqual(this._collectionData, newNotifications)
75+
const hasAnythingChanged = !_.isEqual(this._collectionData, merged)
8776
if (hasAnythingChanged) {
88-
this._collectionData = newNotifications
77+
this._collectionData = merged
8978
}
9079

9180
return hasAnythingChanged

0 commit comments

Comments
 (0)