Skip to content

Commit

Permalink
Move the trigger of item scheduling to the main thread and add an ind…
Browse files Browse the repository at this point in the history
…icator that an item is scheduled to the rundown

Signed-off-by: Axel Boberg <[email protected]>
  • Loading branch information
axelboberg committed Mar 30, 2024
1 parent b813aec commit 26af459
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
4 changes: 1 addition & 3 deletions api/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,8 @@ async function playItem (id) {
const delay = parseInt(clone?.data?.delay)

if (delay && !Number.isNaN(delay)) {
commands.executeCommand('scheduler.delay', `play:${id}`, delay, 'items.playItem', clone)
commands.executeCommand('items.scheduleItem', clone, delay)
} else {
commands.executeCommand('scheduler.abort', `play:${id}`)
commands.executeCommand('items.playItem', clone)
}
}
Expand All @@ -239,7 +238,6 @@ exports.playItem = playItem
* @param { String } id
*/
async function stopItem (id) {
commands.executeCommand('scheduler.abort', `play:${id}`)
commands.executeCommand('items.stopItem', id)
}
exports.stopItem = stopItem
41 changes: 39 additions & 2 deletions lib/api/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,63 @@ function factory (api, workspace) {
* @param { Item } item
*/
function playItem (item) {
if (!item.id) {
if (!item?.id) {
throw new ApiError('Invalid item object', 'ERR_API_ITEMS_INVALID_ITEM')
}

api.commands.executeCommand('scheduler.abort', undefined, `play:${item.id}`)

workspace.state.apply({
items: {
[item.id]: {
state: 'playing',
didStartPlayingAt: Date.now()
didStartPlayingAt: Date.now(),
willStartPlayingAt: Date.now()
}
}
})

api.events.emit('item.play', item)
}

/**
* Schedule an item to be
* played after a certain delay
*
* @param { Item } itemId
* @param { Number } delay
*/
function scheduleItem (item, delay) {
if (!item?.id) {
throw new ApiError('Invalid item object', 'ERR_API_ITEMS_INVALID_ITEM')
}

if (delay == null) {
playItem(item)
return
}

workspace.state.apply({
items: {
[item.id]: {
state: 'scheduled',
wasScheduledAt: Date.now(),
willStartPlayingAt: Date.now() + delay
}
}
})

api.commands.executeCommand('scheduler.delay', undefined, `play:${item.id}`, delay, 'items.playItem', item)
api.events.emit('item.schedule', item)
}

/**
* Stop an item by its id
* @param { String } id
*/
function stopItem (id) {
api.commands.executeCommand('scheduler.abort', undefined, `play:${id}`)

const item = getItem(id)
if (!item) {
return
Expand Down Expand Up @@ -105,6 +141,7 @@ function factory (api, workspace) {

api.commands.registerAsyncCommand('items.playItem', playItem)
api.commands.registerAsyncCommand('items.stopItem', stopItem)
api.commands.registerAsyncCommand('items.scheduleItem', scheduleItem)

api.commands.registerAsyncCommand('items.getItem', getItem)
api.commands.registerAsyncCommand('items.deleteItems', deleteItems)
Expand Down
22 changes: 16 additions & 6 deletions plugins/rundown/app/components/RundownItem/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function RundownItem ({ index, item }) {
]

React.useEffect(() => {
if (item?.state !== 'playing') {
if (item?.state !== 'playing' && item?.state !== 'scheduled') {
setProgress(0)
return
}
Expand All @@ -29,22 +29,32 @@ export function RundownItem ({ index, item }) {
return
}

const progress = (Date.now() - item?.didStartPlayingAt) / item?.data?.duration
let progress = 0

switch (item?.state) {
case 'playing':
progress = (Date.now() - item?.didStartPlayingAt) / item?.data?.duration
break
case 'scheduled':
progress = (item?.willStartPlayingAt - Date.now()) / (item?.willStartPlayingAt - item?.wasScheduledAt)
break
}

if (Number.isNaN(progress)) {
return
}

setProgress(Math.min(progress, 1))
setProgress(Math.max(Math.min(progress, 1), 0))

if (progress >= 1) {
if (progress >= 1 && item?.state === 'playing') {
return
}
window.requestAnimationFrame(loop)
}
loop()

return () => { shouldLoop = false }
}, [item?.state, item?.didStartPlayingAt])
}, [item?.state, item?.didStartPlayingAt, item?.willStartPlayingAt])

return (
<div className='RundownItem'>
Expand Down Expand Up @@ -84,7 +94,7 @@ export function RundownItem ({ index, item }) {
</div>
</Layout.Spread>
{
item?.state === 'playing' &&
['playing', 'scheduled'].includes(item?.state) &&
<div className='RundownItem-progress' style={{ transform: `scale(${progress}, 1)`, backgroundColor: item?.data?.color }} />
}
</div>
Expand Down
1 change: 1 addition & 0 deletions plugins/scheduler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ exports.activate = async () => {
bridge.commands.executeCommand(command, ...args)
}
}

events.set(id, event)
})

Expand Down

0 comments on commit 26af459

Please sign in to comment.