error handling and cleanup
This commit is contained in:
parent
3eae70a39e
commit
1c5e07ee4b
5 changed files with 152 additions and 64 deletions
|
|
@ -5,53 +5,9 @@
|
|||
* The first event fired when Stream Deck starts
|
||||
*/
|
||||
$SD.onConnected(({ actionInfo, appInfo, connection, messageType, port, uuid }) => {
|
||||
// TODO: Remove this test logging code and replace with simple action
|
||||
console.log({ object: 'stream-deck', event: 'onConnected' });
|
||||
})
|
||||
.onDeviceDidConnect(() => {
|
||||
console.log({ object: 'stream-deck', event: 'onDeviceDidConnect' });
|
||||
})
|
||||
.onDeviceDidDisconnect(() => {
|
||||
console.log({ object: 'stream-deck', event: 'onDeviceDidDisconnect' });
|
||||
})
|
||||
.onApplicationDidLaunch(() => {
|
||||
console.log({ object: 'stream-deck', event: 'onApplicationDidLaunch' });
|
||||
})
|
||||
.onApplicationDidTerminate(() => {
|
||||
console.log({ object: 'stream-deck', event: 'onApplicationDidTerminate' });
|
||||
})
|
||||
.onSystemDidWakeUp(() => {
|
||||
console.log({ object: 'stream-deck', event: 'onSystemDidWakeUp' });
|
||||
});
|
||||
const myAction = new Action('com.elgato.template.action');
|
||||
|
||||
new Action('com.elgato.template.action')
|
||||
.onDidReceiveGlobalSettings(() => {
|
||||
console.log({ object: 'action', event: 'onDidReceiveGlobalSettings' });
|
||||
})
|
||||
.onDidReceiveSettings(() => {
|
||||
console.log({ object: 'action', event: 'onDidReceiveSettings' });
|
||||
})
|
||||
.onKeyDown(() => {
|
||||
console.log({ object: 'action', event: 'onKeyDown' });
|
||||
})
|
||||
.onKeyUp(() => {
|
||||
console.log({ object: 'action', event: 'onKeyUp' });
|
||||
})
|
||||
.onWillAppear(() => {
|
||||
console.log({ object: 'action', event: 'onWillAppear' });
|
||||
})
|
||||
.onWillDisappear(() => {
|
||||
console.log({ object: 'action', event: 'onWillDisappear' });
|
||||
})
|
||||
.onTitleParametersDidChange(() => {
|
||||
console.log({ object: 'action', event: 'onTitleParametersDidChange' });
|
||||
})
|
||||
.onPropertyInspectorDidAppear(() => {
|
||||
console.log({ object: 'action', event: 'onPropertyInspectorDidAppear' });
|
||||
})
|
||||
.onPropertyInspectorDidDisappear(() => {
|
||||
console.log({ object: 'action', event: 'onPropertyInspectorDidDisappear' });
|
||||
})
|
||||
.onSendToPlugin(() => {
|
||||
console.log({ object: 'action', event: 'onSendToPlugin' });
|
||||
myAction.onKeyUp(({ action, context, device, event, payload }) => {
|
||||
console.log('Your code goes here!');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -9,8 +9,11 @@ class ELGSDAction {
|
|||
on = EventEmitter.on;
|
||||
emit = EventEmitter.emit;
|
||||
|
||||
|
||||
constructor(UUID) {
|
||||
if (!UUID) {
|
||||
throw 'An action UUID matching the action UUID in your manifest is required when creating Actions.';
|
||||
}
|
||||
|
||||
this.UUID = UUID;
|
||||
}
|
||||
|
||||
|
|
@ -19,7 +22,11 @@ class ELGSDAction {
|
|||
* @param {function} fn
|
||||
*/
|
||||
onDidReceiveSettings(fn) {
|
||||
this.#on(`${this.UUID}.${Events.didReceiveSettings}`, (jsn) => fn(jsn));
|
||||
if (!fn) {
|
||||
throw 'A callback function for the didReceiveSettings event is required for onDidReceiveSettings.';
|
||||
}
|
||||
|
||||
this.on(`${this.UUID}.${Events.didReceiveSettings}`, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -28,7 +35,11 @@ class ELGSDAction {
|
|||
* @param {function} fn
|
||||
*/
|
||||
onKeyDown(fn) {
|
||||
this.#on(`${this.UUID}.${Events.keyDown}`, (jsn) => fn(jsn));
|
||||
if (!fn) {
|
||||
throw 'A callback function for the keyDown event is required for onKeyDown.';
|
||||
}
|
||||
|
||||
this.on(`${this.UUID}.${Events.keyDown}`, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -37,7 +48,11 @@ class ELGSDAction {
|
|||
* @param {function} fn
|
||||
*/
|
||||
onKeyUp(fn) {
|
||||
this.#on(`${this.UUID}.${Events.keyUp}`, (jsn) => fn(jsn));
|
||||
if (!fn) {
|
||||
throw 'A callback function for the keyUp event is required for onKeyUp.';
|
||||
}
|
||||
|
||||
this.on(`${this.UUID}.${Events.keyUp}`, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -46,7 +61,11 @@ class ELGSDAction {
|
|||
* @param {function} fn
|
||||
*/
|
||||
onWillAppear(fn) {
|
||||
this.#on(`${this.UUID}.${Events.willAppear}`, (jsn) => fn(jsn));
|
||||
if (!fn) {
|
||||
throw 'A callback function for the willAppear event is required for onWillAppear.';
|
||||
}
|
||||
|
||||
this.on(`${this.UUID}.${Events.willAppear}`, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -55,7 +74,11 @@ class ELGSDAction {
|
|||
* @param {function} fn
|
||||
*/
|
||||
onWillDisappear(fn) {
|
||||
this.#on(`${this.UUID}.${Events.willDisappear}`, (jsn) => fn(jsn));
|
||||
if (!fn) {
|
||||
throw 'A callback function for the willDisappear event is required for onWillDisappear.';
|
||||
}
|
||||
|
||||
this.on(`${this.UUID}.${Events.willDisappear}`, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -64,7 +87,11 @@ class ELGSDAction {
|
|||
* @param {function} fn
|
||||
*/
|
||||
onTitleParametersDidChange(fn) {
|
||||
this.#on(`${this.UUID}.${Events.titleParametersDidChange}`, (jsn) => fn(jsn));
|
||||
if (!fn) {
|
||||
throw 'A callback function for the titleParametersDidChange event is required for onTitleParametersDidChange.';
|
||||
}
|
||||
|
||||
this.on(`${this.UUID}.${Events.titleParametersDidChange}`, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +100,11 @@ class ELGSDAction {
|
|||
* @param {function} fn
|
||||
*/
|
||||
onPropertyInspectorDidAppear(fn) {
|
||||
this.#on(`${this.UUID}.${Events.propertyInspectorDidAppear}`, (jsn) => fn(jsn));
|
||||
if (!fn) {
|
||||
throw 'A callback function for the propertyInspectorDidAppear event is required for onPropertyInspectorDidAppear.';
|
||||
}
|
||||
|
||||
this.on(`${this.UUID}.${Events.propertyInspectorDidAppear}`, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -82,7 +113,11 @@ class ELGSDAction {
|
|||
* @param {function} fn
|
||||
*/
|
||||
onPropertyInspectorDidDisappear(fn) {
|
||||
this.#on(`${this.UUID}.${Events.propertyInspectorDidDisappear}`, (jsn) => fn(jsn));
|
||||
if (!fn) {
|
||||
throw 'A callback function for the propertyInspectorDidDisappear event is required for onPropertyInspectorDidDisappear.';
|
||||
}
|
||||
|
||||
this.on(`${this.UUID}.${Events.propertyInspectorDidDisappear}`, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -91,7 +126,11 @@ class ELGSDAction {
|
|||
* @param {function} fn
|
||||
*/
|
||||
onSendToPlugin(fn) {
|
||||
this.#on(`${this.UUID}.${Events.sendToPlugin}`, (jsn) => fn(jsn));
|
||||
if (!fn) {
|
||||
throw 'A callback function for the sendToPlugin event is required for onSendToPlugin.';
|
||||
}
|
||||
|
||||
this.on(`${this.UUID}.${Events.sendToPlugin}`, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
* StreamDeck object containing all required code to establish
|
||||
* communication with SD-Software and the Property Inspector
|
||||
*/
|
||||
class ELGSDStreamDeck {
|
||||
class ELGSDStreamDeck {
|
||||
port;
|
||||
uuid;
|
||||
messageType;
|
||||
|
|
@ -90,6 +90,10 @@
|
|||
* @param {string} message
|
||||
*/
|
||||
logMessage(message) {
|
||||
if (!message) {
|
||||
throw 'A message is required for logMessage.';
|
||||
}
|
||||
|
||||
try {
|
||||
if (this.websocket) {
|
||||
const json = {
|
||||
|
|
@ -113,6 +117,10 @@
|
|||
* @returns {Promise<void>}
|
||||
*/
|
||||
async loadLocalization(pathPrefix) {
|
||||
if (!pathPrefix) {
|
||||
throw 'A path to localization json is required for loadLocalization.';
|
||||
}
|
||||
|
||||
const manifest = await this.readJson(`${pathPrefix}${this.language}.json`);
|
||||
this.localization = manifest['Localization'] ?? null;
|
||||
|
||||
|
|
@ -131,6 +139,10 @@
|
|||
* @returns {Promise<any>} json
|
||||
*/
|
||||
async readJson(path) {
|
||||
if (!path) {
|
||||
throw 'A path is required to readJson.';
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = new XMLHttpRequest();
|
||||
req.onerror = reject;
|
||||
|
|
@ -205,6 +217,10 @@
|
|||
* @param {string} urlToOpen
|
||||
*/
|
||||
openUrl(urlToOpen) {
|
||||
if (!url) {
|
||||
throw 'A url is required for openUrl.';
|
||||
}
|
||||
|
||||
this.send(Events.openUrl, this.uuid, {
|
||||
payload: {
|
||||
url: urlToOpen,
|
||||
|
|
@ -230,6 +246,10 @@
|
|||
* @param {string} context
|
||||
*/
|
||||
showAlert(context) {
|
||||
if (!context) {
|
||||
throw 'A context is required to showAlert on the key.';
|
||||
}
|
||||
|
||||
this.send(Events.showAlert, context);
|
||||
}
|
||||
|
||||
|
|
@ -238,6 +258,10 @@
|
|||
* @param {string} context
|
||||
*/
|
||||
showOk(context) {
|
||||
if (!context) {
|
||||
throw 'A context is required to showOk on the key.';
|
||||
}
|
||||
|
||||
this.send(Events.showOk, context);
|
||||
}
|
||||
|
||||
|
|
@ -247,6 +271,14 @@
|
|||
* @param {string} context
|
||||
*/
|
||||
setState(payload, context) {
|
||||
if (!payload) {
|
||||
throw 'A state is required when using setState.';
|
||||
}
|
||||
|
||||
if (!context) {
|
||||
throw 'A context is required when using setState.';
|
||||
}
|
||||
|
||||
this.send(Events.setState, context, {
|
||||
payload: {
|
||||
state: 1 - Number(payload === 0),
|
||||
|
|
@ -261,6 +293,14 @@
|
|||
* @param [target]
|
||||
*/
|
||||
setTitle(title, context, target) {
|
||||
if (!title) {
|
||||
throw 'A title is required for setTitle.';
|
||||
}
|
||||
|
||||
if (!context) {
|
||||
throw 'A key context is required for setTitle.';
|
||||
}
|
||||
|
||||
this.send(Events.setTitle, context, {
|
||||
payload: {
|
||||
title: '' + title || '',
|
||||
|
|
@ -275,10 +315,12 @@
|
|||
* @param {number} [target]
|
||||
*/
|
||||
clearTitle(context, target) {
|
||||
if (!context) {
|
||||
throw 'A key context is required to clearTitle.';
|
||||
}
|
||||
this.setTitle(null, context, target || Constants.hardwareAndSoftware);
|
||||
}
|
||||
|
||||
// TODO; add error handling like this with nice messages for all methods for the template
|
||||
/**
|
||||
* Send payload to property inspector
|
||||
* @param {string} actionUUID
|
||||
|
|
@ -287,11 +329,11 @@
|
|||
*/
|
||||
sendToPropertyInspector(actionUUID, context, payload) {
|
||||
if (typeof actionUUID != 'string') {
|
||||
throw 'sendToPropertyInspector requires an actionUUID string';
|
||||
throw 'An action UUID is required to sendToPropertyInspector.';
|
||||
}
|
||||
|
||||
if (typeof context != 'string') {
|
||||
throw 'sendToPropertyInspector requires a key context string';
|
||||
throw 'A key context is required to sendToPropertyInspector.';
|
||||
}
|
||||
|
||||
this.send(Events.sendToPropertyInspector, context, {
|
||||
|
|
@ -307,6 +349,13 @@
|
|||
* @param {number} [target]
|
||||
*/
|
||||
setImage(img, context, target) {
|
||||
if (!img) {
|
||||
throw 'An image is required for setImage.';
|
||||
}
|
||||
|
||||
if (!context) {
|
||||
throw 'A key context is required for setImage.';
|
||||
}
|
||||
this.send(Events.setImage, context, {
|
||||
payload: {
|
||||
image: img || '',
|
||||
|
|
@ -321,6 +370,14 @@
|
|||
* @param {string} [profile]
|
||||
*/
|
||||
switchToProfile(device, profile) {
|
||||
if (!device) {
|
||||
throw 'A device id is required for switchToProfile.';
|
||||
}
|
||||
|
||||
if (!profile) {
|
||||
throw 'A profile name is required for switchToProfile';
|
||||
}
|
||||
|
||||
this.send(Events.switchToProfile, this.uuid, { device: device, payload: { profile } });
|
||||
}
|
||||
|
||||
|
|
@ -330,6 +387,10 @@
|
|||
* @returns ELGSDStreamDeck
|
||||
*/
|
||||
onConnected(fn) {
|
||||
if (!fn) {
|
||||
throw 'A callback function for the connected event is required for onConnected.';
|
||||
}
|
||||
|
||||
this.on(Events.connected, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
|
@ -342,8 +403,13 @@
|
|||
*/
|
||||
onSendToPropertyInspector(actionUUID, fn) {
|
||||
if (typeof actionUUID != 'string') {
|
||||
throw 'onSendToPropertyInspector requires an actionUUID string.';
|
||||
throw 'An action UUID string is required for onSendToPropertyInspector.';
|
||||
}
|
||||
|
||||
if (!fn) {
|
||||
throw 'A callback function for the sendToPropertyInspector event is required for onSendToPropertyInspector.';
|
||||
}
|
||||
|
||||
this.on(`${actionUUID}.${Events.sendToPropertyInspector}`, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
|
@ -354,6 +420,10 @@
|
|||
* @returns ELGSDStreamDeck
|
||||
*/
|
||||
onDeviceDidConnect(fn) {
|
||||
if (!fn) {
|
||||
throw 'A callback function for the deviceDidConnect event is required for onDeviceDidConnect.';
|
||||
}
|
||||
|
||||
this.on(Events.deviceDidConnect, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
|
@ -364,6 +434,10 @@
|
|||
* @returns ELGSDStreamDeck
|
||||
*/
|
||||
onDeviceDidDisconnect(fn) {
|
||||
if (!fn) {
|
||||
throw 'A callback function for the deviceDidDisconnect event is required for onDeviceDidDisconnect.';
|
||||
}
|
||||
|
||||
this.on(Events.deviceDidDisconnect, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
|
@ -374,6 +448,10 @@
|
|||
* @returns ELGSDStreamDeck
|
||||
*/
|
||||
onApplicationDidLaunch(fn) {
|
||||
if (!fn) {
|
||||
throw 'A callback function for the applicationDidLaunch event is required for onApplicationDidLaunch.';
|
||||
}
|
||||
|
||||
this.on(Events.applicationDidLaunch, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
|
@ -384,6 +462,10 @@
|
|||
* @returns ELGSDStreamDeck
|
||||
*/
|
||||
onApplicationDidTerminate(fn) {
|
||||
if (!fn) {
|
||||
throw 'A callback function for the applicationDidTerminate event is required for onApplicationDidTerminate.';
|
||||
}
|
||||
|
||||
this.on(Events.applicationDidTerminate, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
|
@ -394,6 +476,10 @@
|
|||
* @returns ELGSDStreamDeck
|
||||
*/
|
||||
onSystemDidWakeUp(fn) {
|
||||
if (!fn) {
|
||||
throw 'A callback function for the systemDidWakeUp event is required for onSystemDidWakeUp.';
|
||||
}
|
||||
|
||||
this.on(Events.systemDidWakeUp, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
|
@ -403,6 +489,10 @@
|
|||
* @param {function} fn
|
||||
*/
|
||||
onDidReceiveGlobalSettings(fn) {
|
||||
if (!fn) {
|
||||
throw 'A callback function for the didReceiveGlobalSettings event is required for onDidReceiveGlobalSettings.';
|
||||
}
|
||||
|
||||
this.on(Events.didReceiveGlobalSettings, (jsn) => fn(jsn));
|
||||
return this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ class Utils {
|
|||
const { name, type } = element;
|
||||
const value = jsn[name];
|
||||
const isCheckOrRadio = type === 'checkbox' || type === 'radio';
|
||||
console.log(element.type)
|
||||
|
||||
if (isCheckOrRadio) {
|
||||
const isSingle = value === element.value;
|
||||
if (isSingle || (Array.isArray(value) && value.includes(element.value))) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue