fixed event bugs

This commit is contained in:
Zack Hoherchak 2022-02-23 18:30:52 -05:00
commit 263457dfb2
8 changed files with 571 additions and 547 deletions

View file

@ -48,7 +48,7 @@
<div class="sdpi-info-label hidden" style="top: -1000" value=""></div> <div class="sdpi-info-label hidden" style="top: -1000" value=""></div>
<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-emitter.js"></script>
<script src="../../../libs/js/stream-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>

View file

@ -13,7 +13,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-emitter.js"></script>
<script src="libs/js/stream-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>

View file

@ -5,13 +5,62 @@
* 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'); // TODO: Remove this test logging code and replace with simple action
console.log({ object: 'stream-deck', event: 'onConnected' });
myAction.onKeyUp(({action, context, device, event, payload}) => { new Action('com.elgato.template.action')
StreamDeck.openUrl('https://developer.elgato.com/documentation/stream-deck/sdk/overview/'); .onDidReceiveGlobalSettings(() => {
console.log({ object: 'action', event: 'onDidReceiveGlobalSettings' });
})
.onDidReceiveSettings(() => {
console.log({ object: 'action', event: 'onDidReceiveSettings' });
})
.onKeyDown(() => {
console.log({ object: 'action', event: 'onKeyDown' });
})
.onKeyUp(() => {
console.log({ object: 'action', event: 'onKeyUp' });
})
.onWillAppear(() => {
console.log({ object: 'action', event: 'onWillAppear' });
})
.onWillDisappear(() => {
console.log({ object: 'action', event: 'onWillDisappear' });
})
.onTitleParametersDidChange(() => {
console.log({ object: 'action', event: 'onTitleParametersDidChange' });
})
.onPropertyInspectorDidAppear(() => {
console.log({ object: 'action', event: 'onPropertyInspectorDidAppear' });
})
.onPropertyInspectorDidDisappear(() => {
console.log({ object: 'action', event: 'onPropertyInspectorDidDisappear' });
})
.onSendToPlugin(() => {
console.log({ object: 'action', event: 'onSendToPlugin' });
})
.onSendToPropertyInspector(() => {
console.log({ object: 'action', event: 'onSendToPropertyInspector' });
});
console.log('Your code goes here!'); StreamDeck.onDeviceDidConnect(() => {
console.log({ object: 'stream-deck', event: 'onDeviceDidConnect' });
StreamDeck.showOk(context); })
.onDeviceDidDisconnect(() => {
console.log({ object: 'stream-deck', event: 'onDeviceDidDisconnect' });
})
.onSendToPropertyInspector(() => {
console.log({ object: 'stream-deck', event: 'onSendToPropertyInspector' });
})
.onApplicationDidLaunch(() => {
// TODO: figure out why this event does not fire
console.log({ object: 'stream-deck', event: 'onApplicationDidLaunch' });
})
.onApplicationDidTerminate(() => {
// TODO: figure out why this event does not fire
console.log({ object: 'stream-deck', event: 'onApplicationDidTerminate' });
})
.onSystemDidWakeUp(() => {
console.log({ object: 'stream-deck', event: 'onSystemDidWakeUp' });
}); });
}); });

View file

