Update WebNowPlaying plugin. (#405)

* [WebNowPlaying] Remove unnecessary .bind, general code and type cleanup

* bleh

* bleh 2
This commit is contained in:
Jozen Blue Martinez 2022-02-10 02:12:59 +08:00 committed by GitHub
parent 4436304e45
commit 75221bc984
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,7 +14,7 @@ const pad = (number: number, length: number) => String(number).padStart(length,
* @param {Number} timeInSeconds
* @returns String
*/
const convertTimeToString = (timeInSeconds: number) => {
const convertTimeToString = (timeInSeconds: number) => {
const timeInMinutes = Math.floor(timeInSeconds / 60);
if (timeInMinutes < 60) {
return timeInMinutes + ":" + pad(Math.floor(timeInSeconds % 60), 2);
@ -28,13 +28,13 @@ export default class WebNowPlaying {
*/
public name: string = 'WebNowPlaying';
public description: string = 'Song info and playback control for the Rainmeter WebNowPlaying plugin.';
public version: string = '1.0.0';
public version: string = '1.0.1';
public author: string = 'Zennn <me@jozen.blue>';
private _win: any;
private ws: any = null;
private wsapiConn: any = null;
private playerName: string = 'Cider'/* Apple Music */;
private ws?: WebSocket;
private wsapiConn?: WebSocket;
private playerName: string = 'Cider';
constructor() {
console.debug(`[Plugin][${this.name}] Loading Complete.`);
@ -47,9 +47,7 @@ export default class WebNowPlaying {
*/
private static windowsOnly(_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {
if (process.platform !== 'win32') {
descriptor.value = function () {
return
}
descriptor.value = () => void 0;
}
}
@ -92,22 +90,25 @@ export default class WebNowPlaying {
value = attributes.shuffleMode;
break;
}
this.ws.send(`${field}:${value}`);
this.ws?.send(`${field}:${value}`);
} catch (error) {
if (this.ws.readyState === WebSocket.OPEN) {
if (this.ws?.readyState === WebSocket.OPEN) {
this.ws.send(`Error:Error updating ${field} for ${this.playerName}`);
this.ws.send(`ErrorD:${error}`);
}
}
});
}
private fireEvent(evt: any) {
private fireEvent(evt: WebSocket.MessageEvent) {
if (!evt.data) return;
let value = '';
if (evt.data.split(/ (.+)/).length > 1) {
value = evt.data.split(/ (.+)/)[1];
const data = <string>evt.data;
let value: string = '';
if (data.split(/ (.+)/).length > 1) {
value = data.split(/ (.+)/)[1];
}
const eventName = evt.data.split(' ')[0].toLowerCase();
const eventName = data.split(' ')[0].toLowerCase();
try {
switch (eventName) {
@ -144,7 +145,7 @@ export default class WebNowPlaying {
}
} catch (error) {
console.debug(error);
if (this.ws.readyState === WebSocket.OPEN) {
if (this.ws?.readyState === WebSocket.OPEN) {
this.ws.send(`Error:Error sending event to ${this.playerName}`);
this.ws.send(`ErrorD:${error}`);
}
@ -163,10 +164,10 @@ export default class WebNowPlaying {
try {
this.ws = new WebSocket('ws://127.0.0.1:8974/');
let retry: NodeJS.Timeout;
this.ws.onopen = (() => {
this.ws.onopen = () => {
console.info('[WebNowPlaying] Connected to Rainmeter');
this.ws.send(`PLAYER:${this.playerName}`);
}).bind(this);
this.ws?.send(`PLAYER:${this.playerName}`);
};
this.ws.onclose = () => {
clearTimeout(retry);
@ -175,7 +176,7 @@ export default class WebNowPlaying {
this.ws.onerror = () => {
clearTimeout(retry);
this.ws.close();
this.ws?.close();
};
this.ws.onmessage = this.fireEvent?.bind(this);
@ -194,8 +195,8 @@ export default class WebNowPlaying {
console.info('[WebNowPlaying] Connected to wsapi');
};
this.wsapiConn.onmessage = (evt: { data: string; }) => {
const response = JSON.parse(evt.data);
this.wsapiConn.onmessage = (evt: WebSocket.MessageEvent) => {
const response = JSON.parse(<string>evt.data);
if (response.type === 'playbackStateUpdate') {
this.sendSongInfo(response.data);
}
@ -214,8 +215,8 @@ export default class WebNowPlaying {
public onBeforeQuit() {
if (this.ws) {
this.ws.send('STATE:0');
this.ws.onclose = null; // disable onclose handler first to stop it from retrying
this.ws.close();
this.ws.onclose = () => void 0; // disable onclose handler first to stop it from retrying
this.ws.close();
}
if (this.wsapiConn) {
this.wsapiConn.close();
@ -227,7 +228,8 @@ export default class WebNowPlaying {
* Runs on playback State Change
* @param attributes Music Attributes (attributes.status = current state)
*/
onPlaybackStateDidChange(attributes: any) {
@WebNowPlaying.windowsOnly
public onPlaybackStateDidChange(attributes: any) {
this.sendSongInfo(attributes);
}