renamed constants

This commit is contained in:
Zack Hoherchak 2022-03-02 16:22:53 -05:00
commit 9524f4e91d
3 changed files with 90 additions and 80 deletions

View file

@ -17,7 +17,7 @@ class ELGSDAction {
* @param {*} fn
*/
onDidReceiveSettings(fn) {
this.#on(`${this.UUID}.${DID_RECEIVE_SETTINGS}`, (jsn) => fn(jsn));
this.#on(`${this.UUID}.${Events.didReceiveSettings}`, (jsn) => fn(jsn));
return this;
}
@ -26,7 +26,7 @@ class ELGSDAction {
* @param {*} fn
*/
onDidReceiveGlobalSettings(fn) {
this.#on(DID_RECEIVE_GLOBAL_SETTINGS, (jsn) => fn(jsn));
this.#on(Events.didReceiveGlobalSettings, (jsn) => fn(jsn));
return this;
}
@ -35,7 +35,7 @@ class ELGSDAction {
* @param {*} fn
*/
onKeyDown(fn) {
this.#on(`${this.UUID}.${KEY_DOWN}`, (jsn) => fn(jsn));
this.#on(`${this.UUID}.${Events.keyDown}`, (jsn) => fn(jsn));
return this;
}
@ -44,7 +44,7 @@ class ELGSDAction {
* @param {*} fn
*/
onKeyUp(fn) {
this.#on(`${this.UUID}.${KEY_UP}`, (jsn) => fn(jsn));
this.#on(`${this.UUID}.${Events.keyUp}`, (jsn) => fn(jsn));
return this;
}
@ -53,7 +53,7 @@ class ELGSDAction {
* @param {*} fn
*/
onWillAppear(fn) {
this.#on(`${this.UUID}.${WILL_APPEAR}`, (jsn) => fn(jsn));
this.#on(`${this.UUID}.${Events.willAppear}`, (jsn) => fn(jsn));
return this;
}
@ -62,7 +62,7 @@ class ELGSDAction {
* @param {*} fn
*/
onWillDisappear(fn) {
this.#on(`${this.UUID}.${WILL_DISAPPEAR}`, (jsn) => fn(jsn));
this.#on(`${this.UUID}.${Events.willDisappear}`, (jsn) => fn(jsn));
return this;
}
@ -71,7 +71,7 @@ class ELGSDAction {
* @param {*} fn
*/
onTitleParametersDidChange(fn) {
this.#on(`${this.UUID}.${TITLE_PARAMETERS_DID_CHANGE}`, (jsn) => fn(jsn));
this.#on(`${this.UUID}.${Events.titleParametersDidChange}`, (jsn) => fn(jsn));
return this;
}
@ -80,7 +80,7 @@ class ELGSDAction {
* @param {*} fn
*/
onPropertyInspectorDidAppear(fn) {
this.#on(`${this.UUID}.${PROPERTY_INSPECTOR_DID_APPEAR}`, (jsn) => fn(jsn));
this.#on(`${this.UUID}.${Events.propertyInspectorDidAppear}`, (jsn) => fn(jsn));
return this;
}
@ -89,7 +89,7 @@ class ELGSDAction {
* @param {*} fn
*/
onPropertyInspectorDidDisappear(fn) {
this.#on(`${this.UUID}.${PROPERTY_INSPECTOR_DID_DISAPPEAR}`, (jsn) => fn(jsn));
this.#on(`${this.UUID}.${Events.propertyInspectorDidDisappear}`, (jsn) => fn(jsn));
return this;
}
@ -98,7 +98,7 @@ class ELGSDAction {
* @param {*} fn
*/
onSendToPlugin(fn) {
this.#on(`${this.UUID}.${SEND_TO_PLUGIN}`, (jsn) => fn(jsn));
this.#on(`${this.UUID}.${Events.sendToPlugin}`, (jsn) => fn(jsn));
return this;
}
@ -107,7 +107,7 @@ class ELGSDAction {
* @param {*} fn
*/
onSendToPropertyInspector(fn) {
this.#on(`${this.UUID}.${SEND_TO_PROPERTY_INSPECTOR}`, (jsn) => fn(jsn));
this.#on(`${this.UUID}.${Events.sendToPropertyInspector}`, (jsn) => fn(jsn));
return this;
}
}

View file

@ -1,3 +1,7 @@
/**
* Errors Receieved 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 = {
0: 'The connection has not yet been established',
1: 'The connection is established and communication is possible',
@ -19,44 +23,51 @@ const SocketErrors = {
};
/**
* Events
* 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}}
*/
//TODO: group these in objects that make sense & rename
const DID_RECEIVE_SETTINGS = 'didReceiveSettings';
const DID_RECEIVE_GLOBAL_SETTINGS = 'didReceiveGlobalSettings';
const KEY_DOWN = 'keyDown';
const KEY_UP = 'keyUp';
const WILL_APPEAR = 'willAppear';
const WILL_DISAPPEAR = 'willDisappear';
const TITLE_PARAMETERS_DID_CHANGE = 'titleParametersDidChange';
const DEVICE_DID_CONNECT = 'deviceDidConnect';
const DEVICE_DID_DISCONNECT = 'deviceDidDisconnect';
const APPLICATION_DID_LAUNCH = 'applicationDidLaunch';
const APPLICATION_DID_TERMINATE = 'applicationDidTerminate';
const SYSTEM_DID_WAKE_UP = 'systemDidWakeUp';
const PROPERTY_INSPECTOR_DID_APPEAR = 'propertyInspectorDidAppear';
const PROPERTY_INSPECTOR_DID_DISAPPEAR = 'propertyInspectorDidDisappear';
const SEND_TO_PLUGIN = 'sendToPlugin';
const SEND_TO_PROPERTY_INSPECTOR = 'sendToPropertyInspector';
const CONNECTED = 'connected';
const SET_IMAGE = 'setImage';
const SET_TITLE = 'setTitle';
const SET_STATE = 'setState';
const SHOW_OK = 'showOk';
const SHOW_ALERT = 'showAlert';
const OPEN_URL = 'openUrl';
const SET_GLOBAL_SETTINGS = 'setGlobalSettings';
const GET_GLOBAL_SETTINGS = 'getGlobalSettings';
const SET_SETTINGS = 'setSettings';
const GET_SETTINGS = 'getSettings';
const REGISTER_PROPERTY_INSPECTOR = 'registerPropertyInspector';
const REGISTER_PLUGIN = 'registerPlugin';
const DATA_LOCALIZE = '[data-localize]';
const LOG_MESSAGE = 'logMessage';
const Events = {
didReceiveSettings: 'didReceiveSettings',
didReceiveGlobalSettings: 'didReceiveGlobalSettings',
keyDown: 'keyDown',
keyUp: 'keyUp',
willAppear: 'willAppear',
willDisappear: 'willDisappear',
titleParametersDidChange: 'titleParametersDidChange',
deviceDidConnect: 'deviceDidConnect',
deviceDidDisconnect: 'deviceDidDisconnect',
applicationDidLaunch: 'applicationDidLaunch',
applicationDidTerminate: 'applicationDidTerminate',
systemDidWakeUp: 'systemDidWakeUp',
propertyInspectorDidAppear: 'propertyInspectorDidAppear',
propertyInspectorDidDisappear: 'propertyInspectorDidDisappear',
sendToPlugin: 'sendToPlugin',
sendToPropertyInspector: 'sendToPropertyInspector',
connected: 'connected',
setImage: 'setImage',
setTitle: 'setTitle',
setState: 'setState',
showOk: 'showOk',
showAlert: 'showAlert',
openUrl: 'openUrl',
setGlobalSettings: 'setGlobalSettings',
getGlobalSettings: 'getGlobalSettings',
setSettings: 'setSettings',
getSettings: 'getSettings',
registerPropertyInspector: 'registerPropertyInspector',
registerPlugin: 'registerPlugin',
logMessage: 'logMessage',
}
/**
* Destination
* Constants used for Stream Deck
* @type {{softwareOnly: number, dataLocalize: string, hardwareAndSoftware: number, hardwareOnly: number}}
*/
const HARDWARE_AND_SOFTWARE = 0;
const HARDWARE_ONLY = 1;
const SOFTWARE_ONLY = 2;
const Constants = {
dataLocalize: '[data-localize]',
//TODOZ: Try the things that use this in stream-deck.js to see if we really need it?
hardwareAndSoftware : 0,
hardwareOnly : 1,
softwareOnly : 2,
}

View file

@ -17,8 +17,8 @@ class ELGSDStreamDeck {
language;
localization;
appInfo;
on = EventEmitter.on;
emit = EventEmitter.emit;
#on = EventEmitter.on;
#emit = EventEmitter.emit;
constructor() {
if (ELGSDStreamDeck.__instance) {
@ -35,7 +35,6 @@ class ELGSDStreamDeck {
* @param messageType
* @param appInfoString
* @param actionString
* @private
*/
connect([port, uuid, messageType, appInfoString, actionString]) {
this.port = port;
@ -60,7 +59,7 @@ class ELGSDStreamDeck {
this.websocket.send(JSON.stringify(json));
this.emit(CONNECTED, {
this.#emit(Events.connected, {
connection: this.websocket,
port: this.port,
uuid: this.uuid,
@ -82,10 +81,10 @@ class ELGSDStreamDeck {
this.websocket.onmessage = (evt) => {
const data = evt?.data ? JSON.parse(evt.data) : {};
const { action, event } = data;
const {action, event} = data;
const message = action ? `${action}.${event}` : event;
if (message && message !== '') this.emit(message, data);
if (message && message !== '') this.#emit(message, data);
};
}
@ -97,7 +96,7 @@ class ELGSDStreamDeck {
try {
if (this.websocket) {
const json = {
event: LOG_MESSAGE,
event: Events.logMessage,
payload: {
message: message,
},
@ -118,8 +117,8 @@ class ELGSDStreamDeck {
const manifest = await this.readJson(`${pathPrefix}${this.language}.json`);
this.localization = manifest['Localization'] ?? null;
if (this.messageType === REGISTER_PROPERTY_INSPECTOR && this.localization) {
const elements = document.querySelectorAll(DATA_LOCALIZE);
if (this.messageType === Events.registerPropertyInspector && this.localization) {
const elements = document.querySelectorAll(Constants.dataLocalize);
elements.forEach((element) => {
element.textContent =
@ -161,7 +160,7 @@ class ELGSDStreamDeck {
* @param payload
*/
send(context, fn, payload) {
const pl = Object.assign({}, { event: fn, context: context }, payload);
const pl = Object.assign({}, {event: fn, context: context}, payload);
this.websocket && this.websocket.send(JSON.stringify(pl));
}
@ -170,7 +169,7 @@ class ELGSDStreamDeck {
* @param context
*/
getSettings(context) {
this.send(context ?? this.uuid, GET_SETTINGS, {});
this.send(context ?? this.uuid, Events.getSettings, {});
}
/**
@ -179,7 +178,7 @@ class ELGSDStreamDeck {
* @param context
*/
setSettings(payload, context) {
this.send(context ?? this.uuid, SET_SETTINGS, {
this.send(context ?? this.uuid, Events.setSettings, {
action: StreamDeck?.actionInfo?.action,
payload: payload || {},
targetContext: StreamDeck?.actionInfo?.context,
@ -190,7 +189,7 @@ class ELGSDStreamDeck {
* Request the plugin's persistent data. StreamDeck does not return the data, but trigger the plugin/property inspectors didReceiveGlobalSettings event
*/
getGlobalSettings() {
this.send(this.uuid, GET_GLOBAL_SETTINGS, {});
this.send(this.uuid, Events.getGlobalSettings, {});
}
/**
@ -198,7 +197,7 @@ class ELGSDStreamDeck {
* @param payload
*/
setGlobalSettings(payload) {
this.send(this.uuid, SET_GLOBAL_SETTINGS, {
this.send(this.uuid, Events.setGlobalSettings, {
payload: payload,
});
}
@ -208,7 +207,7 @@ class ELGSDStreamDeck {
* @param urlToOpen
*/
openUrl(urlToOpen) {
this.send(this.uuid, OPEN_URL, {
this.send(this.uuid, Events.openUrl, {
payload: {
url: urlToOpen,
},
@ -223,7 +222,7 @@ class ELGSDStreamDeck {
sendToPlugin(payload, context) {
this.send(
context ?? this.uuid,
SEND_TO_PLUGIN,
Events.sendToPlugin,
{
action: StreamDeck?.actionInfo?.action,
payload: payload || {},
@ -238,7 +237,7 @@ class ELGSDStreamDeck {
* @param context
*/
showAlert(context) {
this.send(context, SHOW_ALERT, {});
this.send(context, Events.showAlert, {});
}
/**
@ -246,7 +245,7 @@ class ELGSDStreamDeck {
* @param context
*/
showOk(context) {
this.send(context, SHOW_OK, {});
this.send(context, Events.showOk, {});
}
/**
@ -255,7 +254,7 @@ class ELGSDStreamDeck {
* @param payload
*/
setState(payload, context) {
this.send(context, SET_STATE, {
this.send(context, Events.setState, {
payload: {
state: 1 - Number(payload === 0),
},
@ -269,10 +268,10 @@ class ELGSDStreamDeck {
* @param target
*/
setTitle(title, target, context) {
this.send(context, SET_TITLE, {
this.send(context, Events.setTitle, {
payload: {
title: '' + title || '',
target: target || HARDWARE_AND_SOFTWARE,
target: target || Constants.hardwareAndSoftware,
},
});
}
@ -283,7 +282,7 @@ class ELGSDStreamDeck {
* @param payload
*/
sendToPropertyInspector(payload, context) {
this.send(context, SEND_TO_PROPERTY_INSPECTOR, {
this.send(context, Events.sendToPropertyInspector, {
action: this.actionInfo.action,
payload: payload,
});
@ -296,10 +295,10 @@ class ELGSDStreamDeck {
* @param target
*/
setImage(context, img, target) {
this.send(context, SET_IMAGE, {
this.send(context, Events.setImage, {
payload: {
image: img || '',
target: target || HARDWARE_AND_SOFTWARE,
target: target || Constants.hardwareAndSoftware,
},
});
}
@ -309,7 +308,7 @@ class ELGSDStreamDeck {
* @param {*} fn
*/
onConnected(fn) {
this.on(CONNECTED, (jsn) => fn(jsn));
this.#on(Events.connected, (jsn) => fn(jsn));
return this;
}
@ -318,7 +317,7 @@ class ELGSDStreamDeck {
* @param fn
*/
onSendToPropertyInspector(fn) {
this.on(SEND_TO_PROPERTY_INSPECTOR, (jsn) => fn(jsn));
this.#on(Events.sendToPropertyInspector, (jsn) => fn(jsn));
return this;
}
@ -327,7 +326,7 @@ class ELGSDStreamDeck {
* @param {*} fn
*/
onDeviceDidConnect(fn) {
this.on(DEVICE_DID_CONNECT, (jsn) => fn(jsn));
this.#on(Events.deviceDidConnect, (jsn) => fn(jsn));
return this;
}
@ -336,7 +335,7 @@ class ELGSDStreamDeck {
* @param {*} fn
*/
onDeviceDidDisconnect(fn) {
this.on(DEVICE_DID_DISCONNECT, (jsn) => fn(jsn));
this.#on(Events.deviceDidDisconnect, (jsn) => fn(jsn));
return this;
}
@ -345,7 +344,7 @@ class ELGSDStreamDeck {
* @param {*} fn
*/
onApplicationDidLaunch(fn) {
this.on(APPLICATION_DID_LAUNCH, (jsn) => fn(jsn));
this.#on(Events.applicationDidLaunch, (jsn) => fn(jsn));
return this;
}
@ -354,7 +353,7 @@ class ELGSDStreamDeck {
* @param {*} fn
*/
onApplicationDidTerminate(fn) {
this.on(APPLICATION_DID_TERMINATE, (jsn) => fn(jsn));
this.#on(Events.applicationDidTerminate, (jsn) => fn(jsn));
return this;
}
@ -363,12 +362,12 @@ class ELGSDStreamDeck {
* @param {*} fn
*/
onSystemDidWakeUp(fn) {
this.on(SYSTEM_DID_WAKE_UP, (jsn) => fn(jsn));
this.#on(Events.systemDidWakeUp, (jsn) => fn(jsn));
return this;
}
}
var $SD = new ELGSDStreamDeck();
const $SD = new ELGSDStreamDeck();
/**
* connectElgatoStreamDeckSocket