streamdeck-typescript
Version:
This library will help you build elgato stream deck plugins in typescript
174 lines • 5.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StreamDeckHandlerBase = void 0;
const event_manager_1 = require("../manager/event.manager");
const settings_manager_1 = require("../manager/settings.manager");
class StreamDeckHandlerBase {
constructor() {
this._documentReady = false;
this._connectionReady = false;
this._globalSettingsReady = false;
this._documentReadyInvoked = false;
this._connectionReadyInvoked = false;
this._globalSettingsInvoked = false;
this._onReadyInvoked = false;
this._debug = false;
this._cachedEvents = [];
this._settingsManager = new settings_manager_1.SettingsManager(this);
this._eventManager = event_manager_1.EventManager.INSTANCE;
if (this._sd_events)
for (let event of this._sd_events)
event('*', this);
window.connectElgatoStreamDeckSocket = (port, uuid, registerEvent, info, actionInfo) => {
this._port = port;
this._uuid = uuid;
this._registerEvent = registerEvent;
this._info = JSON.parse(info);
if (actionInfo) {
this._eventManager.callEvents('registerPi', '*', actionInfo);
}
this._connectElgatoStreamDeckSocket();
this._docReady(() => {
this._documentReady = true;
this._handleReadyState();
});
};
}
get port() {
return this._port;
}
get uuid() {
return this._uuid;
}
get registerEvent() {
return this._registerEvent;
}
get info() {
return this._info;
}
get settingsManager() {
return this._settingsManager;
}
get documentReady() {
return this._documentReady;
}
get connectionReady() {
return this._connectionReady;
}
get globalSettingsReady() {
return this._globalSettingsReady;
}
setSettings(settings, context) {
this.send('setSettings', {
context: context,
payload: settings,
});
}
requestSettings(context) {
this.send('getSettings', {
context: context,
});
}
setGlobalSettings(settings) {
this.send('setGlobalSettings', {
context: this._uuid,
payload: settings,
});
}
requestGlobalSettings() {
this.send('getGlobalSettings', {
context: this._uuid,
});
}
openUrl(url) {
this.send('openUrl', {
payload: { url },
});
}
logMessage(message) {
this.send('logMessage', {
payload: { message },
});
}
send(event, data) {
const eventToSend = Object.assign({ event }, data);
if (this._debug)
console.log(`SEND ${event}`, eventToSend, this._ws);
if (this._ws)
this._ws.send(JSON.stringify(eventToSend));
else {
if (this._debug)
console.error('COULD NOT SEND. CACHING FOR RESEND EVENT');
this._cachedEvents.push(JSON.stringify(eventToSend));
}
}
enableDebug() {
this._debug = true;
}
_docReady(fn) {
if (document.readyState === 'complete' ||
document.readyState === 'interactive') {
setTimeout(() => fn(), 1);
}
else {
document.addEventListener('DOMContentLoaded', fn);
}
}
_connectElgatoStreamDeckSocket() {
this._ws = new WebSocket('ws://127.0.0.1:' + this._port);
this._ws.onopen = () => this._open();
this._ws.onclose = () => {
this._eventManager.callEvents('connectionClosed');
};
this._ws.onmessage = (ev) => this._eventHandler(ev);
}
_open() {
this.send(this._registerEvent, { uuid: this._uuid });
if (this._cachedEvents.length >= 1) {
if (this._debug)
console.log('RESENDING CACHED EVENTS: ', this._cachedEvents);
for (let cachedEvent of this._cachedEvents) {
this._ws.send(cachedEvent);
}
}
this._connectionReady = true;
this._handleReadyState();
this.requestGlobalSettings();
}
_handleReadyState() {
if (this._connectionReady && !this._connectionReadyInvoked) {
this._connectionReadyInvoked = true;
this._eventManager.callEvents('connectionOpened');
}
if (this._documentReady && !this._documentReadyInvoked) {
this._documentReadyInvoked = true;
this._eventManager.callEvents('documentLoaded');
}
if (this._globalSettingsReady && !this._globalSettingsInvoked) {
this._globalSettingsInvoked = true;
this._eventManager.callEvents('globalSettingsAvailable', '*', this.settingsManager);
}
if (this._globalSettingsInvoked &&
this._documentReadyInvoked &&
this._connectionReadyInvoked &&
!this._onReadyInvoked) {
this._onReadyInvoked = true;
this._eventManager.callEvents('setupReady');
}
}
_eventHandler(ev) {
var _a;
const eventData = JSON.parse(ev.data);
const event = eventData.event;
if (this._debug)
console.log(`RECEIVE ${event}`, eventData, ev);
if (event === 'didReceiveGlobalSettings') {
this.settingsManager.cacheGlobalSettings(eventData.payload.settings);
this._globalSettingsReady = true;
this._handleReadyState();
}
this._eventManager.callEvents(event, (_a = eventData.action) !== null && _a !== void 0 ? _a : '*', eventData);
}
}
exports.StreamDeckHandlerBase = StreamDeckHandlerBase;
//# sourceMappingURL=stream-deck-handler-base.js.map