Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(LSG): get rid of async in handlers; extract shared logic to base classes #1367

Open
wants to merge 1 commit into
base: release53
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,79 +1,56 @@
import { Logger } from 'winston'
import { CoreHandler } from '../coreHandler'
import { CollectionBase, Collection, CollectionObserver } from '../wsHandler'
import { Collection, PickArr, PublicationCollection } from '../wsHandler'
import { AdLibAction } from '@sofie-automation/corelib/dist/dataModel/AdlibAction'
import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance'
import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections'
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
import { AdLibActionId, RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
import { SelectedPartInstances } from './partInstancesHandler'
import { RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
import { CollectionHandlers } from '../liveStatusServer'

const PLAYLIST_KEYS = ['currentPartInfo', 'nextPartInfo'] as const
type Playlist = PickArr<DBRundownPlaylist, typeof PLAYLIST_KEYS>

export class AdLibActionsHandler
extends CollectionBase<AdLibAction[], CorelibPubSub.adLibActions, CollectionName.AdLibActions>
implements Collection<AdLibAction[]>, CollectionObserver<SelectedPartInstances>
extends PublicationCollection<AdLibAction[], CorelibPubSub.adLibActions, CollectionName.AdLibActions>
implements Collection<AdLibAction[]>
{
public observerName: string
private _curRundownId: RundownId | undefined
private _curPartInstance: DBPartInstance | undefined
private _currentRundownId: RundownId | undefined

constructor(logger: Logger, coreHandler: CoreHandler) {
super(AdLibActionsHandler.name, CollectionName.AdLibActions, CorelibPubSub.adLibActions, logger, coreHandler)
this.observerName = this._name
super(CollectionName.AdLibActions, CorelibPubSub.adLibActions, logger, coreHandler)
}

init(handlers: CollectionHandlers): void {
super.init(handlers)

handlers.playlistHandler.subscribe(this.onPlaylistUpdate, PLAYLIST_KEYS)
}

async changed(id: AdLibActionId, changeType: string): Promise<void> {
this.logDocumentChange(id, changeType)
if (!this._collectionName) return
const col = this._core.getCollection(this._collectionName)
if (!col) throw new Error(`collection '${this._collectionName}' not found!`)
this._collectionData = col.find({ rundownId: this._curRundownId })
await this.notify(this._collectionData)
protected changed(): void {
this.updateAndNotify()
}

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

await new Promise(process.nextTick.bind(this))
if (!this._collectionName) return
if (!this._publicationName) return
if (prevRundownId !== this._curRundownId) {
if (this._subscriptionId) this._coreHandler.unsubscribe(this._subscriptionId)
if (this._dbObserver) this._dbObserver.stop()
if (this._curRundownId && this._curPartInstance) {
this._subscriptionId = await this._coreHandler.setupSubscription(this._publicationName, [
this._curRundownId,
])
this._dbObserver = this._coreHandler.setupObserver(this._collectionName)
this._dbObserver.added = (id) => {
void this.changed(id, 'added').catch(this._logger.error)
}
this._dbObserver.changed = (id) => {
void this.changed(id, 'changed').catch(this._logger.error)
}
this._dbObserver.removed = (id) => {
void this.changed(id, 'removed').catch(this._logger.error)
}
const rundownPlaylist = data

const collection = this._core.getCollection(this._collectionName)
if (!collection) throw new Error(`collection '${this._collectionName}' not found!`)
this._collectionData = collection.find({
rundownId: this._curRundownId,
})
await this.notify(this._collectionData)
this._currentRundownId = rundownPlaylist?.currentPartInfo?.rundownId ?? rundownPlaylist?.nextPartInfo?.rundownId

if (prevRundownId !== this._currentRundownId) {
this.stopSubscription()
if (this._currentRundownId) {
this.setupSubscription([this._currentRundownId])
}
// no need to trigger updateAndNotify() because the subscription will take care of this
}
}

// override notify to implement empty array handling
async notify(data: AdLibAction[] | undefined): Promise<void> {
this.logNotifyingUpdate(data?.length)
if (data !== undefined) {
for (const observer of this._observers) {
await observer.update(this._name, data)
}
}
protected updateAndNotify(): void {
const col = this.getCollectionOrFail()
this._collectionData = col.find({ rundownId: this._currentRundownId })
this.notify(this._collectionData)
}
}
87 changes: 31 additions & 56 deletions packages/live-status-gateway/src/collections/adLibsHandler.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,55 @@
import { Logger } from 'winston'
import { CoreHandler } from '../coreHandler'
import { CollectionBase, Collection, CollectionObserver } from '../wsHandler'
import { Collection, PickArr, PublicationCollection } from '../wsHandler'
import { AdLibPiece } from '@sofie-automation/corelib/dist/dataModel/AdLibPiece'
import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance'
import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections'
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
import { PieceId, RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
import { SelectedPartInstances } from './partInstancesHandler'
import { RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
import { CollectionHandlers } from '../liveStatusServer'

const PLAYLIST_KEYS = ['currentPartInfo', 'nextPartInfo'] as const
type Playlist = PickArr<DBRundownPlaylist, typeof PLAYLIST_KEYS>

export class AdLibsHandler
extends CollectionBase<AdLibPiece[], CorelibPubSub.adLibPieces, CollectionName.AdLibPieces>
implements Collection<AdLibPiece[]>, CollectionObserver<SelectedPartInstances>
extends PublicationCollection<AdLibPiece[], CorelibPubSub.adLibPieces, CollectionName.AdLibPieces>
implements Collection<AdLibPiece[]>
{
public observerName: string
// private _core: CoreConnection
private _currentRundownId: RundownId | undefined
private _currentPartInstance: DBPartInstance | undefined

constructor(logger: Logger, coreHandler: CoreHandler) {
super(AdLibsHandler.name, CollectionName.AdLibPieces, CorelibPubSub.adLibPieces, logger, coreHandler)
this.observerName = this._name
super(CollectionName.AdLibPieces, CorelibPubSub.adLibPieces, logger, coreHandler)
}

init(handlers: CollectionHandlers): void {
super.init(handlers)

handlers.playlistHandler.subscribe(this.onPlaylistUpdate, PLAYLIST_KEYS)
}

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

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

await new Promise(process.nextTick.bind(this))
if (!this._collectionName) return
if (!this._publicationName) return
if (prevRundownId !== this._currentRundownId) {
if (this._subscriptionId) this._coreHandler.unsubscribe(this._subscriptionId)
if (this._dbObserver) this._dbObserver.stop()
if (this._currentRundownId && this._currentPartInstance) {
this._subscriptionId = await this._coreHandler.setupSubscription(this._publicationName, [
this._currentRundownId,
])
this._dbObserver = this._coreHandler.setupObserver(this._collectionName)
this._dbObserver.added = (id) => {
void this.changed(id, 'added').catch(this._logger.error)
}
this._dbObserver.changed = (id) => {
void this.changed(id, 'changed').catch(this._logger.error)
}
this._dbObserver.removed = (id) => {
void this.changed(id, 'removed').catch(this._logger.error)
}
this._currentRundownId = rundownPlaylist?.currentPartInfo?.rundownId ?? rundownPlaylist?.nextPartInfo?.rundownId

const collection = this._core.getCollection(this._collectionName)
if (!collection) throw new Error(`collection '${this._collectionName}' not found!`)
this._collectionData = collection.find({
rundownId: this._currentRundownId,
})
await this.notify(this._collectionData)
if (prevRundownId !== this._currentRundownId) {
this.stopSubscription()
if (this._currentRundownId) {
this.setupSubscription([this._currentRundownId])
}
// no need to trigger updateAndNotify() because the subscription will take care of this
}
}

// override notify to implement empty array handling
async notify(data: AdLibPiece[] | undefined): Promise<void> {
this.logNotifyingUpdate(data?.length)
if (data !== undefined) {
for (const observer of this._observers) {
await observer.update(this._name, data)
}
}
protected updateAndNotify(): void {
const collection = this.getCollectionOrFail()
this._collectionData = collection.find({ rundownId: this._currentRundownId })
this.notify(this._collectionData)
}
}
Original file line number Diff line number Diff line change
@@ -1,85 +1,63 @@
import { Logger } from 'winston'
import { CoreHandler } from '../coreHandler'
import { CollectionBase, Collection, CollectionObserver } from '../wsHandler'
import { Collection, PickArr, PublicationCollection } from '../wsHandler'
import { RundownBaselineAdLibAction } from '@sofie-automation/corelib/dist/dataModel/RundownBaselineAdLibAction'
import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections'
import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
import { RundownBaselineAdLibActionId, RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
import { SelectedPartInstances } from './partInstancesHandler'
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
import { CollectionHandlers } from '../liveStatusServer'
import { RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'

const PLAYLIST_KEYS = ['currentPartInfo', 'nextPartInfo'] as const
type Playlist = PickArr<DBRundownPlaylist, typeof PLAYLIST_KEYS>

export class GlobalAdLibActionsHandler
extends CollectionBase<
extends PublicationCollection<
RundownBaselineAdLibAction[],
CorelibPubSub.rundownBaselineAdLibActions,
CollectionName.RundownBaselineAdLibActions
>
implements Collection<RundownBaselineAdLibAction[]>, CollectionObserver<SelectedPartInstances>
implements Collection<RundownBaselineAdLibAction[]>
{
public observerName: string
private _currentRundownId: RundownId | undefined

constructor(logger: Logger, coreHandler: CoreHandler) {
super(
GlobalAdLibActionsHandler.name,
CollectionName.RundownBaselineAdLibActions,
CorelibPubSub.rundownBaselineAdLibActions,
logger,
coreHandler
)
this.observerName = this._name
}

async changed(id: RundownBaselineAdLibActionId, changeType: string): Promise<void> {
this.logDocumentChange(id, changeType)
if (!this._collectionName) return
const col = this._core.getCollection(this._collectionName)
if (!col) throw new Error(`collection '${this._collectionName}' not found!`)
this._collectionData = col.find({ rundownId: this._currentRundownId })
await this.notify(this._collectionData)
init(handlers: CollectionHandlers): void {
super.init(handlers)

handlers.playlistHandler.subscribe(this.onPlaylistUpdate, PLAYLIST_KEYS)
}

changed(): void {
this.updateAndNotify()
}

async update(source: string, data: SelectedPartInstances | undefined): Promise<void> {
this.logUpdateReceived('partInstances', source)
private onPlaylistUpdate = (data: Playlist | undefined): void => {
this.logUpdateReceived('playlist')
const prevRundownId = this._currentRundownId
const partInstance = data ? data.current ?? data.next : undefined
this._currentRundownId = partInstance?.rundownId
const rundownPlaylist = data

this._currentRundownId = rundownPlaylist?.currentPartInfo?.rundownId ?? rundownPlaylist?.nextPartInfo?.rundownId

await new Promise(process.nextTick.bind(this))
if (!this._collectionName) return
if (!this._publicationName) return
if (prevRundownId !== this._currentRundownId) {
if (this._subscriptionId) this._coreHandler.unsubscribe(this._subscriptionId)
if (this._dbObserver) this._dbObserver.stop()
this.stopSubscription()
if (this._currentRundownId) {
this._subscriptionId = await this._coreHandler.setupSubscription(this._publicationName, [
this._currentRundownId,
])
this._dbObserver = this._coreHandler.setupObserver(this._collectionName)
this._dbObserver.added = (id) => {
void this.changed(id, 'added').catch(this._logger.error)
}
this._dbObserver.changed = (id) => {
void this.changed(id, 'changed').catch(this._logger.error)
}
this._dbObserver.removed = (id) => {
void this.changed(id, 'removed').catch(this._logger.error)
}

const collection = this._core.getCollection(this._collectionName)
if (!collection) throw new Error(`collection '${this._collectionName}' not found!`)
this._collectionData = collection.find({ rundownId: this._currentRundownId })
await this.notify(this._collectionData)
this.setupSubscription([this._currentRundownId])
}
}
}

// override notify to implement empty array handling
async notify(data: RundownBaselineAdLibAction[] | undefined): Promise<void> {
this.logNotifyingUpdate(data?.length)
if (data !== undefined) {
for (const observer of this._observers) {
await observer.update(this._name, data)
}
}
protected updateAndNotify(): void {
const collection = this.getCollectionOrFail()
this._collectionData = collection.find({ rundownId: this._currentRundownId })
this.notify(this._collectionData)
}
}
Loading
Loading