11import { Meteor } from 'meteor/meteor'
22import { RundownId , RundownPlaylistId , StudioId } from '@sofie-automation/corelib/dist/dataModel/Ids'
3- import _ from 'underscore'
43import { Rundowns } from '../../collections'
4+ import { PromiseDebounce } from './debounce'
55
66const REACTIVITY_DEBOUNCE = 20
77
@@ -12,30 +12,53 @@ type ChangedHandler = (rundownIds: RundownId[]) => Promise<() => void>
1212 * Note: Updates are debounced to avoid rapid updates firing
1313 */
1414export class RundownsObserver implements Meteor . LiveQueryHandle {
15- #rundownsLiveQuery: Meteor . LiveQueryHandle
15+ #rundownsLiveQuery! : Meteor . LiveQueryHandle
1616 #rundownIds: Set < RundownId > = new Set < RundownId > ( )
1717 #changed: ChangedHandler | undefined
1818 #cleanup: ( ( ) => void ) | undefined
1919
20- constructor ( studioId : StudioId , playlistId : RundownPlaylistId , onChanged : ChangedHandler ) {
20+ readonly #triggerUpdateRundownContent = new PromiseDebounce < void > ( async ( ) => {
21+ if ( ! this . #changed) return
22+ this . #cleanup?.( )
23+
24+ const changed = this . #changed
25+ this . #cleanup = await changed ( this . rundownIds )
26+ } , REACTIVITY_DEBOUNCE )
27+
28+ private constructor ( onChanged : ChangedHandler ) {
2129 this . #changed = onChanged
22- this . #rundownsLiveQuery = Rundowns . observe (
30+ }
31+
32+ static async create (
33+ studioId : StudioId ,
34+ playlistId : RundownPlaylistId ,
35+ onChanged : ChangedHandler
36+ ) : Promise < RundownsObserver > {
37+ const observer = new RundownsObserver ( onChanged )
38+
39+ await observer . init ( studioId , playlistId )
40+
41+ return observer
42+ }
43+
44+ private async init ( studioId : StudioId , playlistId : RundownPlaylistId ) {
45+ this . #rundownsLiveQuery = await Rundowns . observe (
2346 {
2447 playlistId,
2548 studioId,
2649 } ,
2750 {
2851 added : ( doc ) => {
2952 this . #rundownIds. add ( doc . _id )
30- this . triggerUpdateRundownContent ( )
53+ this . # triggerUpdateRundownContent. trigger ( )
3154 } ,
3255 changed : ( doc ) => {
3356 this . #rundownIds. add ( doc . _id )
34- this . triggerUpdateRundownContent ( )
57+ this . # triggerUpdateRundownContent. trigger ( )
3558 } ,
3659 removed : ( doc ) => {
3760 this . #rundownIds. delete ( doc . _id )
38- this . triggerUpdateRundownContent ( )
61+ this . # triggerUpdateRundownContent. trigger ( )
3962 } ,
4063 } ,
4164 {
@@ -44,28 +67,16 @@ export class RundownsObserver implements Meteor.LiveQueryHandle {
4467 } ,
4568 }
4669 )
47- this . triggerUpdateRundownContent ( )
70+
71+ this . #triggerUpdateRundownContent. trigger ( )
4872 }
4973
5074 public get rundownIds ( ) : RundownId [ ] {
5175 return Array . from ( this . #rundownIds)
5276 }
5377
54- private innerUpdateRundownContent = ( ) => {
55- if ( ! this . #changed) return
56- this . #cleanup?.( )
57-
58- const changed = this . #changed
59- this . #cleanup = changed ( this . rundownIds )
60- }
61-
62- private triggerUpdateRundownContent = _ . debounce (
63- Meteor . bindEnvironment ( this . innerUpdateRundownContent ) ,
64- REACTIVITY_DEBOUNCE
65- )
66-
6778 public stop = ( ) : void => {
68- this . triggerUpdateRundownContent . cancel ( )
79+ this . # triggerUpdateRundownContent. cancelWaiting ( )
6980 this . #rundownsLiveQuery. stop ( )
7081 this . #changed = undefined
7182 this . #cleanup?.( )
0 commit comments