Skip to content

Commit 519f254

Browse files
committed
wip: something
1 parent f681c79 commit 519f254

File tree

12 files changed

+591
-366
lines changed

12 files changed

+591
-366
lines changed

meteor/server/api/deviceTriggers/StudioDeviceTriggerManager.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import { SomeAction, SomeBlueprintTrigger } from '@sofie-automation/blueprints-i
3030
import { DeviceActions } from '@sofie-automation/shared-lib/dist/core/model/ShowStyle'
3131
import { DummyReactiveVar } from '@sofie-automation/meteor-lib/dist/triggers/reactive-var'
3232
import { MeteorTriggersContext } from './triggersContext'
33+
import { Tracker } from 'meteor/tracker'
34+
import { TriggerTrackerComputation } from '@sofie-automation/meteor-lib/dist/triggers/triggersContext'
3335

3436
export class StudioDeviceTriggerManager {
3537
#lastShowStyleBaseId: ShowStyleBaseId | null = null
@@ -43,7 +45,11 @@ export class StudioDeviceTriggerManager {
4345
StudioActionManagers.set(studioId, new StudioActionManager())
4446
}
4547

46-
updateTriggers(cache: ContentCache, showStyleBaseId: ShowStyleBaseId): void {
48+
async updateTriggers(
49+
cache: ContentCache,
50+
showStyleBaseId: ShowStyleBaseId,
51+
computation: Tracker.Computation | null
52+
): Promise<void> {
4753
const studioId = this.studioId
4854
this.#lastShowStyleBaseId = showStyleBaseId
4955

@@ -88,7 +94,7 @@ export class StudioDeviceTriggerManager {
8894

8995
const addedPreviewIds: PreviewWrappedAdLibId[] = []
9096

91-
Object.entries<SomeAction>(triggeredAction.actions).forEach(([key, action]) => {
97+
for (const [key, action] of Object.entries<SomeAction>(triggeredAction.actions)) {
9298
// Since the compiled action is cached using this actionId as a key, having the action
9399
// and the filterChain allows for a quicker invalidation without doing a deepEquals
94100
const actionId = protectString<DeviceActionId>(
@@ -106,7 +112,7 @@ export class StudioDeviceTriggerManager {
106112
}
107113
touchedActionIds.push(actionId)
108114

109-
Object.entries<SomeBlueprintTrigger>(triggeredAction.triggers).forEach(([key, trigger]) => {
115+
for (const [key, trigger] of Object.entries<SomeBlueprintTrigger>(triggeredAction.triggers)) {
110116
if (!isDeviceTrigger(trigger)) {
111117
return
112118
}
@@ -141,7 +147,7 @@ export class StudioDeviceTriggerManager {
141147
},
142148
})
143149
upsertedDeviceTriggerMountedActionIds.push(deviceTriggerMountedActionId)
144-
})
150+
}
145151

146152
if (!isPreviewableAction(thisAction)) {
147153
const adLibPreviewId = protectString(`${actionId}_preview`)
@@ -165,7 +171,10 @@ export class StudioDeviceTriggerManager {
165171

166172
addedPreviewIds.push(adLibPreviewId)
167173
} else {
168-
const previewedAdLibs = thisAction.preview(context)
174+
const previewedAdLibs = await thisAction.preview(
175+
context,
176+
computation as any as TriggerTrackerComputation | null
177+
)
169178

170179
previewedAdLibs.forEach((adLib) => {
171180
const adLibPreviewId = protectString<PreviewWrappedAdLibId>(
@@ -195,7 +204,7 @@ export class StudioDeviceTriggerManager {
195204
addedPreviewIds.push(adLibPreviewId)
196205
})
197206
}
198-
})
207+
}
199208

200209
DeviceTriggerMountedActionAdlibsPreview.remove({
201210
triggeredActionId: triggeredAction._id,

meteor/server/api/deviceTriggers/observer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Meteor.startup(() => {
4444
const manager = new StudioDeviceTriggerManager(studioId)
4545
const observer = new StudioObserver(studioId, (showStyleBaseId, cache) => {
4646
workInQueue(async () => {
47-
manager.updateTriggers(cache, showStyleBaseId)
47+
await manager.updateTriggers(cache, showStyleBaseId, null) // nocommit - confirm this
4848
})
4949

5050
return () => {

meteor/server/api/deviceTriggers/triggersContext.ts

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { TriggersContext } from '@sofie-automation/meteor-lib/dist/triggers/triggersContext'
1+
import {
2+
TriggersAsyncCollection,
3+
TriggersContext,
4+
TriggerTrackerComputation,
5+
} from '@sofie-automation/meteor-lib/dist/triggers/triggersContext'
26
import { SINGLE_USE_TOKEN_SALT } from '@sofie-automation/meteor-lib/dist/api/userActions'
3-
import { assertNever, getHash, Time } from '../../lib/tempLib'
7+
import { assertNever, getHash, ProtectedString, Time } from '../../lib/tempLib'
48
import { getCurrentTime } from '../../lib/lib'
59
import { MeteorCall } from '../methods'
610
import { ClientAPI } from '@sofie-automation/meteor-lib/dist/api/client'
@@ -28,26 +32,57 @@ import {
2832
} from '../../collections'
2933
import { DBPart } from '@sofie-automation/corelib/dist/dataModel/Part'
3034
import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance'
35+
import { AsyncOnlyReadOnlyMongoCollection } from '../../collections/collection'
3136

3237
export function hashSingleUseToken(token: string): string {
3338
return getHash(SINGLE_USE_TOKEN_SALT + token)
3439
}
3540

41+
class TriggersCollection2<DBInterface extends { _id: ProtectedString<any> }>
42+
implements TriggersAsyncCollection<DBInterface>
43+
{
44+
readonly #collection: AsyncOnlyReadOnlyMongoCollection<DBInterface>
45+
46+
constructor(collection: AsyncOnlyReadOnlyMongoCollection<DBInterface>) {
47+
this.#collection = collection
48+
}
49+
50+
async findFetchAsync(
51+
computation: TriggerTrackerComputation | null,
52+
selector: any,
53+
options?: any
54+
): Promise<Array<DBInterface>> {
55+
return Tracker.withComputation(computation as Tracker.Computation | null, async () => {
56+
return this.#collection.findFetchAsync(selector, options)
57+
})
58+
}
59+
60+
async findOneAsync(
61+
computation: TriggerTrackerComputation | null,
62+
selector: any,
63+
options?: any
64+
): Promise<DBInterface | undefined> {
65+
return Tracker.withComputation(computation as Tracker.Computation | null, async () => {
66+
return this.#collection.findOneAsync(selector, options)
67+
})
68+
}
69+
}
70+
3671
export const MeteorTriggersContext: TriggersContext = {
3772
MeteorCall,
3873

3974
logger,
4075

4176
isClient: false,
4277

43-
AdLibActions,
44-
AdLibPieces,
45-
Parts,
46-
RundownBaselineAdLibActions,
47-
RundownBaselineAdLibPieces,
48-
RundownPlaylists,
49-
Rundowns,
50-
Segments,
78+
AdLibActions: new TriggersCollection2(AdLibActions),
79+
AdLibPieces: new TriggersCollection2(AdLibPieces),
80+
Parts: new TriggersCollection2(Parts),
81+
RundownBaselineAdLibActions: new TriggersCollection2(RundownBaselineAdLibActions),
82+
RundownBaselineAdLibPieces: new TriggersCollection2(RundownBaselineAdLibPieces),
83+
RundownPlaylists: new TriggersCollection2(RundownPlaylists),
84+
Rundowns: new TriggersCollection2(Rundowns),
85+
Segments: new TriggersCollection2(Segments),
5186

5287
hashSingleUseToken,
5388

@@ -67,14 +102,17 @@ export const MeteorTriggersContext: TriggersContext = {
67102
)
68103
},
69104

70-
nonreactiveTracker: Tracker.nonreactive,
105+
withComputation: async (computation, func) => {
106+
return Tracker.withComputation(computation as Tracker.Computation | null, func)
107+
},
71108

72109
memoizedIsolatedAutorun: async <TArgs extends any[], TRes>(
73-
fnc: (...args: TArgs) => Promise<TRes>,
110+
computation: TriggerTrackerComputation | null,
111+
fnc: (computation: TriggerTrackerComputation | null, ...args: TArgs) => Promise<TRes>,
74112
_functionName: string,
75113
...params: TArgs
76114
): Promise<TRes> => {
77-
return fnc(...params)
115+
return fnc(computation, ...params)
78116
},
79117

80118
createContextForRundownPlaylistChain,

0 commit comments

Comments
 (0)