forked from Sofie-Automation/sofie-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayoutContext.ts
More file actions
138 lines (124 loc) · 4.37 KB
/
playoutContext.ts
File metadata and controls
138 lines (124 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { PeripheralDeviceId, StudioId } from '@sofie-automation/corelib/dist/dataModel/Ids'
import { DBRundown } from '@sofie-automation/corelib/dist/dataModel/Rundown'
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist/RundownPlaylist'
import { literal } from '@sofie-automation/corelib/dist/lib'
import { MongoFieldSpecifierOnesStrict } from '@sofie-automation/corelib/dist/mongo'
import { PackageManagerPlayoutContext } from '@sofie-automation/shared-lib/dist/package-manager/publications'
import { check } from 'meteor/check'
import { ReadonlyDeep } from 'type-fest'
import { RundownPlaylists, Rundowns } from '../../collections'
import {
meteorCustomPublish,
SetupObserversResult,
setUpOptimizedObserverArray,
TriggerUpdate,
} from '../../lib/customPublication'
import { logger } from '../../logging'
import {
PeripheralDevicePubSub,
PeripheralDevicePubSubCollectionsNames,
} from '@sofie-automation/shared-lib/dist/pubsub/peripheralDevice'
import { checkAccessAndGetPeripheralDevice } from '../../security/check'
export type RundownPlaylistCompact = Pick<DBRundownPlaylist, '_id' | 'activationId' | 'rehearsal' | 'rundownIdsInOrder'>
const rundownPlaylistFieldSpecifier = literal<MongoFieldSpecifierOnesStrict<RundownPlaylistCompact>>({
_id: 1,
activationId: 1,
rehearsal: 1,
rundownIdsInOrder: 1,
})
interface PackageManagerPlayoutContextArgs {
readonly studioId: StudioId
readonly deviceId: PeripheralDeviceId
}
type PackageManagerPlayoutContextUpdateProps = Record<string, never>
type PackageManagerPlayoutContextState = Record<string, never>
async function setupExpectedPackagesPublicationObservers(
args: ReadonlyDeep<PackageManagerPlayoutContextArgs>,
triggerUpdate: TriggerUpdate<PackageManagerPlayoutContextUpdateProps>
): Promise<SetupObserversResult> {
// Set up observers:
return [
RundownPlaylists.observeChanges(
{
studioId: args.studioId,
},
{
added: () => triggerUpdate({}),
changed: () => triggerUpdate({}),
removed: () => triggerUpdate({}),
},
{
projection: rundownPlaylistFieldSpecifier,
}
),
]
}
async function manipulateExpectedPackagesPublicationData(
args: ReadonlyDeep<PackageManagerPlayoutContextArgs>,
_state: Partial<PackageManagerPlayoutContextState>,
_updateProps: Partial<PackageManagerPlayoutContextUpdateProps> | undefined
): Promise<PackageManagerPlayoutContext[] | null> {
// Prepare data for publication:
// Future: this may want to cache on the state, but with only a single observer there feels little point
const activePlaylist = (await RundownPlaylists.findOneAsync(
{
studioId: args.studioId,
activationId: { $exists: true },
},
{ projection: rundownPlaylistFieldSpecifier }
)) as RundownPlaylistCompact | undefined
const activeRundowns = activePlaylist
? ((await Rundowns.findFetchAsync(
{
playlistId: activePlaylist._id,
},
{
projection: { _id: 1 },
}
)) as Pick<DBRundown, '_id'>[])
: []
return literal<PackageManagerPlayoutContext[]>([
{
_id: args.deviceId,
activePlaylist: activePlaylist
? {
_id: activePlaylist._id,
active: !!activePlaylist.activationId,
rehearsal: !!activePlaylist.rehearsal,
}
: null,
activeRundowns: activeRundowns.map((rundown) => {
return {
_id: rundown._id,
_rank: activePlaylist?.rundownIdsInOrder?.indexOf(rundown._id) ?? 0,
}
}),
},
])
}
meteorCustomPublish(
PeripheralDevicePubSub.packageManagerPlayoutContext,
PeripheralDevicePubSubCollectionsNames.packageManagerPlayoutContext,
async function (pub, deviceId: PeripheralDeviceId, token: string | undefined) {
check(deviceId, String)
const peripheralDevice = await checkAccessAndGetPeripheralDevice(deviceId, token, this)
const studioId = peripheralDevice.studioAndConfigId?.studioId
if (!studioId) {
logger.warn(`Pub.packageManagerPlayoutContext: device "${peripheralDevice._id}" has no studioId`)
return this.ready()
}
await setUpOptimizedObserverArray<
PackageManagerPlayoutContext,
PackageManagerPlayoutContextArgs,
PackageManagerPlayoutContextState,
PackageManagerPlayoutContextUpdateProps
>(
`${PeripheralDevicePubSub.packageManagerPlayoutContext}_${studioId}_${deviceId}`,
{ studioId, deviceId },
setupExpectedPackagesPublicationObservers,
manipulateExpectedPackagesPublicationData,
pub,
500 // ms, wait this time before sending an update
)
}
)