Skip to content

Commit e860c39

Browse files
committed
refactor: extract logic shared by adlibs(actions) handlers
1 parent ff6516b commit e860c39

File tree

5 files changed

+87
-200
lines changed

5 files changed

+87
-200
lines changed
Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,11 @@
11
import { Logger } from 'winston'
22
import { CoreHandler } from '../coreHandler'
3-
import { Collection } from '../wsHandler'
4-
import { PublicationCollection } from '../publicationCollection'
5-
import { AdLibAction } from '@sofie-automation/corelib/dist/dataModel/AdlibAction'
63
import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections'
74
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
8-
import { RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
9-
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
10-
import { CollectionHandlers } from '../liveStatusServer'
11-
import { PickKeys } from '@sofie-automation/shared-lib/dist/lib/types'
12-
13-
const PLAYLIST_KEYS = ['currentPartInfo', 'nextPartInfo'] as const
14-
type Playlist = PickKeys<DBRundownPlaylist, typeof PLAYLIST_KEYS>
15-
16-
export class AdLibActionsHandler
17-
extends PublicationCollection<AdLibAction[], CorelibPubSub.adLibActions, CollectionName.AdLibActions>
18-
implements Collection<AdLibAction[]>
19-
{
20-
private _currentRundownId: RundownId | undefined
5+
import { RundownContentHandlerBase } from './rundownContentHandlerBase'
216

7+
export class AdLibActionsHandler extends RundownContentHandlerBase<CorelibPubSub.adLibActions> {
228
constructor(logger: Logger, coreHandler: CoreHandler) {
239
super(CollectionName.AdLibActions, CorelibPubSub.adLibActions, logger, coreHandler)
2410
}
25-
26-
init(handlers: CollectionHandlers): void {
27-
super.init(handlers)
28-
29-
handlers.playlistHandler.subscribe(this.onPlaylistUpdate, PLAYLIST_KEYS)
30-
}
31-
32-
protected changed(): void {
33-
this.updateAndNotify()
34-
}
35-
36-
private onPlaylistUpdate = (data: Playlist | undefined): void => {
37-
this.logUpdateReceived('playlist')
38-
const prevRundownId = this._currentRundownId
39-
40-
const rundownPlaylist = data
41-
42-
this._currentRundownId = rundownPlaylist?.currentPartInfo?.rundownId ?? rundownPlaylist?.nextPartInfo?.rundownId
43-
44-
if (prevRundownId !== this._currentRundownId) {
45-
this.stopSubscription()
46-
if (this._currentRundownId) {
47-
this.setupSubscription([this._currentRundownId])
48-
}
49-
// no need to trigger updateAndNotify() because the subscription will take care of this
50-
}
51-
}
52-
53-
private updateAndNotify(): void {
54-
const col = this.getCollectionOrFail()
55-
this._collectionData = col.find({ rundownId: this._currentRundownId })
56-
this.notify(this._collectionData)
57-
}
5811
}
Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,11 @@
11
import { Logger } from 'winston'
22
import { CoreHandler } from '../coreHandler'
3-
import { Collection } from '../wsHandler'
4-
import { PublicationCollection } from '../publicationCollection'
5-
import { AdLibPiece } from '@sofie-automation/corelib/dist/dataModel/AdLibPiece'
63
import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections'
74
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
8-
import { RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
9-
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
10-
import { CollectionHandlers } from '../liveStatusServer'
11-
import { PickKeys } from '@sofie-automation/shared-lib/dist/lib/types'
12-
13-
const PLAYLIST_KEYS = ['currentPartInfo', 'nextPartInfo'] as const
14-
type Playlist = PickKeys<DBRundownPlaylist, typeof PLAYLIST_KEYS>
15-
16-
export class AdLibsHandler
17-
extends PublicationCollection<AdLibPiece[], CorelibPubSub.adLibPieces, CollectionName.AdLibPieces>
18-
implements Collection<AdLibPiece[]>
19-
{
20-
private _currentRundownId: RundownId | undefined
5+
import { RundownContentHandlerBase } from './rundownContentHandlerBase'
216

7+
export class AdLibsHandler extends RundownContentHandlerBase<CorelibPubSub.adLibPieces> {
228
constructor(logger: Logger, coreHandler: CoreHandler) {
239
super(CollectionName.AdLibPieces, CorelibPubSub.adLibPieces, logger, coreHandler)
2410
}
25-
26-
init(handlers: CollectionHandlers): void {
27-
super.init(handlers)
28-
29-
handlers.playlistHandler.subscribe(this.onPlaylistUpdate, PLAYLIST_KEYS)
30-
}
31-
32-
protected changed(): void {
33-
this.updateAndNotify()
34-
}
35-
36-
private onPlaylistUpdate = (data: Playlist | undefined): void => {
37-
this.logUpdateReceived('playlist')
38-
const prevRundownId = this._currentRundownId
39-
const rundownPlaylist = data
40-
41-
this._currentRundownId = rundownPlaylist?.currentPartInfo?.rundownId ?? rundownPlaylist?.nextPartInfo?.rundownId
42-
43-
if (prevRundownId !== this._currentRundownId) {
44-
this.stopSubscription()
45-
if (this._currentRundownId) {
46-
this.setupSubscription([this._currentRundownId])
47-
}
48-
// no need to trigger updateAndNotify() because the subscription will take care of this
49-
}
50-
}
51-
52-
private updateAndNotify(): void {
53-
const collection = this.getCollectionOrFail()
54-
this._collectionData = collection.find({ rundownId: this._currentRundownId })
55-
this.notify(this._collectionData)
56-
}
5711
}
Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
import { Logger } from 'winston'
22
import { CoreHandler } from '../coreHandler'
3-
import { Collection } from '../wsHandler'
4-
import { PublicationCollection } from '../publicationCollection'
5-
import { RundownBaselineAdLibAction } from '@sofie-automation/corelib/dist/dataModel/RundownBaselineAdLibAction'
63
import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections'
74
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
8-
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
9-
import { CollectionHandlers } from '../liveStatusServer'
10-
import { RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
11-
import { PickKeys } from '@sofie-automation/shared-lib/dist/lib/types'
12-
13-
const PLAYLIST_KEYS = ['currentPartInfo', 'nextPartInfo'] as const
14-
type Playlist = PickKeys<DBRundownPlaylist, typeof PLAYLIST_KEYS>
15-
16-
export class GlobalAdLibActionsHandler
17-
extends PublicationCollection<
18-
RundownBaselineAdLibAction[],
19-
CorelibPubSub.rundownBaselineAdLibActions,
20-
CollectionName.RundownBaselineAdLibActions
21-
>
22-
implements Collection<RundownBaselineAdLibAction[]>
23-
{
24-
private _currentRundownId: RundownId | undefined
5+
import { RundownContentHandlerBase } from './rundownContentHandlerBase'
256

7+
export class GlobalAdLibActionsHandler extends RundownContentHandlerBase<CorelibPubSub.rundownBaselineAdLibActions> {
268
constructor(logger: Logger, coreHandler: CoreHandler) {
279
super(
2810
CollectionName.RundownBaselineAdLibActions,
@@ -31,35 +13,4 @@ export class GlobalAdLibActionsHandler
3113
coreHandler
3214
)
3315
}
34-
35-
init(handlers: CollectionHandlers): void {
36-
super.init(handlers)
37-
38-
handlers.playlistHandler.subscribe(this.onPlaylistUpdate, PLAYLIST_KEYS)
39-
}
40-
41-
protected changed(): void {
42-
this.updateAndNotify()
43-
}
44-
45-
private onPlaylistUpdate = (data: Playlist | undefined): void => {
46-
this.logUpdateReceived('playlist')
47-
const prevRundownId = this._currentRundownId
48-
const rundownPlaylist = data
49-
50-
this._currentRundownId = rundownPlaylist?.currentPartInfo?.rundownId ?? rundownPlaylist?.nextPartInfo?.rundownId
51-
52-
if (prevRundownId !== this._currentRundownId) {
53-
this.stopSubscription()
54-
if (this._currentRundownId) {
55-
this.setupSubscription([this._currentRundownId])
56-
}
57-
}
58-
}
59-
60-
private updateAndNotify(): void {
61-
const collection = this.getCollectionOrFail()
62-
this._collectionData = collection.find({ rundownId: this._currentRundownId })
63-
this.notify(this._collectionData)
64-
}
6516
}
Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,10 @@
11
import { Logger } from 'winston'
22
import { CoreHandler } from '../coreHandler'
3-
import { Collection } from '../wsHandler'
4-
import { PublicationCollection } from '../publicationCollection'
5-
import { RundownBaselineAdLibItem } from '@sofie-automation/corelib/dist/dataModel/RundownBaselineAdLibPiece'
63
import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections'
74
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
8-
import { RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
9-
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
10-
import { CollectionHandlers } from '../liveStatusServer'
11-
import { PickKeys } from '@sofie-automation/shared-lib/dist/lib/types'
12-
13-
const PLAYLIST_KEYS = ['currentPartInfo', 'nextPartInfo'] as const
14-
type Playlist = PickKeys<DBRundownPlaylist, typeof PLAYLIST_KEYS>
15-
16-
export class GlobalAdLibsHandler
17-
extends PublicationCollection<
18-
RundownBaselineAdLibItem[],
19-
CorelibPubSub.rundownBaselineAdLibPieces,
20-
CollectionName.RundownBaselineAdLibPieces
21-
>
22-
implements Collection<RundownBaselineAdLibItem[]>
23-
{
24-
private _currentRundownId: RundownId | undefined
25-
5+
import { RundownContentHandlerBase } from './rundownContentHandlerBase'
6+
export class GlobalAdLibsHandler extends RundownContentHandlerBase<CorelibPubSub.rundownBaselineAdLibPieces> {
267
constructor(logger: Logger, coreHandler: CoreHandler) {
278
super(CollectionName.RundownBaselineAdLibPieces, CorelibPubSub.rundownBaselineAdLibPieces, logger, coreHandler)
289
}
29-
30-
init(handlers: CollectionHandlers): void {
31-
super.init(handlers)
32-
33-
handlers.playlistHandler.subscribe(this.onPlaylistUpdate, PLAYLIST_KEYS)
34-
}
35-
36-
protected changed(): void {
37-
this.updateAndNotify()
38-
}
39-
40-
private onPlaylistUpdate = (data: Playlist | undefined): void => {
41-
this.logUpdateReceived('playlist')
42-
const prevRundownId = this._currentRundownId
43-
const rundownPlaylist = data
44-
45-
this._currentRundownId = rundownPlaylist?.currentPartInfo?.rundownId ?? rundownPlaylist?.nextPartInfo?.rundownId
46-
47-
if (prevRundownId !== this._currentRundownId) {
48-
this.stopSubscription()
49-
if (this._currentRundownId) {
50-
this.setupSubscription([this._currentRundownId])
51-
}
52-
}
53-
}
54-
55-
private updateAndNotify(): void {
56-
const collection = this.getCollectionOrFail()
57-
this._collectionData = collection.find({ rundownId: this._currentRundownId })
58-
this.notify(this._collectionData)
59-
}
6010
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { Logger } from 'winston'
2+
import { CoreHandler } from '../coreHandler'
3+
import { Collection } from '../wsHandler'
4+
import { PublicationCollection } from '../publicationCollection'
5+
import { CorelibPubSubCollections, CorelibPubSubTypes } from '@sofie-automation/corelib/dist/pubsub'
6+
import { RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
7+
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
8+
import { CollectionHandlers } from '../liveStatusServer'
9+
import { PickKeys } from '@sofie-automation/shared-lib/dist/lib/types'
10+
import { CollectionDocCheck } from '@sofie-automation/server-core-integration'
11+
import { ParametersOfFunctionOrNever } from '@sofie-automation/server-core-integration/dist/lib/subscriptions'
12+
13+
const PLAYLIST_KEYS = ['currentPartInfo', 'nextPartInfo'] as const
14+
type Playlist = PickKeys<DBRundownPlaylist, typeof PLAYLIST_KEYS>
15+
16+
type MatchingKeys<T, Args extends any[]> = {
17+
[K in keyof T]: T[K] extends (...args: Args) => any ? K : never
18+
}[keyof T]
19+
20+
type RundownMatchingKeys = MatchingKeys<CorelibPubSubTypes, [RundownId[], (string | undefined)?]>
21+
22+
/**
23+
* For items whose `rundownId` should equal `rundownId` of the current Part (or next Part, if the firts Take was not performed)
24+
*/
25+
export abstract class RundownContentHandlerBase<TPubSub extends RundownMatchingKeys>
26+
extends PublicationCollection<
27+
CollectionDocCheck<CorelibPubSubCollections[ReturnType<CorelibPubSubTypes[TPubSub]>]>[],
28+
TPubSub,
29+
ReturnType<CorelibPubSubTypes[TPubSub]>
30+
>
31+
implements Collection<CollectionDocCheck<CorelibPubSubCollections[ReturnType<CorelibPubSubTypes[TPubSub]>]>[]>
32+
{
33+
private _currentRundownId: RundownId | undefined
34+
35+
constructor(
36+
collection: ReturnType<CorelibPubSubTypes[TPubSub]>,
37+
publication: TPubSub,
38+
logger: Logger,
39+
coreHandler: CoreHandler
40+
) {
41+
super(collection, publication, logger, coreHandler)
42+
}
43+
44+
init(handlers: CollectionHandlers): void {
45+
super.init(handlers)
46+
47+
handlers.playlistHandler.subscribe(this.onPlaylistUpdate, PLAYLIST_KEYS)
48+
}
49+
50+
protected changed(): void {
51+
this.updateAndNotify()
52+
}
53+
54+
private onPlaylistUpdate = (data: Playlist | undefined): void => {
55+
this.logUpdateReceived('playlist')
56+
const prevRundownId = this._currentRundownId
57+
58+
const rundownPlaylist = data
59+
60+
this._currentRundownId = rundownPlaylist?.currentPartInfo?.rundownId ?? rundownPlaylist?.nextPartInfo?.rundownId
61+
62+
if (prevRundownId !== this._currentRundownId) {
63+
this.stopSubscription()
64+
if (this._currentRundownId) {
65+
const args = [[this._currentRundownId]] as unknown as ParametersOfFunctionOrNever<
66+
CorelibPubSubTypes[TPubSub]
67+
> // TODO: get rid of this type conversion
68+
this.setupSubscription(...args)
69+
}
70+
// no need to trigger updateAndNotify() because the subscription will take care of this
71+
}
72+
}
73+
74+
private updateAndNotify(): void {
75+
const col = this.getCollectionOrFail()
76+
this._collectionData = col.find({ rundownId: this._currentRundownId })
77+
this.notify(this._collectionData)
78+
}
79+
}

0 commit comments

Comments
 (0)