Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Jan 12, 2024
1 parent 312e31e commit 26586a3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
19 changes: 12 additions & 7 deletions packages/apps/backend/src/api-server/ApiServer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { RealTimeConnection, feathers } from '@feathersjs/feathers'
import { koa, rest, bodyParser, errorHandler, serveStatic, cors } from '@feathersjs/koa'
import { koa, rest, bodyParser, errorHandler, serveStatic, cors, Application } from '@feathersjs/koa'
import socketio from '@feathersjs/socketio'
import { EventEmitter } from 'eventemitter3'
import { ServiceTypes } from '@sofie-prompter-editor/shared-model'
import { RundownPlaylistId, ServiceTypes } from '@sofie-prompter-editor/shared-model'
import { LoggerInstance } from '../lib/logger.js'
import { PublishChannels } from './PublishChannels.js'
import { PlaylistFeathersService, PlaylistService } from './services/PlaylistService.js'
Expand Down Expand Up @@ -76,11 +76,7 @@ export class ApiServer extends EventEmitter<ApiServerEvents> {

if (this.coreConnection) {
for (const playlistId of this.coreConnection.getSubscribedPlaylists()) {
const subscriberCount = this.app.channel(PublishChannels.RundownsInPlaylist(playlistId)).length
if (subscriberCount === 0) {
// Noone is listening to this playlist, so unsubscribe from it:
this.coreConnection.unsubscribeFromPlaylist(playlistId)
}
this.unsubscribeFromPlaylistIfNoOneIsListening(playlistId, this.app)
}
}
})
Expand All @@ -99,4 +95,13 @@ export class ApiServer extends EventEmitter<ApiServerEvents> {
.catch(reject)
})
}
public unsubscribeFromPlaylistIfNoOneIsListening(playlistId: RundownPlaylistId, app: Application<ServiceTypes, any>) {
// Check if no one is subscribed to this playlist and unsubscribe from it if so:

const subscriberCount = app.channel(PublishChannels.RundownsInPlaylist(playlistId)).length
if (subscriberCount === 0) {
// No one is listening to this playlist, so unsubscribe from it:
this.coreConnection.unsubscribeFromPlaylist(playlistId)
}
}
}
10 changes: 5 additions & 5 deletions packages/apps/backend/src/api-server/services/RundownService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,16 @@ export class RundownService extends EventEmitter<Definition.Events> implements D
}

public async subscribeToRundownsInPlaylist(playlistId: RundownPlaylistId, params: Params): Promise<void> {
if (!params.connection) throw new Error('No connection!')
this.coreConnection?.subscribeToPlaylist(playlistId)

if (!params.connection) throw new Error('No connection!')
this.app.channel(PublishChannels.RundownsInPlaylist(playlistId)).join(params.connection)
this.coreConnection?.subscribeToPlaylist(playlistId)
}
public async unSubscribefromRundownsInPlaylist(playlistId: RundownPlaylistId, params: Params): Promise<void> {
if (!params.connection) throw new Error('No connection!')
public async unSubscribeFromRundownsInPlaylist(playlistId: RundownPlaylistId, params: Params): Promise<void> {
this.coreConnection?.unsubscribeFromPlaylistIfNoOneIsListening(playlistId)

if (!params.connection) throw new Error('No connection!')
this.app.channel(PublishChannels.RundownsInPlaylist(playlistId)).leave(params.connection)
this.coreConnection?.subscribeToPlaylist(playlistId)
}
}
type Result = Definition.Result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
JSONBlobStringify,
StatusCode,
} from '@sofie-automation/server-core-integration'
import { RundownPlaylistId } from '@sofie-prompter-editor/shared-model'
import { RundownPlaylistId, ServiceTypes } from '@sofie-prompter-editor/shared-model'
import {
PeripheralDeviceCategory,
PeripheralDeviceType,
Expand All @@ -27,6 +27,8 @@ import { PartHandler } from './dataHandlers/PartHandler.js'
import { Transformers } from './dataTransformers/Transformers.js'
import { PieceHandler } from './dataHandlers/PieceHandler.js'
import { ShowStyleBaseHandler } from './dataHandlers/ShowStyleBaseHandler.js'
import { PublishChannels } from '../api-server/PublishChannels.js'
import { Application } from '@feathersjs/koa'

interface SofieCoreConnectionEvents {
connected: []
Expand Down Expand Up @@ -132,7 +134,8 @@ export class SofieCoreConnection extends EventEmitter<SofieCoreConnectionEvents>
return this.subscriberManager.getSubscribedPlaylists()
}
public unsubscribeFromPlaylist(playlistId: RundownPlaylistId) {
// Remove connection from all subscriptions
// Remove subscription to playlist.
// Note: Only call this is no one is subscribed!
this.subscriberManager.unsubscribeFromPlaylist(playlistId)
}
private setStatus(id: string, status: StatusCode, message: string): void {
Expand Down
2 changes: 2 additions & 0 deletions packages/apps/client/src/model/UIRundown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ export class UIRundown {

close(): void {
this.store.openRundown = null

this.store.connection.rundown.unSubscribeFromRundownsInPlaylist(this.id).catch(console.error)
this.dispose()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const ALL_METHODS = [
'remove',
//
'subscribeToRundownsInPlaylist',
'unSubscribeFromRundownsInPlaylist',
] as const
/** The methods exposed by this class are exposed in the API */
interface Methods extends Omit<ServiceMethods, 'patch'> {
Expand All @@ -36,7 +37,7 @@ interface Methods extends Omit<ServiceMethods, 'patch'> {
/** Subscribe to all info within a specific playlist */
subscribeToRundownsInPlaylist(playlistId: RundownPlaylistId, params?: Params): Promise<void>

unSubscribefromRundownsInPlaylist(playlistId: RundownPlaylistId, params?: Params): Promise<void>
unSubscribeFromRundownsInPlaylist(playlistId: RundownPlaylistId, params?: Params): Promise<void>
}
export interface Service extends Methods, EventEmitter<Events> {}

Expand Down

0 comments on commit 26586a3

Please sign in to comment.