Skip to content

Commit 3252531

Browse files
committed
refactor(EAV-450): get rid of async in handlers; extract shared logic to base classes
1 parent b51ee26 commit 3252531

30 files changed

+1227
-1448
lines changed
Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,56 @@
11
import { Logger } from 'winston'
22
import { CoreHandler } from '../coreHandler'
3-
import { CollectionBase, Collection, CollectionObserver } from '../wsHandler'
3+
import { Collection, PickArr, PublicationCollection } from '../wsHandler'
44
import { AdLibAction } from '@sofie-automation/corelib/dist/dataModel/AdlibAction'
5-
import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance'
65
import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections'
76
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
8-
import { AdLibActionId, RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
9-
import { SelectedPartInstances } from './partInstancesHandler'
7+
import { RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
8+
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
9+
import { CollectionHandlers } from '../liveStatusServer'
10+
11+
const PLAYLIST_KEYS = ['currentPartInfo', 'nextPartInfo'] as const
12+
type Playlist = PickArr<DBRundownPlaylist, typeof PLAYLIST_KEYS>
1013

1114
export class AdLibActionsHandler
12-
extends CollectionBase<AdLibAction[], CorelibPubSub.adLibActions, CollectionName.AdLibActions>
13-
implements Collection<AdLibAction[]>, CollectionObserver<SelectedPartInstances>
15+
extends PublicationCollection<AdLibAction[], CorelibPubSub.adLibActions, CollectionName.AdLibActions>
16+
implements Collection<AdLibAction[]>
1417
{
15-
public observerName: string
16-
private _curRundownId: RundownId | undefined
17-
private _curPartInstance: DBPartInstance | undefined
18+
private _currentRundownId: RundownId | undefined
1819

1920
constructor(logger: Logger, coreHandler: CoreHandler) {
20-
super(AdLibActionsHandler.name, CollectionName.AdLibActions, CorelibPubSub.adLibActions, logger, coreHandler)
21-
this.observerName = this._name
21+
super(CollectionName.AdLibActions, CorelibPubSub.adLibActions, logger, coreHandler)
22+
}
23+
24+
init(handlers: CollectionHandlers): void {
25+
super.init(handlers)
26+
27+
handlers.playlistHandler.subscribe(this.onPlaylistUpdate, PLAYLIST_KEYS)
2228
}
2329

24-
async changed(id: AdLibActionId, changeType: string): Promise<void> {
25-
this.logDocumentChange(id, changeType)
26-
if (!this._collectionName) return
27-
const col = this._core.getCollection(this._collectionName)
28-
if (!col) throw new Error(`collection '${this._collectionName}' not found!`)
29-
this._collectionData = col.find({ rundownId: this._curRundownId })
30-
await this.notify(this._collectionData)
30+
protected changed(): void {
31+
this.updateAndNotify()
3132
}
3233

33-
async update(source: string, data: SelectedPartInstances | undefined): Promise<void> {
34-
this.logUpdateReceived('partInstances', source)
35-
const prevRundownId = this._curRundownId
36-
this._curPartInstance = data ? data.current ?? data.next : undefined
37-
this._curRundownId = this._curPartInstance ? this._curPartInstance.rundownId : undefined
34+
private onPlaylistUpdate = (data: Playlist | undefined): void => {
35+
this.logUpdateReceived('playlist')
36+
const prevRundownId = this._currentRundownId
3837

39-
await new Promise(process.nextTick.bind(this))
40-
if (!this._collectionName) return
41-
if (!this._publicationName) return
42-
if (prevRundownId !== this._curRundownId) {
43-
if (this._subscriptionId) this._coreHandler.unsubscribe(this._subscriptionId)
44-
if (this._dbObserver) this._dbObserver.stop()
45-
if (this._curRundownId && this._curPartInstance) {
46-
this._subscriptionId = await this._coreHandler.setupSubscription(this._publicationName, [
47-
this._curRundownId,
48-
])
49-
this._dbObserver = this._coreHandler.setupObserver(this._collectionName)
50-
this._dbObserver.added = (id) => {
51-
void this.changed(id, 'added').catch(this._logger.error)
52-
}
53-
this._dbObserver.changed = (id) => {
54-
void this.changed(id, 'changed').catch(this._logger.error)
55-
}
56-
this._dbObserver.removed = (id) => {
57-
void this.changed(id, 'removed').catch(this._logger.error)
58-
}
38+
const rundownPlaylist = data
5939

60-
const collection = this._core.getCollection(this._collectionName)
61-
if (!collection) throw new Error(`collection '${this._collectionName}' not found!`)
62-
this._collectionData = collection.find({
63-
rundownId: this._curRundownId,
64-
})
65-
await this.notify(this._collectionData)
40+
this._currentRundownId = rundownPlaylist?.currentPartInfo?.rundownId ?? rundownPlaylist?.nextPartInfo?.rundownId
41+
42+
if (prevRundownId !== this._currentRundownId) {
43+
this.stopSubscription()
44+
if (this._currentRundownId) {
45+
this.setupSubscription([this._currentRundownId])
6646
}
47+
// no need to trigger updateAndNotify() because the subscription will take care of this
6748
}
6849
}
6950

70-
// override notify to implement empty array handling
71-
async notify(data: AdLibAction[] | undefined): Promise<void> {
72-
this.logNotifyingUpdate(data?.length)
73-
if (data !== undefined) {
74-
for (const observer of this._observers) {
75-
await observer.update(this._name, data)
76-
}
77-
}
51+
protected updateAndNotify(): void {
52+
const col = this.getCollectionOrFail()
53+
this._collectionData = col.find({ rundownId: this._currentRundownId })
54+
this.notify(this._collectionData)
7855
}
7956
}
Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,55 @@
11
import { Logger } from 'winston'
22
import { CoreHandler } from '../coreHandler'
3-
import { CollectionBase, Collection, CollectionObserver } from '../wsHandler'
3+
import { Collection, PickArr, PublicationCollection } from '../wsHandler'
44
import { AdLibPiece } from '@sofie-automation/corelib/dist/dataModel/AdLibPiece'
5-
import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance'
65
import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections'
76
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
8-
import { PieceId, RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
9-
import { SelectedPartInstances } from './partInstancesHandler'
7+
import { RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
8+
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
9+
import { CollectionHandlers } from '../liveStatusServer'
10+
11+
const PLAYLIST_KEYS = ['currentPartInfo', 'nextPartInfo'] as const
12+
type Playlist = PickArr<DBRundownPlaylist, typeof PLAYLIST_KEYS>
1013

1114
export class AdLibsHandler
12-
extends CollectionBase<AdLibPiece[], CorelibPubSub.adLibPieces, CollectionName.AdLibPieces>
13-
implements Collection<AdLibPiece[]>, CollectionObserver<SelectedPartInstances>
15+
extends PublicationCollection<AdLibPiece[], CorelibPubSub.adLibPieces, CollectionName.AdLibPieces>
16+
implements Collection<AdLibPiece[]>
1417
{
15-
public observerName: string
16-
// private _core: CoreConnection
1718
private _currentRundownId: RundownId | undefined
18-
private _currentPartInstance: DBPartInstance | undefined
1919

2020
constructor(logger: Logger, coreHandler: CoreHandler) {
21-
super(AdLibsHandler.name, CollectionName.AdLibPieces, CorelibPubSub.adLibPieces, logger, coreHandler)
22-
this.observerName = this._name
21+
super(CollectionName.AdLibPieces, CorelibPubSub.adLibPieces, logger, coreHandler)
22+
}
23+
24+
init(handlers: CollectionHandlers): void {
25+
super.init(handlers)
26+
27+
handlers.playlistHandler.subscribe(this.onPlaylistUpdate, PLAYLIST_KEYS)
2328
}
2429

25-
async changed(id: PieceId, changeType: string): Promise<void> {
26-
this.logDocumentChange(id, changeType)
27-
if (!this._collectionName) return
28-
const col = this._core.getCollection(this._collectionName)
29-
if (!col) throw new Error(`collection '${this._collectionName}' not found!`)
30-
this._collectionData = col.find({ rundownId: this._currentRundownId })
31-
await this.notify(this._collectionData)
30+
changed(): void {
31+
this.updateAndNotify()
3232
}
3333

34-
async update(source: string, data: SelectedPartInstances | undefined): Promise<void> {
35-
this.logUpdateReceived('partInstances', source)
34+
private onPlaylistUpdate = (data: Playlist | undefined): void => {
35+
this.logUpdateReceived('playlist')
3636
const prevRundownId = this._currentRundownId
37-
this._currentPartInstance = data ? data.current ?? data.next : undefined
38-
this._currentRundownId = this._currentPartInstance?.rundownId
37+
const rundownPlaylist = data
3938

40-
await new Promise(process.nextTick.bind(this))
41-
if (!this._collectionName) return
42-
if (!this._publicationName) return
43-
if (prevRundownId !== this._currentRundownId) {
44-
if (this._subscriptionId) this._coreHandler.unsubscribe(this._subscriptionId)
45-
if (this._dbObserver) this._dbObserver.stop()
46-
if (this._currentRundownId && this._currentPartInstance) {
47-
this._subscriptionId = await this._coreHandler.setupSubscription(this._publicationName, [
48-
this._currentRundownId,
49-
])
50-
this._dbObserver = this._coreHandler.setupObserver(this._collectionName)
51-
this._dbObserver.added = (id) => {
52-
void this.changed(id, 'added').catch(this._logger.error)
53-
}
54-
this._dbObserver.changed = (id) => {
55-
void this.changed(id, 'changed').catch(this._logger.error)
56-
}
57-
this._dbObserver.removed = (id) => {
58-
void this.changed(id, 'removed').catch(this._logger.error)
59-
}
39+
this._currentRundownId = rundownPlaylist?.currentPartInfo?.rundownId ?? rundownPlaylist?.nextPartInfo?.rundownId
6040

61-
const collection = this._core.getCollection(this._collectionName)
62-
if (!collection) throw new Error(`collection '${this._collectionName}' not found!`)
63-
this._collectionData = collection.find({
64-
rundownId: this._currentRundownId,
65-
})
66-
await this.notify(this._collectionData)
41+
if (prevRundownId !== this._currentRundownId) {
42+
this.stopSubscription()
43+
if (this._currentRundownId) {
44+
this.setupSubscription([this._currentRundownId])
6745
}
46+
// no need to trigger updateAndNotify() because the subscription will take care of this
6847
}
6948
}
7049

71-
// override notify to implement empty array handling
72-
async notify(data: AdLibPiece[] | undefined): Promise<void> {
73-
this.logNotifyingUpdate(data?.length)
74-
if (data !== undefined) {
75-
for (const observer of this._observers) {
76-
await observer.update(this._name, data)
77-
}
78-
}
50+
protected updateAndNotify(): void {
51+
const collection = this.getCollectionOrFail()
52+
this._collectionData = collection.find({ rundownId: this._currentRundownId })
53+
this.notify(this._collectionData)
7954
}
8055
}
Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,63 @@
11
import { Logger } from 'winston'
22
import { CoreHandler } from '../coreHandler'
3-
import { CollectionBase, Collection, CollectionObserver } from '../wsHandler'
3+
import { Collection, PickArr, PublicationCollection } from '../wsHandler'
44
import { RundownBaselineAdLibAction } from '@sofie-automation/corelib/dist/dataModel/RundownBaselineAdLibAction'
55
import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections'
66
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
7-
import { RundownBaselineAdLibActionId, RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
8-
import { SelectedPartInstances } from './partInstancesHandler'
7+
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
8+
import { CollectionHandlers } from '../liveStatusServer'
9+
import { RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
10+
11+
const PLAYLIST_KEYS = ['currentPartInfo', 'nextPartInfo'] as const
12+
type Playlist = PickArr<DBRundownPlaylist, typeof PLAYLIST_KEYS>
913

1014
export class GlobalAdLibActionsHandler
11-
extends CollectionBase<
15+
extends PublicationCollection<
1216
RundownBaselineAdLibAction[],
1317
CorelibPubSub.rundownBaselineAdLibActions,
1418
CollectionName.RundownBaselineAdLibActions
1519
>
16-
implements Collection<RundownBaselineAdLibAction[]>, CollectionObserver<SelectedPartInstances>
20+
implements Collection<RundownBaselineAdLibAction[]>
1721
{
18-
public observerName: string
1922
private _currentRundownId: RundownId | undefined
2023

2124
constructor(logger: Logger, coreHandler: CoreHandler) {
2225
super(
23-
GlobalAdLibActionsHandler.name,
2426
CollectionName.RundownBaselineAdLibActions,
2527
CorelibPubSub.rundownBaselineAdLibActions,
2628
logger,
2729
coreHandler
2830
)
29-
this.observerName = this._name
3031
}
3132

32-
async changed(id: RundownBaselineAdLibActionId, changeType: string): Promise<void> {
33-
this.logDocumentChange(id, changeType)
34-
if (!this._collectionName) return
35-
const col = this._core.getCollection(this._collectionName)
36-
if (!col) throw new Error(`collection '${this._collectionName}' not found!`)
37-
this._collectionData = col.find({ rundownId: this._currentRundownId })
38-
await this.notify(this._collectionData)
33+
init(handlers: CollectionHandlers): void {
34+
super.init(handlers)
35+
36+
handlers.playlistHandler.subscribe(this.onPlaylistUpdate, PLAYLIST_KEYS)
37+
}
38+
39+
changed(): void {
40+
this.updateAndNotify()
3941
}
4042

41-
async update(source: string, data: SelectedPartInstances | undefined): Promise<void> {
42-
this.logUpdateReceived('partInstances', source)
43+
private onPlaylistUpdate = (data: Playlist | undefined): void => {
44+
this.logUpdateReceived('playlist')
4345
const prevRundownId = this._currentRundownId
44-
const partInstance = data ? data.current ?? data.next : undefined
45-
this._currentRundownId = partInstance?.rundownId
46+
const rundownPlaylist = data
47+
48+
this._currentRundownId = rundownPlaylist?.currentPartInfo?.rundownId ?? rundownPlaylist?.nextPartInfo?.rundownId
4649

47-
await new Promise(process.nextTick.bind(this))
48-
if (!this._collectionName) return
49-
if (!this._publicationName) return
5050
if (prevRundownId !== this._currentRundownId) {
51-
if (this._subscriptionId) this._coreHandler.unsubscribe(this._subscriptionId)
52-
if (this._dbObserver) this._dbObserver.stop()
51+
this.stopSubscription()
5352
if (this._currentRundownId) {
54-
this._subscriptionId = await this._coreHandler.setupSubscription(this._publicationName, [
55-
this._currentRundownId,
56-
])
57-
this._dbObserver = this._coreHandler.setupObserver(this._collectionName)
58-
this._dbObserver.added = (id) => {
59-
void this.changed(id, 'added').catch(this._logger.error)
60-
}
61-
this._dbObserver.changed = (id) => {
62-
void this.changed(id, 'changed').catch(this._logger.error)
63-
}
64-
this._dbObserver.removed = (id) => {
65-
void this.changed(id, 'removed').catch(this._logger.error)
66-
}
67-
68-
const collection = this._core.getCollection(this._collectionName)
69-
if (!collection) throw new Error(`collection '${this._collectionName}' not found!`)
70-
this._collectionData = collection.find({ rundownId: this._currentRundownId })
71-
await this.notify(this._collectionData)
53+
this.setupSubscription([this._currentRundownId])
7254
}
7355
}
7456
}
7557

76-
// override notify to implement empty array handling
77-
async notify(data: RundownBaselineAdLibAction[] | undefined): Promise<void> {
78-
this.logNotifyingUpdate(data?.length)
79-
if (data !== undefined) {
80-
for (const observer of this._observers) {
81-
await observer.update(this._name, data)
82-
}
83-
}
58+
protected updateAndNotify(): void {
59+
const collection = this.getCollectionOrFail()
60+
this._collectionData = collection.find({ rundownId: this._currentRundownId })
61+
this.notify(this._collectionData)
8462
}
8563
}

0 commit comments

Comments
 (0)