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/utils.js"></script>
<script src="../../../libs/js/constants.js"></script> <script src="../../../libs/js/constants.js"></script>
<script src="../../../libs/js/event-handler.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="../../../libs/js/dynamic-styles.js"></script>
<script src="inspector.js"></script> <script src="inspector.js"></script>
</body> </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" /> /// <reference path="../../../libs/js/utils.js" />
const streamDeck = new StreamDeck(); StreamDeck.onConnected((jsn) => {
StreamDeck.loadLocalization('../../../');
streamDeck.onConnected((jsn) => { const form = document.querySelector('#property-inspector');
streamDeck.loadLocalization('../../../'); const {actionInfo, appInfo, connection, messageType, port, uuid} = jsn;
const {payload, context} = actionInfo;
const {settings} = payload;
const form = document.querySelector('#property-inspector'); Utils.setFormValue(settings, form);
const { actionInfo, appInfo, connection, messageType, port, uuid } = jsn;
const { payload, context } = actionInfo;
const { settings } = payload;
Utils.setFormValue(settings, form); form.addEventListener(
'input',
form.addEventListener( Utils.debounce(150, () => {
'input', const value = Utils.getFormValue(form);
Utils.debounce(150, () => { StreamDeck.sendToPlugin(value, context);
const value = Utils.getFormValue(form); StreamDeck.setSettings(value);
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) * (this can be removed if the external window is not used)
*/ */
window.sendToInspector = (data) => { window.sendToInspector = (data) => {
console.log(data); console.log(data);
}; };
document.querySelector('#open-external').addEventListener('click', () => { document.querySelector('#open-external').addEventListener('click', () => {
window.open('../../../external.html'); window.open('../../../external.html');
}); });

View file

@ -14,7 +14,7 @@
<body> <body>
<script src="libs/js/constants.js"></script> <script src="libs/js/constants.js"></script>
<script src="libs/js/event-handler.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="libs/js/action.js"></script>
<script src="app.js"></script> <script src="app.js"></script>
</body> </body>

View file

@ -1,19 +1,17 @@
/// <reference path="libs/js/action.js" /> /// <reference path="libs/js/action.js" />
/// <reference path="libs/js/deck.js" /> /// <reference path="libs/js/stream-deck.js" />
const streamDeck = new StreamDeck();
/** /**
* The first event fired when Stream Deck starts * 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'); const myAction = new Action('com.elgato.template.action');
myAction.onKeyUp(({action, context, device, event, payload}) => { 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!'); console.log('Your code goes here!');
streamDeck.showOk(context); StreamDeck.showOk(context);
}); });
}); });

View file

@ -4,7 +4,7 @@
* @class Action * @class Action
* A Stream Deck plugin action, where you can register callback functions for different events * A Stream Deck plugin action, where you can register callback functions for different events
*/ */
class Action { class ELGSDAction {
UUID; UUID;
#on = EventHandler.on; #on = EventHandler.on;
#removeListener = EventHandler.remove; #removeListener = EventHandler.remove;
@ -159,3 +159,5 @@ class Action {
return this.#removeListener(`${this.UUID}.${eventName}`); 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); return '#' + (g | (b << 8) | (r << 16)).toString(16).padStart(6, 0);
}; };
new StreamDeck().onConnected(({ appInfo }) => { StreamDeck.onConnected(({ appInfo }) => {
if (appInfo?.colors) return; if (appInfo?.colors) return;
const clrs = appInfo.colors; const clrs = appInfo.colors;
const node = document.getElementById('#sdpi-dynamic-styles') || document.createElement('style'); const node = document.getElementById('#sdpi-dynamic-styles') || document.createElement('style');

View file

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

View file

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