@ -6,9 +6,7 @@
*/ */
class ELGSDAction { class ELGSDAction {
UUID; UUID;
#on = EventHandler.on; #on = EventEmitter.on;
#removeListener = EventHandler.remove;
#removeAllListeners = EventHandler.removeAll;
constructor(UUID) { constructor(UUID) {
this.UUID = UUID; this.UUID = UUID;
@ -51,7 +49,7 @@ class ELGSDAction {
} }
/** /**
* Registers a callback function for the willAppear event, which fires when an action appears on they key * Registers a callback function for the willAppear event, which fires when an action appears on the canvas
* @param {*} fn * @param {*} fn
*/ */
onWillAppear(fn) { onWillAppear(fn) {
@ -59,6 +57,15 @@ class ELGSDAction {
return this; return this;
} }
/**
* Registers a callback function for the willAppear event, which fires when an action disappears on the canvas
* @param {*} fn
*/
onWillDisappear(fn) {
this.#on(`${this.UUID}.${WILL_DISAPPEAR}`, (jsn) => fn(jsn));
return this;
}
/** /**
* Registers a callback function for the titleParametersDidChange event, which fires when a user changes the key title * Registers a callback function for the titleParametersDidChange event, which fires when a user changes the key title
* @param {*} fn * @param {*} fn
@ -69,52 +76,7 @@ class ELGSDAction {
} }
/** /**
* Registers a callback function for the deviceDidConnect event, which fires when a device is plugged in * Registers a callback function for the propertyInspectorDidAppear event, which fires when the property inspector is displayed
* @param {*} fn
*/
onDeviceDidConnect(fn) {
this.#on(`${this.UUID}.${DEVICE_DID_CONNECT}`, (jsn) => fn(jsn));
return this;
}
/**
* Registers a callback function for the deviceDidDisconnect event, which fires fires when a device is unplugged
* @param {*} fn
*/
onDeviceDidDisconnect(fn) {
this.#on(`${this.UUID}.${DEVICE_DID_DISCONNECT}`, (jsn) => fn(jsn));
return this;
}
/**
* Registers a callback function for the applicationDidLaunch event, which fires fires when the application starts
* @param {*} fn
*/
onApplicationDidLaunch(fn) {
this.#on(`${this.UUID}.${APPLICATION_DID_LAUNCH}`, (jsn) => fn(jsn));
return this;
}
/**
* Registers a callback function for the applicationDidTerminate event, which fires fires when the application exits
* @param {*} fn
*/
onApplicationDidTerminate(fn) {
this.#on(`${this.UUID}.${APPLICATION_DID_TERMINATE}`, (jsn) => fn(jsn));
return this;
}
/**
* Registers a callback function for the systemDidWakeUp event, which fires fires when the computer wakes
* @param {*} fn
*/
onSystemDidWakeUp(fn) {
this.#on(`${this.UUID}.${SYSTEM_DID_WAKE_UP}`, (jsn) => fn(jsn));
return this;
}
/**
* Registers a callback function for the propertyInspectorDidAppear event, which fires fires when the property inspector is displayed
* @param {*} fn * @param {*} fn
*/ */
onPropertyInspectorDidAppear(fn) { onPropertyInspectorDidAppear(fn) {
@ -123,7 +85,7 @@ class ELGSDAction {
} }
/** /**
* Registers a callback function for the propertyInspectorDidDisappear event, which fires fires when the property inspector is closed * Registers a callback function for the propertyInspectorDidDisappear event, which fires when the property inspector is closed
* @param {*} fn * @param {*} fn
*/ */
onPropertyInspectorDidDisappear(fn) { onPropertyInspectorDidDisappear(fn) {
@ -132,7 +94,7 @@ class ELGSDAction {
} }
/** /**
* Registers a callback function for the sendToPlugin event, which fires fires when the property inspector uses the sendToPlugin api * Registers a callback function for the sendToPlugin event, which fires when the property inspector uses the sendToPlugin api
* @param {*} fn * @param {*} fn
*/ */
onSendToPlugin(fn) { onSendToPlugin(fn) {
@ -141,23 +103,13 @@ class ELGSDAction {
} }
/** /**
* Registers a callback function for the sendToPropertyInspector event, which fires fires when the plugin uses the sendToPropertyInspector api * Registers a callback function for the sendToPropertyInspector event, which fires when the plugin uses the sendToPropertyInspector api
* @param {*} fn * @param {*} fn
*/ */
onSendToPropertyInspector(fn) { onSendToPropertyInspector(fn) {
this.#on(`${this.UUID}.${SEND_TO_PROPERTY_INSPECTOR}`, (jsn) => fn(jsn)); this.#on(`${this.UUID}.${SEND_TO_PROPERTY_INSPECTOR}`, (jsn) => fn(jsn));
return this; return this;
} }
/**
* Removes the event listener
* @param eventName
*/
removeListener(eventName) {
if (!eventName) return this.#removeAllListeners(this.UUID);
if (eventName === DID_RECEIVE_GLOBAL_SETTINGS) return this.#removeListener(eventName);
return this.#removeListener(`${this.UUID}.${eventName}`);
}
} }
var Action = ELGSDAction; var Action = ELGSDAction;

View file

@ -26,6 +26,7 @@ const DID_RECEIVE_GLOBAL_SETTINGS = 'didReceiveGlobalSettings';
const KEY_DOWN = 'keyDown'; const KEY_DOWN = 'keyDown';
const KEY_UP = 'keyUp'; const KEY_UP = 'keyUp';
const WILL_APPEAR = 'willAppear'; const WILL_APPEAR = 'willAppear';
const WILL_DISAPPEAR = 'willDisappear';
const TITLE_PARAMETERS_DID_CHANGE = 'titleParametersDidChange'; const TITLE_PARAMETERS_DID_CHANGE = 'titleParametersDidChange';
const DEVICE_DID_CONNECT = 'deviceDidConnect'; const DEVICE_DID_CONNECT = 'deviceDidConnect';
const DEVICE_DID_DISCONNECT = 'deviceDidDisconnect'; const DEVICE_DID_DISCONNECT = 'deviceDidDisconnect';

View file

@ -0,0 +1,31 @@
class ELGSDEventEmitter {
eventList = new Map();
on(name, fn) {
if (!EventEmitter.eventList.has(name)) {
EventEmitter.eventList.set(name, EventEmitter.pubSub());
}
return EventEmitter.eventList.get(name).sub(fn);
}
emit(name, data) {
return EventEmitter.eventList.has(name) && EventEmitter.eventList.get(name).pub(data);
}
pubSub() {
const subscribers = new Set();
const sub = (fn) => {
subscribers.add(fn);
return () => {
subscribers.delete(fn);
};
};
const pub = (data) => subscribers.forEach((fn) => fn(data));
return Object.freeze({pub, sub});
}
}
var EventEmitter = new ELGSDEventEmitter();

View file

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

View file

@ -15,10 +15,8 @@ class ELGSDStreamDeck {
#language; #language;
#localization; #localization;
#appInfo; #appInfo;
#on = EventHandler.on; #on = EventEmitter.on;
#emit = EventHandler.emit; #emit = EventEmitter.emit;
#removeListener = EventHandler.remove;
#removeAllListeners = EventHandler.removeAll;
constructor() { constructor() {
if (ELGSDStreamDeck.__instance) { if (ELGSDStreamDeck.__instance) {
@ -310,6 +308,7 @@ class ELGSDStreamDeck {
*/ */
onConnected(fn) { onConnected(fn) {
this.#on(CONNECTED, (jsn) => fn(jsn)); this.#on(CONNECTED, (jsn) => fn(jsn));
return this;
} }
/** /**
@ -318,15 +317,52 @@ class ELGSDStreamDeck {
*/ */
onSendToPropertyInspector(fn) { onSendToPropertyInspector(fn) {
this.#on(SEND_TO_PROPERTY_INSPECTOR, (jsn) => fn(jsn)); this.#on(SEND_TO_PROPERTY_INSPECTOR, (jsn) => fn(jsn));
return this;
} }
/** /**
* Removes the event listener * Registers a callback function for the deviceDidConnect event, which fires when a device is plugged in
* @param eventName * @param {*} fn
*/ */
removeListener(eventName) { onDeviceDidConnect(fn) {
if (!eventName) return this.#removeAllListeners(); this.#on(DEVICE_DID_CONNECT, (jsn) => fn(jsn));
return this.#removeListener(eventName); return this;
}
/**
* Registers a callback function for the deviceDidDisconnect event, which fires when a device is unplugged
* @param {*} fn
*/
onDeviceDidDisconnect(fn) {
this.#on(DEVICE_DID_DISCONNECT, (jsn) => fn(jsn));
return this;
}
/**
* Registers a callback function for the applicationDidLaunch event, which fires when the application starts
* @param {*} fn
*/
onApplicationDidLaunch(fn) {
this.#on(APPLICATION_DID_LAUNCH, (jsn) => fn(jsn));
return this;
}
/**
* Registers a callback function for the applicationDidTerminate event, which fires when the application exits
* @param {*} fn
*/
onApplicationDidTerminate(fn) {
this.#on(APPLICATION_DID_TERMINATE, (jsn) => fn(jsn));
return this;
}
/**
* Registers a callback function for the systemDidWakeUp event, which fires when the computer wakes
* @param {*} fn
*/
onSystemDidWakeUp(fn) {
this.#on(SYSTEM_DID_WAKE_UP, (jsn) => fn(jsn));
return this;
} }
} }