removed static event handler

This commit is contained in:
Zack Hoherchak 2022-02-15 15:37:04 -05:00
commit c2bfece006
8 changed files with 79 additions and 75 deletions

View file

@ -57,7 +57,7 @@
<script src="../../../libs/js/utils.js"></script>
<script src="../../../libs/js/constants.js"></script>
<script src="../../../libs/js/event-handler.js"></script>
<script src="../../../libs/js/deck.js"></script>
<script src="../../../libs/js/stream-deck.js"></script>
<script src="../../../libs/js/dynamic-styles.js"></script>
<script src="inspector.js"></script>
</body>

View file

@ -1,26 +1,24 @@
/// <reference path="../../../libs/js/deck.js" />
/// <reference path="../../../libs/js/stream-deck.js" />
/// <reference path="../../../libs/js/utils.js" />
const streamDeck = new StreamDeck();
StreamDeck.onConnected((jsn) => {
StreamDeck.loadLocalization('../../../');
streamDeck.onConnected((jsn) => {
streamDeck.loadLocalization('../../../');
const form = document.querySelector('#property-inspector');
const {actionInfo, appInfo, connection, messageType, port, uuid} = jsn;
const {payload, context} = actionInfo;
const {settings} = payload;
const form = document.querySelector('#property-inspector');
const { actionInfo, appInfo, connection, messageType, port, uuid } = jsn;
const { payload, context } = actionInfo;
const { settings } = payload;
Utils.setFormValue(settings, form);
Utils.setFormValue(settings, form);
form.addEventListener(
'input',
Utils.debounce(150, () => {
const value = Utils.getFormValue(form);
streamDeck.sendToPlugin(value, context);
streamDeck.setSettings(value);
})
);
form.addEventListener(
'input',
Utils.debounce(150, () => {
const value = Utils.getFormValue(form);
StreamDeck.sendToPlugin(value, context);
StreamDeck.setSettings(value);
})
);
});
/**
@ -28,9 +26,9 @@ streamDeck.onConnected((jsn) => {
* (this can be removed if the external window is not used)
*/
window.sendToInspector = (data) => {
console.log(data);
console.log(data);
};
document.querySelector('#open-external').addEventListener('click', () => {
window.open('../../../external.html');
window.open('../../../external.html');
});

View file

@ -14,7 +14,7 @@
<body>
<script src="libs/js/constants.js"></script>
<script src="libs/js/event-handler.js"></script>
<script src="libs/js/deck.js"></script>
<script src="libs/js/stream-deck.js"></script>
<script src="libs/js/action.js"></script>
<script src="app.js"></script>
</body>

View file

@ -1,19 +1,17 @@
/// <reference path="libs/js/action.js" />
/// <reference path="libs/js/deck.js" />
const streamDeck = new StreamDeck();
/// <reference path="libs/js/stream-deck.js" />
/**
* The first event fired when Stream Deck starts
*/
streamDeck.onConnected(({actionInfo, appInfo, connection, messageType, port, uuid}) => {
StreamDeck.onConnected(({actionInfo, appInfo, connection, messageType, port, uuid}) => {
const myAction = new Action('com.elgato.template.action');
myAction.onKeyUp(({action, context, device, event, payload}) => {
streamDeck.openUrl('https://developer.elgato.com/documentation/stream-deck/sdk/overview/');
StreamDeck.openUrl('https://developer.elgato.com/documentation/stream-deck/sdk/overview/');
console.log('Your code goes here!');
streamDeck.showOk(context);
StreamDeck.showOk(context);
});
});

View file

@ -4,7 +4,7 @@
* @class Action
* A Stream Deck plugin action, where you can register callback functions for different events
*/
class Action {
class ELGSDAction {
UUID;
#on = EventHandler.on;
#removeListener = EventHandler.remove;
@ -159,3 +159,5 @@ class Action {
return this.#removeListener(`${this.UUID}.${eventName}`);
}
}
var Action = ELGSDAction;

View file

@ -8,7 +8,7 @@ const fadeColor = function (col, amt) {
return '#' + (g | (b << 8) | (r << 16)).toString(16).padStart(6, 0);
};
new StreamDeck().onConnected(({ appInfo }) => {
StreamDeck.onConnected(({ appInfo }) => {
if (appInfo?.colors) return;
const clrs = appInfo.colors;
const node = document.getElementById('#sdpi-dynamic-styles') || document.createElement('style');

View file

@ -1,41 +1,45 @@
class EventHandler {
static eventList = new Map();
class ELGSDEventHandler {
eventList = new Map();
static on(name, fn) {
if (!EventHandler.eventList.has(name)) {
EventHandler.eventList.set(name, EventHandler.pubSub());
}
on(name, fn) {
if (!EventHandler.eventList.has(name)) {
EventHandler.eventList.set(name, EventHandler.pubSub());
}
return EventHandler.eventList.get(name).sub(fn);
}
return EventHandler.eventList.get(name).sub(fn);
}
static remove(name) {
this.eventList.delete(name)
}
remove(name) {
this.eventList.delete(name);
}
static removeAll(name) {
Array.from(this.eventList.keys()).filter(key => key.startsWith(name)).forEach(key => this.eventList.delete(key))
}
removeAll(name) {
Array.from(this.eventList.keys())
.filter((key) => key.startsWith(name))
.forEach((key) => this.eventList.delete(key));
}
static has(name) {
return EventHandler.eventList.has(name);
}
has(name) {
return EventHandler.eventList.has(name);
}
static emit(name, data) {
return EventHandler.eventList.has(name) && EventHandler.eventList.get(name).pub(data);
}
emit(name, data) {
return EventHandler.eventList.has(name) && EventHandler.eventList.get(name).pub(data);
}
static pubSub() {
const subscribers = new Set();
pubSub() {
const subscribers = new Set();
const sub = (fn) => {
subscribers.add(fn);
return () => {
subscribers.delete(fn);
};
};
const sub = (fn) => {
subscribers.add(fn);
return () => {
subscribers.delete(fn);
};
};
const pub = (data) => subscribers.forEach((fn) => fn(data));
return Object.freeze({pub, sub});
}
const pub = (data) => subscribers.forEach((fn) => fn(data));
return Object.freeze({ pub, sub });
}
}
var EventHandler = new ELGSDEventHandler();

View file

@ -6,7 +6,7 @@
* StreamDeck object containing all required code to establish
* communication with SD-Software and the Property Inspector
*/
class StreamDeck {
class ELGSDStreamDeck {
#port;
#uuid;
#messageType;
@ -21,11 +21,11 @@ class StreamDeck {
#removeAllListeners = EventHandler.removeAll;
constructor() {
if (StreamDeck.__instance) {
return StreamDeck.__instance;
if (ELGSDStreamDeck.__instance) {
return ELGSDStreamDeck.__instance;
}
StreamDeck.__instance = this;
ELGSDStreamDeck.__instance = this;
}
/**
@ -330,6 +330,8 @@ class StreamDeck {
}
}
var StreamDeck = new ELGSDStreamDeck();
/**
* connectElgatoStreamDeckSocket
* This is the first function StreamDeck Software calls, when
@ -341,5 +343,5 @@ class StreamDeck {
* @param {string} actionInfo - Context is an internal identifier used to communicate to the host application.
*/
function connectElgatoStreamDeckSocket() {
new StreamDeck().connect(arguments);
StreamDeck.connect(arguments);
}