|
1 | 1 | import { Logger } from 'winston' |
2 | 2 | import { CoreHandler } from '../coreHandler' |
3 | | -import { CollectionBase, Collection, CollectionObserver } from '../wsHandler' |
| 3 | +import { Collection, PickArr, PublicationCollection } from '../wsHandler' |
4 | 4 | import { AdLibAction } from '@sofie-automation/corelib/dist/dataModel/AdlibAction' |
5 | | -import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance' |
6 | 5 | import { CollectionName } from '@sofie-automation/corelib/dist/dataModel/Collections' |
7 | 6 | 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> |
10 | 13 |
|
11 | 14 | 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[]> |
14 | 17 | { |
15 | | - public observerName: string |
16 | | - private _curRundownId: RundownId | undefined |
17 | | - private _curPartInstance: DBPartInstance | undefined |
| 18 | + private _currentRundownId: RundownId | undefined |
18 | 19 |
|
19 | 20 | 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) |
22 | 28 | } |
23 | 29 |
|
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() |
31 | 32 | } |
32 | 33 |
|
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 |
38 | 37 |
|
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 |
59 | 39 |
|
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]) |
66 | 46 | } |
| 47 | + // no need to trigger updateAndNotify() because the subscription will take care of this |
67 | 48 | } |
68 | 49 | } |
69 | 50 |
|
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) |
78 | 55 | } |
79 | 56 | } |
0 commit comments