|
| 1 | +import { PeripheralDeviceId, RundownId, RundownPlaylistId } from '@sofie-automation/corelib/dist/dataModel/Ids' |
| 2 | +import { ReadonlyDeep } from 'type-fest' |
| 3 | +import { |
| 4 | + CustomPublishCollection, |
| 5 | + meteorCustomPublish, |
| 6 | + setUpCollectionOptimizedObserver, |
| 7 | + SetupObserversResult, |
| 8 | + TriggerUpdate, |
| 9 | +} from '../../lib/customPublication' |
| 10 | +import { logger } from '../../logging' |
| 11 | +import { ContentCache, createReactiveContentCache } from './reactiveContentCache' |
| 12 | +import { RundownsObserver } from '../lib/rundownsObserver' |
| 13 | +import { RundownContentObserver } from './rundownContentObserver' |
| 14 | +import { |
| 15 | + PeripheralDevicePubSub, |
| 16 | + PeripheralDevicePubSubCollectionsNames, |
| 17 | +} from '@sofie-automation/shared-lib/dist/pubsub/peripheralDevice' |
| 18 | +import { checkAccessAndGetPeripheralDevice } from '../../security/check' |
| 19 | +import { check } from '../../lib/check' |
| 20 | +import { IngestRundownStatus } from '@sofie-automation/shared-lib/dist/ingest/rundownStatus' |
| 21 | +import { protectString } from '@sofie-automation/corelib/dist/protectedString' |
| 22 | +import { DBRundown } from '@sofie-automation/corelib/dist/dataModel/Rundown' |
| 23 | +import { createIngestRundownStatus } from './createIngestRundownStatus' |
| 24 | + |
| 25 | +interface IngestRundownStatusArgs { |
| 26 | + readonly deviceId: PeripheralDeviceId |
| 27 | +} |
| 28 | + |
| 29 | +export interface IngestRundownStatusState { |
| 30 | + contentCache: ReadonlyDeep<ContentCache> |
| 31 | +} |
| 32 | + |
| 33 | +interface IngestRundownStatusUpdateProps { |
| 34 | + newCache: ContentCache |
| 35 | + |
| 36 | + invalidateRundownIds: RundownId[] |
| 37 | + invalidatePlaylistIds: RundownPlaylistId[] |
| 38 | +} |
| 39 | + |
| 40 | +async function setupIngestRundownStatusPublicationObservers( |
| 41 | + args: ReadonlyDeep<IngestRundownStatusArgs>, |
| 42 | + triggerUpdate: TriggerUpdate<IngestRundownStatusUpdateProps> |
| 43 | +): Promise<SetupObserversResult> { |
| 44 | + const rundownsObserver = await RundownsObserver.createForPeripheralDevice(args.deviceId, async (rundownIds) => { |
| 45 | + logger.silly(`Creating new RundownContentObserver`, rundownIds) |
| 46 | + |
| 47 | + // TODO - can this be done cheaper? |
| 48 | + const cache = createReactiveContentCache(rundownIds) |
| 49 | + |
| 50 | + // Push update |
| 51 | + triggerUpdate({ newCache: cache }) |
| 52 | + |
| 53 | + const contentObserver = await RundownContentObserver.create(rundownIds, cache) |
| 54 | + |
| 55 | + const innerQueries = [ |
| 56 | + cache.Playlists.find({}).observeChanges( |
| 57 | + { |
| 58 | + added: (docId) => triggerUpdate({ invalidatePlaylistIds: [protectString(docId)] }), |
| 59 | + changed: (docId) => triggerUpdate({ invalidatePlaylistIds: [protectString(docId)] }), |
| 60 | + removed: (docId) => triggerUpdate({ invalidatePlaylistIds: [protectString(docId)] }), |
| 61 | + }, |
| 62 | + { nonMutatingCallbacks: true } |
| 63 | + ), |
| 64 | + cache.Rundowns.find({}).observeChanges( |
| 65 | + { |
| 66 | + added: (docId) => { |
| 67 | + triggerUpdate({ invalidateRundownIds: [protectString(docId)] }) |
| 68 | + contentObserver.checkPlaylistIds() |
| 69 | + }, |
| 70 | + changed: (docId) => { |
| 71 | + triggerUpdate({ invalidateRundownIds: [protectString(docId)] }) |
| 72 | + contentObserver.checkPlaylistIds() |
| 73 | + }, |
| 74 | + removed: (docId) => { |
| 75 | + triggerUpdate({ invalidateRundownIds: [protectString(docId)] }) |
| 76 | + contentObserver.checkPlaylistIds() |
| 77 | + }, |
| 78 | + }, |
| 79 | + { nonMutatingCallbacks: true } |
| 80 | + ), |
| 81 | + cache.Parts.find({}).observe({ |
| 82 | + added: (doc) => triggerUpdate({ invalidateRundownIds: [doc.rundownId] }), |
| 83 | + changed: (doc, oldDoc) => triggerUpdate({ invalidateRundownIds: [doc.rundownId, oldDoc.rundownId] }), |
| 84 | + removed: (doc) => triggerUpdate({ invalidateRundownIds: [doc.rundownId] }), |
| 85 | + }), |
| 86 | + cache.PartInstances.find({}).observe({ |
| 87 | + added: (doc) => triggerUpdate({ invalidateRundownIds: [doc.rundownId] }), |
| 88 | + changed: (doc, oldDoc) => triggerUpdate({ invalidateRundownIds: [doc.rundownId, oldDoc.rundownId] }), |
| 89 | + removed: (doc) => triggerUpdate({ invalidateRundownIds: [doc.rundownId] }), |
| 90 | + }), |
| 91 | + cache.NrcsIngestData.find({}).observe({ |
| 92 | + added: (doc) => triggerUpdate({ invalidateRundownIds: [doc.rundownId] }), |
| 93 | + changed: (doc, oldDoc) => triggerUpdate({ invalidateRundownIds: [doc.rundownId, oldDoc.rundownId] }), |
| 94 | + removed: (doc) => triggerUpdate({ invalidateRundownIds: [doc.rundownId] }), |
| 95 | + }), |
| 96 | + ] |
| 97 | + |
| 98 | + return () => { |
| 99 | + contentObserver.dispose() |
| 100 | + |
| 101 | + for (const query of innerQueries) { |
| 102 | + query.stop() |
| 103 | + } |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + // Set up observers: |
| 108 | + return [rundownsObserver] |
| 109 | +} |
| 110 | + |
| 111 | +async function manipulateIngestRundownStatusPublicationData( |
| 112 | + _args: IngestRundownStatusArgs, |
| 113 | + state: Partial<IngestRundownStatusState>, |
| 114 | + collection: CustomPublishCollection<IngestRundownStatus>, |
| 115 | + updateProps: Partial<ReadonlyDeep<IngestRundownStatusUpdateProps>> | undefined |
| 116 | +): Promise<void> { |
| 117 | + // Prepare data for publication: |
| 118 | + |
| 119 | + if (updateProps?.newCache !== undefined) { |
| 120 | + state.contentCache = updateProps.newCache ?? undefined |
| 121 | + } |
| 122 | + |
| 123 | + if (!state.contentCache) { |
| 124 | + // Remove all the notes |
| 125 | + collection.remove(null) |
| 126 | + |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + const updateAll = !updateProps || !!updateProps?.newCache |
| 131 | + if (updateAll) { |
| 132 | + // Remove all the notes |
| 133 | + collection.remove(null) |
| 134 | + |
| 135 | + const knownRundownIds = new Set(state.contentCache.RundownIds) |
| 136 | + |
| 137 | + for (const rundownId of knownRundownIds) { |
| 138 | + const newDoc = createIngestRundownStatus(state.contentCache, rundownId) |
| 139 | + if (newDoc) collection.replace(newDoc) |
| 140 | + } |
| 141 | + } else { |
| 142 | + const regenerateForRundownIds = new Set(updateProps.invalidateRundownIds) |
| 143 | + |
| 144 | + // Include anything where the playlist has changed |
| 145 | + if (updateProps.invalidatePlaylistIds && updateProps.invalidatePlaylistIds.length > 0) { |
| 146 | + const rundownsToUpdate = state.contentCache.Rundowns.find( |
| 147 | + { |
| 148 | + playlistId: { $in: updateProps.invalidatePlaylistIds }, |
| 149 | + }, |
| 150 | + { |
| 151 | + projection: { |
| 152 | + _id: 1, |
| 153 | + }, |
| 154 | + } |
| 155 | + ).fetch() as Pick<DBRundown, '_id'>[] |
| 156 | + |
| 157 | + for (const rundown of rundownsToUpdate) { |
| 158 | + regenerateForRundownIds.add(rundown._id) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + for (const rundownId of regenerateForRundownIds) { |
| 163 | + const newDoc = createIngestRundownStatus(state.contentCache, rundownId) |
| 164 | + if (newDoc) { |
| 165 | + collection.replace(newDoc) |
| 166 | + } else { |
| 167 | + collection.remove(rundownId) |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +meteorCustomPublish( |
| 174 | + PeripheralDevicePubSub.ingestDeviceRundownStatus, |
| 175 | + PeripheralDevicePubSubCollectionsNames.ingestRundownStatus, |
| 176 | + async function (pub, deviceId: PeripheralDeviceId, token: string | undefined) { |
| 177 | + check(deviceId, String) |
| 178 | + |
| 179 | + await checkAccessAndGetPeripheralDevice(deviceId, token, this) |
| 180 | + |
| 181 | + await setUpCollectionOptimizedObserver< |
| 182 | + IngestRundownStatus, |
| 183 | + IngestRundownStatusArgs, |
| 184 | + IngestRundownStatusState, |
| 185 | + IngestRundownStatusUpdateProps |
| 186 | + >( |
| 187 | + `pub_${PeripheralDevicePubSub.ingestDeviceRundownStatus}_${deviceId}`, |
| 188 | + { deviceId }, |
| 189 | + setupIngestRundownStatusPublicationObservers, |
| 190 | + manipulateIngestRundownStatusPublicationData, |
| 191 | + pub, |
| 192 | + 100 |
| 193 | + ) |
| 194 | + } |
| 195 | +) |
0 commit comments