added more documentation

This commit is contained in:
Zack Hoherchak 2022-03-02 20:29:38 -05:00
commit 8de6987465
5 changed files with 100 additions and 79 deletions

View file

@ -15,8 +15,10 @@
.onDidReceiveSettings(() => { .onDidReceiveSettings(() => {
console.log({ object: 'action', event: 'onDidReceiveSettings' }); console.log({ object: 'action', event: 'onDidReceiveSettings' });
}) })
.onKeyDown(() => { .onKeyDown(({context, device}) => {
console.log(jsn);
console.log({ object: 'action', event: 'onKeyDown' }); console.log({ object: 'action', event: 'onKeyDown' });
$SD.switchToProfile(device, 'HUD XL');
}) })
.onKeyUp(() => { .onKeyUp(() => {
console.log({ object: 'action', event: 'onKeyUp' }); console.log({ object: 'action', event: 'onKeyUp' });

View file

@ -112,4 +112,4 @@ class ELGSDAction {
} }
} }
var Action = ELGSDAction; const Action = ELGSDAction;

View file

@ -1,6 +1,5 @@
/** /**
* Errors Receieved from WebSocket * Errors received from WebSocket
* @type {{"1000": string, "1011": string, "1010": string, "0": string, "1008": string, "1": string, "1007": string, "2": string, "1006": string, "3": string, "1005": string, "1004": string, "1015": string, "1003": string, "1002": string, "1001": string, "1009": string}}
*/ */
const SocketErrors = { const SocketErrors = {
0: 'The connection has not yet been established', 0: 'The connection has not yet been established',
@ -24,7 +23,6 @@ const SocketErrors = {
/** /**
* Events used for communicating with Stream Deck * Events used for communicating with Stream Deck
* @type {{sendToPlugin: string, deviceDidDisconnect: string, getSettings: string, deviceDidConnect: string, logMessage: string, keyUp: string, didReceiveGlobalSettings: string, showOk: string, didReceiveSettings: string, willAppear: string, applicationDidLaunch: string, setGlobalSettings: string, getGlobalSettings: string, applicationDidTerminate: string, willDisappear: string, propertyInspectorDidAppear: string, setTitle: string, titleParametersDidChange: string, registerPropertyInspector: string, connected: string, openUrl: string, setSettings: string, sendToPropertyInspector: string, registerPlugin: string, systemDidWakeUp: string, setState: string, propertyInspectorDidDisappear: string, keyDown: string, showAlert: string, setImage: string}}
*/ */
const Events = { const Events = {
didReceiveSettings: 'didReceiveSettings', didReceiveSettings: 'didReceiveSettings',
@ -57,11 +55,11 @@ const Events = {
registerPropertyInspector: 'registerPropertyInspector', registerPropertyInspector: 'registerPropertyInspector',
registerPlugin: 'registerPlugin', registerPlugin: 'registerPlugin',
logMessage: 'logMessage', logMessage: 'logMessage',
switchToProfile: 'switchToProfile'
} }
/** /**
* Constants used for Stream Deck * Constants used for Stream Deck
* @type {{softwareOnly: number, dataLocalize: string, hardwareAndSoftware: number, hardwareOnly: number}}
*/ */
const Constants = { const Constants = {
dataLocalize: '[data-localize]', dataLocalize: '[data-localize]',

View file

@ -28,4 +28,4 @@ class ELGSDEventEmitter {
} }
} }
var EventEmitter = new ELGSDEventEmitter(); const EventEmitter = new ELGSDEventEmitter();

View file

@ -1,7 +1,9 @@
/// <reference path="event-emitter.js" /> /// <reference path="event-emitter.js" />
/// <reference path="constants.js" /> /// <reference path="constants.js" />
// TODO: add clearTitle, switchToProfile (see https://elgato.slack.com/archives/D02LPKC0M9D/p1645183819000839 ), swap optional context to be second everywhere // TODO: switchToProfile (see https://elgato.slack.com/archives/D02LPKC0M9D/p1645183819000839 )
// TODO: See if we can add params for the call back function, so that the IDE will provide the jsn object with "context, device, etc"
// TODO: https://stackoverflow.com/questions/24214962/whats-the-proper-way-to-document-callbacks-with-jsdoc
/** /**
* @class StreamDeck * @class StreamDeck
@ -30,11 +32,11 @@ class ELGSDStreamDeck {
/** /**
* Connect to Stream Deck * Connect to Stream Deck
* @param port * @param {string} port
* @param uuid * @param {string} uuid
* @param messageType * @param {string} messageType
* @param appInfoString * @param {string} appInfoString
* @param actionString * @param {string} actionString
*/ */
connect([port, uuid, messageType, appInfoString, actionString]) { connect([port, uuid, messageType, appInfoString, actionString]) {
this.port = port; this.port = port;
@ -81,7 +83,7 @@ class ELGSDStreamDeck {
this.websocket.onmessage = (evt) => { this.websocket.onmessage = (evt) => {
const data = evt?.data ? JSON.parse(evt.data) : {}; const data = evt?.data ? JSON.parse(evt.data) : {};
const {action, event} = data; const { action, event } = data;
const message = action ? `${action}.${event}` : event; const message = action ? `${action}.${event}` : event;
if (message && message !== '') this.#emit(message, data); if (message && message !== '') this.#emit(message, data);
@ -90,7 +92,7 @@ class ELGSDStreamDeck {
/** /**
* Write to log file * Write to log file
* @param message * @param {string} message
*/ */
logMessage(message) { logMessage(message) {
try { try {
@ -110,7 +112,7 @@ class ELGSDStreamDeck {
/** /**
* Fetches the specified language json file * Fetches the specified language json file
* @param pathPrefix * @param {string} pathPrefix
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async loadLocalization(pathPrefix) { async loadLocalization(pathPrefix) {
@ -121,16 +123,15 @@ class ELGSDStreamDeck {
const elements = document.querySelectorAll(Constants.dataLocalize); const elements = document.querySelectorAll(Constants.dataLocalize);
elements.forEach((element) => { elements.forEach((element) => {
element.textContent = element.textContent = this.localization[element.textContent] ?? element.textContent;
this.localization[element.textContent] ?? element.textContent;
}); });
} }
} }
/** /**
* *
* @param {*} path * @param {string} path
* @returns * @returns {Promise<any>} json
*/ */
async readJson(path) { async readJson(path) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -155,33 +156,32 @@ class ELGSDStreamDeck {
/** /**
* Send JSON payload to StreamDeck * Send JSON payload to StreamDeck
* @param context * @param {string} event
* @param fn * @param {string} context
* @param payload * @param {object} [payload]
*/ */
send(context, fn, payload) { send(event, context, payload = {}) {
const pl = Object.assign({}, {event: fn, context: context}, payload); const pl = Object.assign({}, {event: event, context: context}, payload);
this.websocket && this.websocket.send(JSON.stringify(pl)); this.websocket && this.websocket.send(JSON.stringify(pl)); }
}
/** /**
* Request the actions's persistent data. StreamDeck does not return the data, but trigger the actions's didReceiveSettings event * Request the actions's persistent data. StreamDeck does not return the data, but trigger the actions's didReceiveSettings event
* @param context * @param {string} [context]
*/ */
getSettings(context) { getSettings(context) {
this.send(context ?? this.uuid, Events.getSettings, {}); this.send(Events.getSettings, context ?? this.uuid);
} }
/** /**
* Save the actions's persistent data. * Save the actions's persistent data.
* @param payload * @param {object} payload
* @param context * @param [context]
*/ */
setSettings(payload, context) { setSettings(payload, context) {
this.send(context ?? this.uuid, Events.setSettings, { this.send(Events.setSettings, context ?? this.uuid, {
action: StreamDeck?.actionInfo?.action, action: this?.actionInfo?.action,
payload: payload || {}, payload: payload || {},
targetContext: StreamDeck?.actionInfo?.context, targetContext: this?.actionInfo?.context,
}); });
} }
@ -189,25 +189,25 @@ class ELGSDStreamDeck {
* Request the plugin's persistent data. StreamDeck does not return the data, but trigger the plugin/property inspectors didReceiveGlobalSettings event * Request the plugin's persistent data. StreamDeck does not return the data, but trigger the plugin/property inspectors didReceiveGlobalSettings event
*/ */
getGlobalSettings() { getGlobalSettings() {
this.send(this.uuid, Events.getGlobalSettings, {}); this.send(Events.getGlobalSettings, this.uuid);
} }
/** /**
* Save the plugin's persistent data * Save the plugin's persistent data
* @param payload * @param {object} payload
*/ */
setGlobalSettings(payload) { setGlobalSettings(payload) {
this.send(this.uuid, Events.setGlobalSettings, { this.send(Events.setGlobalSettings, this.uuid, {
payload: payload, payload: payload,
}); });
} }
/** /**
* Opens a URL in the default web browser * Opens a URL in the default web browser
* @param urlToOpen * @param {string} urlToOpen
*/ */
openUrl(urlToOpen) { openUrl(urlToOpen) {
this.send(this.uuid, Events.openUrl, { this.send(Events.openUrl, this.uuid, {
payload: { payload: {
url: urlToOpen, url: urlToOpen,
}, },
@ -216,45 +216,40 @@ class ELGSDStreamDeck {
/** /**
* Send payload from the property inspector to the plugin * Send payload from the property inspector to the plugin
* @param payload * @param {object} payload
* @param context * @param {string} [context]
*/ */
sendToPlugin(payload, context) { sendToPlugin(payload, context) {
this.send( this.send(Events.sendToPlugin, context ?? this.uuid, {
context ?? this.uuid, action: this?.actionInfo?.action,
Events.sendToPlugin,
{
action: StreamDeck?.actionInfo?.action,
payload: payload || {}, payload: payload || {},
targetContext: StreamDeck?.actionInfo?.context, targetContext: this?.actionInfo?.context,
}, });
false
);
} }
/** /**
* Display alert triangle on actions key * Display alert triangle on actions key
* @param context * @param {string} context
*/ */
showAlert(context) { showAlert(context) {
this.send(context, Events.showAlert, {}); this.send(Events.showAlert, context);
} }
/** /**
* Display ok check mark on actions key * Display ok check mark on actions key
* @param context * @param {string} context
*/ */
showOk(context) { showOk(context) {
this.send(context, Events.showOk, {}); this.send(Events.showOk, context);
} }
/** /**
* Set the state of the actions * Set the state of the actions
* @param context * @param {object} payload
* @param payload * @param {string} context
*/ */
setState(payload, context) { setState(payload, context) {
this.send(context, Events.setState, { this.send(Events.setState, context, {
payload: { payload: {
state: 1 - Number(payload === 0), state: 1 - Number(payload === 0),
}, },
@ -262,13 +257,13 @@ class ELGSDStreamDeck {
} }
/** /**
* Set the title of the actions's key * Set the title of the action's key
* @param context * @param {string} title
* @param title * @param {string} context
* @param target * @param [target]
*/ */
setTitle(title, target, context) { setTitle(title, context, target) {
this.send(context, Events.setTitle, { this.send(Events.setTitle, context, {
payload: { payload: {
title: '' + title || '', title: '' + title || '',
target: target || Constants.hardwareAndSoftware, target: target || Constants.hardwareAndSoftware,
@ -276,13 +271,22 @@ class ELGSDStreamDeck {
}); });
} }
/**
*
* @param {string} context
* @param {number} [target]
*/
clearTitle(context, target) {
this.setTitle(null, context, target || Constants.hardwareAndSoftware);
}
/** /**
* Send payload to property inspector * Send payload to property inspector
* @param context * @param {object} payload
* @param payload * @param {string} context
*/ */
sendToPropertyInspector(payload, context) { sendToPropertyInspector(payload, context) {
this.send(context, Events.sendToPropertyInspector, { this.send(Events.sendToPropertyInspector, context, {
action: this.actionInfo.action, action: this.actionInfo.action,
payload: payload, payload: payload,
}); });
@ -290,12 +294,12 @@ class ELGSDStreamDeck {
/** /**
* Set the actions key image * Set the actions key image
* @param context * @param {string} img
* @param img * @param {string} context
* @param target * @param {number} [target]
*/ */
setImage(context, img, target) { setImage(img, context, target) {
this.send(context, Events.setImage, { this.send(Events.setImage, context, {
payload: { payload: {
image: img || '', image: img || '',
target: target || Constants.hardwareAndSoftware, target: target || Constants.hardwareAndSoftware,
@ -303,9 +307,20 @@ class ELGSDStreamDeck {
}); });
} }
//TODO: fix profile switch not working
/**
* Switches to profile or returns to previous profile
* @param {string} device
* @param {string} [profile]
*/
switchToProfile(device, profile ) {
this.send(Events.switchToProfile, this.uuid, { device: device, payload: { profile } });
}
/** /**
* Registers a callback function for when Stream Deck is connected * Registers a callback function for when Stream Deck is connected
* @param {*} fn * @param {function} fn
* @returns ELGSDStreamDeck
*/ */
onConnected(fn) { onConnected(fn) {
this.#on(Events.connected, (jsn) => fn(jsn)); this.#on(Events.connected, (jsn) => fn(jsn));
@ -314,7 +329,8 @@ class ELGSDStreamDeck {
/** /**
* Registers a callback function for when Stream Deck sends data to the property inspector * Registers a callback function for when Stream Deck sends data to the property inspector
* @param fn * @param {function} fn
* @returns ELGSDStreamDeck
*/ */
onSendToPropertyInspector(fn) { onSendToPropertyInspector(fn) {
this.#on(Events.sendToPropertyInspector, (jsn) => fn(jsn)); this.#on(Events.sendToPropertyInspector, (jsn) => fn(jsn));
@ -323,7 +339,8 @@ class ELGSDStreamDeck {
/** /**
* Registers a callback function for the deviceDidConnect event, which fires when a device is plugged in * Registers a callback function for the deviceDidConnect event, which fires when a device is plugged in
* @param {*} fn * @param {function} fn
* @returns ELGSDStreamDeck
*/ */
onDeviceDidConnect(fn) { onDeviceDidConnect(fn) {
this.#on(Events.deviceDidConnect, (jsn) => fn(jsn)); this.#on(Events.deviceDidConnect, (jsn) => fn(jsn));
@ -332,7 +349,8 @@ class ELGSDStreamDeck {
/** /**
* Registers a callback function for the deviceDidDisconnect event, which fires when a device is unplugged * Registers a callback function for the deviceDidDisconnect event, which fires when a device is unplugged
* @param {*} fn * @param {function} fn
* @returns ELGSDStreamDeck
*/ */
onDeviceDidDisconnect(fn) { onDeviceDidDisconnect(fn) {
this.#on(Events.deviceDidDisconnect, (jsn) => fn(jsn)); this.#on(Events.deviceDidDisconnect, (jsn) => fn(jsn));
@ -341,7 +359,8 @@ class ELGSDStreamDeck {
/** /**
* Registers a callback function for the applicationDidLaunch event, which fires when the application starts * Registers a callback function for the applicationDidLaunch event, which fires when the application starts
* @param {*} fn * @param {function} fn
* @returns ELGSDStreamDeck
*/ */
onApplicationDidLaunch(fn) { onApplicationDidLaunch(fn) {
this.#on(Events.applicationDidLaunch, (jsn) => fn(jsn)); this.#on(Events.applicationDidLaunch, (jsn) => fn(jsn));
@ -350,7 +369,8 @@ class ELGSDStreamDeck {
/** /**
* Registers a callback function for the applicationDidTerminate event, which fires when the application exits * Registers a callback function for the applicationDidTerminate event, which fires when the application exits
* @param {*} fn * @param {function} fn
* @returns ELGSDStreamDeck
*/ */
onApplicationDidTerminate(fn) { onApplicationDidTerminate(fn) {
this.#on(Events.applicationDidTerminate, (jsn) => fn(jsn)); this.#on(Events.applicationDidTerminate, (jsn) => fn(jsn));
@ -359,7 +379,8 @@ class ELGSDStreamDeck {
/** /**
* Registers a callback function for the systemDidWakeUp event, which fires when the computer wakes * Registers a callback function for the systemDidWakeUp event, which fires when the computer wakes
* @param {*} fn * @param {function} fn
* @returns ELGSDStreamDeck
*/ */
onSystemDidWakeUp(fn) { onSystemDidWakeUp(fn) {
this.#on(Events.systemDidWakeUp, (jsn) => fn(jsn)); this.#on(Events.systemDidWakeUp, (jsn) => fn(jsn));