removed private $SD fields

This commit is contained in:
Zack Hoherchak 2022-02-28 16:35:12 -05:00
commit 50fe30ca8b

View file

@ -9,16 +9,16 @@
* communication with SD-Software and the Property Inspector * communication with SD-Software and the Property Inspector
*/ */
class ELGSDStreamDeck { class ELGSDStreamDeck {
#port; port;
#uuid; uuid;
#messageType; messageType;
#actionInfo; actionInfo;
#websocket; websocket;
#language; language;
#localization; localization;
#appInfo; appInfo;
#on = EventEmitter.on; on = EventEmitter.on;
#emit = EventEmitter.emit; emit = EventEmitter.emit;
constructor() { constructor() {
if (ELGSDStreamDeck.__instance) { if (ELGSDStreamDeck.__instance) {
@ -38,54 +38,54 @@ class ELGSDStreamDeck {
* @private * @private
*/ */
connect([port, uuid, messageType, appInfoString, actionString]) { connect([port, uuid, messageType, appInfoString, actionString]) {
this.#port = port; this.port = port;
this.#uuid = uuid; this.uuid = uuid;
this.#messageType = messageType; this.messageType = messageType;
this.#actionInfo = actionString ? JSON.parse(actionString) : null; this.actionInfo = actionString ? JSON.parse(actionString) : null;
this.#appInfo = JSON.parse(appInfoString); this.appInfo = JSON.parse(appInfoString);
this.#language = this.#appInfo?.application?.language ?? null; this.language = this.appInfo?.application?.language ?? null;
if (this.#websocket) { if (this.websocket) {
this.#websocket.close(); this.websocket.close();
this.#websocket = null; this.websocket = null;
} }
this.#websocket = new WebSocket('ws://127.0.0.1:' + this.#port); this.websocket = new WebSocket('ws://127.0.0.1:' + this.port);
this.#websocket.onopen = () => { this.websocket.onopen = () => {
const json = { const json = {
event: this.#messageType, event: this.messageType,
uuid: this.#uuid, uuid: this.uuid,
}; };
this.#websocket.send(JSON.stringify(json)); this.websocket.send(JSON.stringify(json));
this.#emit(CONNECTED, { this.emit(CONNECTED, {
connection: this.#websocket, connection: this.websocket,
port: this.#port, port: this.port,
uuid: this.#uuid, uuid: this.uuid,
actionInfo: this.#actionInfo, actionInfo: this.actionInfo,
appInfo: this.#appInfo, appInfo: this.appInfo,
messageType: this.#messageType, messageType: this.messageType,
}); });
}; };
this.#websocket.onerror = (evt) => { this.websocket.onerror = (evt) => {
const error = `WEBOCKET ERROR: ${evt}, ${evt.data}, ${SocketErrors[evt?.code]}`; const error = `WEBOCKET ERROR: ${evt}, ${evt.data}, ${SocketErrors[evt?.code]}`;
console.warn(error); console.warn(error);
this.log(error); this.log(error);
}; };
this.#websocket.onclose = (evt) => { this.websocket.onclose = (evt) => {
console.warn('WEBOCKET CLOSED:', SocketErrors[evt?.code]); console.warn('WEBOCKET CLOSED:', SocketErrors[evt?.code]);
}; };
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);
}; };
} }
@ -95,14 +95,14 @@ class ELGSDStreamDeck {
*/ */
logMessage(message) { logMessage(message) {
try { try {
if (this.#websocket) { if (this.websocket) {
const json = { const json = {
event: LOG_MESSAGE, event: LOG_MESSAGE,
payload: { payload: {
message: message, message: message,
}, },
}; };
this.#websocket.send(JSON.stringify(json)); this.websocket.send(JSON.stringify(json));
} }
} catch (e) { } catch (e) {
console.log('Websocket not defined'); console.log('Websocket not defined');
@ -115,15 +115,15 @@ class ELGSDStreamDeck {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async loadLocalization(pathPrefix) { async loadLocalization(pathPrefix) {
const manifest = await this.readJson(`${pathPrefix}${this.#language}.json`); const manifest = await this.readJson(`${pathPrefix}${this.language}.json`);
this.#localization = manifest['Localization'] ?? null; this.localization = manifest['Localization'] ?? null;
if (this.#messageType === REGISTER_PROPERTY_INSPECTOR && this.#localization) { if (this.messageType === REGISTER_PROPERTY_INSPECTOR && this.localization) {
const elements = document.querySelectorAll(DATA_LOCALIZE); const elements = document.querySelectorAll(DATA_LOCALIZE);
elements.forEach((element) => { elements.forEach((element) => {
element.textContent = element.textContent =
this.#localization[element.textContent] ?? element.textContent; this.localization[element.textContent] ?? element.textContent;
}); });
} }
} }
@ -162,7 +162,7 @@ class ELGSDStreamDeck {
*/ */
send(context, fn, 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)); this.websocket && this.websocket.send(JSON.stringify(pl));
} }
/** /**
@ -170,7 +170,7 @@ class ELGSDStreamDeck {
* @param context * @param context
*/ */
getSettings(context) { getSettings(context) {
this.send(context ?? this.#uuid, GET_SETTINGS, {}); this.send(context ?? this.uuid, GET_SETTINGS, {});
} }
/** /**
@ -179,7 +179,7 @@ class ELGSDStreamDeck {
* @param context * @param context
*/ */
setSettings(payload, context) { setSettings(payload, context) {
this.send(context ?? this.#uuid, SET_SETTINGS, { this.send(context ?? this.uuid, SET_SETTINGS, {
action: StreamDeck?.actionInfo?.action, action: StreamDeck?.actionInfo?.action,
payload: payload || {}, payload: payload || {},
targetContext: StreamDeck?.actionInfo?.context, targetContext: StreamDeck?.actionInfo?.context,
@ -190,7 +190,7 @@ 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, GET_GLOBAL_SETTINGS, {}); this.send(this.uuid, GET_GLOBAL_SETTINGS, {});
} }
/** /**
@ -198,7 +198,7 @@ class ELGSDStreamDeck {
* @param payload * @param payload
*/ */
setGlobalSettings(payload) { setGlobalSettings(payload) {
this.send(this.#uuid, SET_GLOBAL_SETTINGS, { this.send(this.uuid, SET_GLOBAL_SETTINGS, {
payload: payload, payload: payload,
}); });
} }
@ -208,7 +208,7 @@ class ELGSDStreamDeck {
* @param urlToOpen * @param urlToOpen
*/ */
openUrl(urlToOpen) { openUrl(urlToOpen) {
this.send(this.#uuid, OPEN_URL, { this.send(this.uuid, OPEN_URL, {
payload: { payload: {
url: urlToOpen, url: urlToOpen,
}, },
@ -222,7 +222,7 @@ class ELGSDStreamDeck {
*/ */
sendToPlugin(payload, context) { sendToPlugin(payload, context) {
this.send( this.send(
context ?? this.#uuid, context ?? this.uuid,
SEND_TO_PLUGIN, SEND_TO_PLUGIN,
{ {
action: StreamDeck?.actionInfo?.action, action: StreamDeck?.actionInfo?.action,
@ -284,7 +284,7 @@ class ELGSDStreamDeck {
*/ */
sendToPropertyInspector(payload, context) { sendToPropertyInspector(payload, context) {
this.send(context, SEND_TO_PROPERTY_INSPECTOR, { this.send(context, SEND_TO_PROPERTY_INSPECTOR, {
action: this.#actionInfo.action, action: this.actionInfo.action,
payload: payload, payload: payload,
}); });
} }
@ -309,7 +309,7 @@ class ELGSDStreamDeck {
* @param {*} fn * @param {*} fn
*/ */
onConnected(fn) { onConnected(fn) {
this.#on(CONNECTED, (jsn) => fn(jsn)); this.on(CONNECTED, (jsn) => fn(jsn));
return this; return this;
} }
@ -318,7 +318,7 @@ class ELGSDStreamDeck {
* @param fn * @param fn
*/ */
onSendToPropertyInspector(fn) { onSendToPropertyInspector(fn) {
this.#on(SEND_TO_PROPERTY_INSPECTOR, (jsn) => fn(jsn)); this.on(SEND_TO_PROPERTY_INSPECTOR, (jsn) => fn(jsn));
return this; return this;
} }
@ -327,7 +327,7 @@ class ELGSDStreamDeck {
* @param {*} fn * @param {*} fn
*/ */
onDeviceDidConnect(fn) { onDeviceDidConnect(fn) {
this.#on(DEVICE_DID_CONNECT, (jsn) => fn(jsn)); this.on(DEVICE_DID_CONNECT, (jsn) => fn(jsn));
return this; return this;
} }
@ -336,7 +336,7 @@ class ELGSDStreamDeck {
* @param {*} fn * @param {*} fn
*/ */
onDeviceDidDisconnect(fn) { onDeviceDidDisconnect(fn) {
this.#on(DEVICE_DID_DISCONNECT, (jsn) => fn(jsn)); this.on(DEVICE_DID_DISCONNECT, (jsn) => fn(jsn));
return this; return this;
} }
@ -345,7 +345,7 @@ class ELGSDStreamDeck {
* @param {*} fn * @param {*} fn
*/ */
onApplicationDidLaunch(fn) { onApplicationDidLaunch(fn) {
this.#on(APPLICATION_DID_LAUNCH, (jsn) => fn(jsn)); this.on(APPLICATION_DID_LAUNCH, (jsn) => fn(jsn));
return this; return this;
} }
@ -354,7 +354,7 @@ class ELGSDStreamDeck {
* @param {*} fn * @param {*} fn
*/ */
onApplicationDidTerminate(fn) { onApplicationDidTerminate(fn) {
this.#on(APPLICATION_DID_TERMINATE, (jsn) => fn(jsn)); this.on(APPLICATION_DID_TERMINATE, (jsn) => fn(jsn));
return this; return this;
} }
@ -363,7 +363,7 @@ class ELGSDStreamDeck {
* @param {*} fn * @param {*} fn
*/ */
onSystemDidWakeUp(fn) { onSystemDidWakeUp(fn) {
this.#on(SYSTEM_DID_WAKE_UP, (jsn) => fn(jsn)); this.on(SYSTEM_DID_WAKE_UP, (jsn) => fn(jsn));
return this; return this;
} }
} }