-
Notifications
You must be signed in to change notification settings - Fork 14
/
nodecg-widget-obs-controls.js
119 lines (104 loc) · 2.95 KB
/
nodecg-widget-obs-controls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* `nodecg-widget-obs-controls`
* The controls for a specific connection to OBS.
*
* @customElement
* @polymer
*/
class NodecgWidgetObsControls extends Polymer.Element {
static get is() {
return 'nodecg-widget-obs-controls';
}
static get properties() {
return {
namespace: {
type: String,
observer: '_namespaceChanged'
},
websocketReplicant: {
type: Object,
readOnly: true
},
status: {
type: String,
value: 'disconnected',
observer: '_statusChanged',
readOnly: true,
notify: true
},
ipAddress: String,
port: Number,
password: String
};
}
constructor() {
super();
this._handleWebsocketReplicantChange = this._handleWebsocketReplicantChange.bind(this);
}
ready() {
super.ready();
this.$.connect.disabled = true;
}
connect() {
nodecg.sendMessage(`${this.namespace}:connect`, {
ip: this.ipAddress,
port: parseInt(this.port, 10),
password: this.password
}).catch(err => {
this.dispatchEvent(new CustomEvent('connection-failure', {
detail: err,
bubbles: true,
composed: true
}));
});
}
disconnect() {
nodecg.sendMessage(`${this.namespace}:disconnect`);
}
_namespaceChanged(newNamespace) {
if (this.websocketReplicant) {
this.websocketReplicant.removeListener('change', this._handleWebsocketReplicantChange);
}
this._setWebsocketReplicant(nodecg.Replicant(`${newNamespace}:websocket`));
this.websocketReplicant.on('change', this._handleWebsocketReplicantChange);
}
_statusChanged(newStatus, oldStatus) {
this.$.connect.hidden = newStatus === 'connected' || newStatus === 'connecting';
this.$['indicator-status-online'].hidden = newStatus !== 'connected';
this.$.disconnect.hidden = newStatus !== 'connected';
this.$['indicator-status-offline'].hidden = newStatus !== 'disconnected' && newStatus !== 'error';
this.$.abort.hidden = newStatus !== 'connecting';
this.$['indicator-status-connecting'].hidden = newStatus !== 'connecting';
if (oldStatus === 'connected') {
this.dispatchEvent(new CustomEvent('disconnected', {bubbles: true, composed: true}));
} else if (newStatus === 'connected') {
this.dispatchEvent(new CustomEvent('connected', {bubbles: true, composed: true}));
}
switch (newStatus) {
case 'connected':
this.$['indicator-led'].color = 'green';
break;
case 'connecting':
this.$['indicator-led'].color = 'yellow';
break;
case 'disconnected':
case 'error':
this.$['indicator-led'].color = 'red';
break;
/* istanbul ignore next */
default:
// Do nothing.
}
}
_handleWebsocketReplicantChange(newVal) {
this.ipAddress = newVal.ip;
this.port = newVal.port;
this.password = newVal.password;
this.disableControls = newVal.status === 'connected' || newVal.status === 'connecting';
this._setStatus(newVal.status);
}
_calcConnectDisabled(ipAddress, port) {
return !ipAddress || !port;
}
}
window.customElements.define(NodecgWidgetObsControls.is, NodecgWidgetObsControls);