-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add on play options to playable items
Signed-off-by: Axel Boberg <[email protected]>
- Loading branch information
1 parent
9d80533
commit 40af571
Showing
6 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-FileCopyrightText: 2024 Sveriges Television AB | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
const bridge = require('bridge') | ||
|
||
/** | ||
* Get the id of the next sibling | ||
* from the specified item within | ||
* its parent item | ||
* @param { String } parentId | ||
* @param { String } itemId | ||
* @returns { Promise.<String | undefined> } | ||
*/ | ||
async function getNextSibling (parentId, itemId) { | ||
const siblings = await bridge.state.get(`items.${parentId}.children`) || [] | ||
const index = siblings.indexOf(itemId) | ||
return siblings[index + 1] | ||
} | ||
exports.getNextSibling = getNextSibling |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// SPDX-FileCopyrightText: 2024 Sveriges Television AB | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
const bridge = require('bridge') | ||
const commands = require('./commands') | ||
|
||
const ON_PLAY_OPTIONS = { | ||
playNextSibling: 1, | ||
selectNextSibling: 2 | ||
} | ||
|
||
const MAIN_ROLE_ID = 1 | ||
|
||
const HANDLERS = [ | ||
/* | ||
Handle the on play option | ||
*/ | ||
{ | ||
condition: item => item.data?.onPlay, | ||
fn: async item => { | ||
const value = parseInt(item?.data?.onPlay) | ||
|
||
if (value === ON_PLAY_OPTIONS.playNextSibling) { | ||
const sibling = await commands.getNextSibling(item?.parent, item?.id) | ||
if (!sibling) { | ||
return | ||
} | ||
bridge.items.playItem(sibling) | ||
return | ||
} | ||
|
||
if (value === ON_PLAY_OPTIONS.selectNextSibling) { | ||
const sibling = await commands.getNextSibling(item?.parent, item?.id) | ||
if (!sibling) { | ||
return | ||
} | ||
selectItem(sibling) | ||
} | ||
} | ||
} | ||
] | ||
|
||
/** | ||
* Get the main client's id | ||
* @returns { Promise.<String | undefined> } | ||
*/ | ||
async function getMainClientId () { | ||
const connections = await bridge.state.get('_connections') | ||
const main = Object.entries(connections) | ||
.filter(([_, value]) => value?.role === MAIN_ROLE_ID) | ||
.map(([key]) => { | ||
return key | ||
}) | ||
|
||
return main[0] | ||
} | ||
|
||
/** | ||
* Select the specified item | ||
* in the main client | ||
* @param { String } id | ||
* @returns | ||
*/ | ||
async function selectItem (id) { | ||
const mainClientId = await getMainClientId() | ||
if (mainClientId == null) { | ||
return | ||
} | ||
bridge.state.apply({ | ||
_connections: { | ||
[mainClientId]: { | ||
selection: { $replace: [id] } | ||
} | ||
} | ||
}) | ||
} | ||
|
||
bridge.events.on('item.play', item => { | ||
for (const handler of HANDLERS) { | ||
if (handler.condition(item)) { | ||
handler.fn(item) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters