removed static event handler
This commit is contained in:
parent
0a4fe9ee9c
commit
c2bfece006
8 changed files with 79 additions and 75 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
Loading…
Reference in a new issue