Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
const { InstanceStatus } = require('@companion-module/base')
const entities = require('entities')
const http = require('http')

const Client = require('node-rest-client').Client

function fetchAndEncodeBase64(url) {
return new Promise((resolve, reject) => {
http
.get(url, (res) => {
if (res.statusCode !== 200) {
reject(new Error(`Request Failed. Status Code: ${res.statusCode}`))
res.resume()
return
}

const chunks = []
res.on('data', (chunk) => {
chunks.push(chunk)
})

res.on('end', () => {
const base64 = Buffer.concat(chunks).toString('base64')
resolve(base64)
})
})
.on('error', (err) => {
reject(err)
})
})
}

module.exports = {
initConnection: function () {
let self = this
Expand Down Expand Up @@ -178,12 +205,25 @@ module.exports = {

buildAppList: function () {
let self = this
const promises = []

self.CHOICES_APPS = []

for (let i = 0; i < self.DATA.apps.length; i++) {
let app = self.DATA.apps[i]
self.CHOICES_APPS.push({ id: app.uri, label: entities.decodeHTML(app.title) })
if (app.icon) {
promises.push(
fetchAndEncodeBase64(app.icon).then((png64) => {
self.CHOICES_APPS[i].png64 = png64
}),
)
}
}

;(async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the leading semicolon some weird prettier quirk (I've seen similar before)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

await Promise.allSettled(promises)
self.initPresets()
})()
Comment on lines +224 to +227

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not hot enough on my async/promise stuff; will this function block until the promise is resolved and not allow program flow to continue, or will it do it in the background as it's async? Are we fine because the await is inside an async?

I was just thinking it would be good to initPresets beforehand too, but that's irrelevant if it runs in the background...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once all promises are resolved, the presets will be updated. Since this is local communication, it is assumed that all promises resolve immediately, allowing the presets to be updated in bulk.
Before retrieving icons, initPresets is executed. Once all icons have been successfully retrieved, initPresets is executed again.

},
}
8 changes: 7 additions & 1 deletion src/presets.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ module.exports = {

for (let i = 0; i < (this.CHOICES_APPS || []).length; i++) {
let app = this.CHOICES_APPS[i]
presets[`select_app_${i}`] = {
const preset = {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the mutable Javascript const!

Out of curiosity, what's the benefit of the preset over just putting the properties directly into the one we were creating before?

category: 'App',
name: `Select app ${app.label}`,
type: 'button',
Expand All @@ -271,6 +271,7 @@ module.exports = {
size: '10',
color: foregroundColor,
bgcolor: foregroundColorBlack,
png64: app.png64,
Comment thread
itota marked this conversation as resolved.
Outdated
},
steps: [
{
Expand All @@ -287,6 +288,11 @@ module.exports = {
},
],
}
if (app.png64) {
preset.style.png64 = app.png64
preset.style.alignment = 'center:bottom'
}
presets[`select_app_${i}`] = preset
}

self.setPresetDefinitions(presets)
Expand Down