Skip to content

Commit 8bf84b3

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

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

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

Lines changed: 8 additions & 24 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,18 @@ export class NotificationsHandler extends PublicationCollection<
5958
}
6059

6160
private updateCollectionData(): boolean {
62-
const merged = new Map<string, DBNotificationObj>()
61+
let merged: DBNotificationObj[] = []
6362

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-
}
63+
if (this._playlistNotificationsHandler && this._rundownNotificationsHandler) {
64+
merged = [
65+
...this._playlistNotificationsHandler.getPublishedDocs(),
66+
...this._rundownNotificationsHandler.getPublishedDocs(),
67+
]
7268
}
7369

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-
}
82-
}
83-
84-
const newNotifications = Array.from(merged.values())
85-
86-
const hasAnythingChanged = !_.isEqual(this._collectionData, newNotifications)
70+
const hasAnythingChanged = !_.isEqual(this._collectionData, merged)
8771
if (hasAnythingChanged) {
88-
this._collectionData = newNotifications
72+
this._collectionData = merged
8973
}
9074

9175
return hasAnythingChanged

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { Logger } from 'winston'
88
import { CoreHandler } from '../../coreHandler.js'
99
import { CollectionHandlers } from '../../liveStatusServer.js'
1010
import { PublicationCollection } from '../../publicationCollection.js'
11-
import { DBNotificationObj } from '@sofie-automation/corelib/dist/dataModel/Notifications'
11+
import {
12+
DBNotificationObj,
13+
DBNotificationTargetRundownPlaylist,
14+
} from '@sofie-automation/corelib/dist/dataModel/Notifications'
1215

1316
const PLAYLIST_KEYS = ['_id'] as const
1417
type Playlist = PickKeys<DBRundownPlaylist, typeof PLAYLIST_KEYS>
@@ -44,7 +47,15 @@ export class PlaylistNotificationsHandler extends PublicationCollection<
4447

4548
private updateCollectionData() {
4649
const collection = this.getCollectionOrFail()
47-
this._collectionData = collection.find({})
50+
this._collectionData = collection.find((doc: DBNotificationObj) => {
51+
const relatedTo: DBNotificationTargetRundownPlaylist | undefined = (doc.relatedTo as any).playlistId
52+
? (doc.relatedTo as DBNotificationTargetRundownPlaylist)
53+
: undefined
54+
55+
return (
56+
relatedTo && relatedTo.playlistId === this._currentPlaylistId && this._studioId === relatedTo.studioId
57+
)
58+
})
4859
}
4960

5061
private clearCollectionData() {

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Logger } from 'winston'
77
import { CoreHandler } from '../../coreHandler.js'
88
import { CollectionHandlers } from '../../liveStatusServer.js'
99
import { PublicationCollection } from '../../publicationCollection.js'
10-
import { DBNotificationObj } from '@sofie-automation/corelib/dist/dataModel/Notifications'
10+
import { DBNotificationObj, DBNotificationTargetRundown } from '@sofie-automation/corelib/dist/dataModel/Notifications'
1111
import { PickKeys } from '@sofie-automation/shared-lib/dist/lib/types'
1212
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
1313

@@ -47,7 +47,17 @@ export class RundownNotificationsHandler extends PublicationCollection<
4747

4848
private updateCollectionData() {
4949
const collection = this.getCollectionOrFail()
50-
this._collectionData = collection.find({})
50+
this._collectionData = collection.find((doc: DBNotificationObj) => {
51+
const relatedTo: DBNotificationTargetRundown | undefined = (doc.relatedTo as any).rundownId
52+
? (doc.relatedTo as DBNotificationTargetRundown)
53+
: undefined
54+
55+
return (
56+
relatedTo &&
57+
this._currentRundownIds.includes(relatedTo.rundownId) &&
58+
this._studioId === relatedTo.studioId
59+
)
60+
})
5161
}
5262

5363
private clearCollectionData() {

0 commit comments

Comments
 (0)