From 7ed531775659828de7420bce5838c2f1a47ec332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C5=BD=C3=A1ra?= Date: Thu, 7 May 2020 20:06:43 +0200 Subject: [PATCH] clenaup --- app/cyp.js | 35 +++++++++++++---------------------- app/js/elements/app.js | 8 ++++---- app/js/elements/player.js | 2 +- app/js/elements/playlists.js | 2 +- app/js/elements/queue.js | 4 +--- app/js/mpd.js | 19 ++++++------------- 6 files changed, 26 insertions(+), 44 deletions(-) diff --git a/app/cyp.js b/app/cyp.js index a9dba85..3c6b1d2 100644 --- a/app/cyp.js +++ b/app/cyp.js @@ -237,7 +237,7 @@ function pathContents(lines) { return result; } -let ws; +let ws, app; let commandQueue = []; let current; let canTerminateIdle = false; @@ -287,10 +287,7 @@ async function idle() { canTerminateIdle = false; let changed = linesToStruct(lines).changed || []; changed = [].concat(changed); - if (changed.length > 0) { - // FIXME not on window - window.dispatchEvent(new CustomEvent("idle-change", {detail:changed})); - } + (changed.length > 0) && app.dispatchEvent(new CustomEvent("idle-change", {detail:changed})); } async function command(cmd) { @@ -403,14 +400,13 @@ function escape(str) { return str.replace(/(['"\\])/g, "\\$1"); } -async function init() { +async function init(a) { + app = a; let response = await fetch("/ticket", {method:"POST"}); let ticket = (await response.json()).ticket; - let resolve, reject; - let promise = new Promise((res, rej) => { - resolve = res; - reject = rej; + return new Promise((resolve, reject) => { + current = {resolve, reject}; try { let url = new URL(location.href); @@ -424,9 +420,6 @@ async function init() { ws.addEventListener("message", onMessage); ws.addEventListener("close", onClose); }); - - current = {resolve, reject, promise}; - return Promise; } var mpd = /*#__PURE__*/Object.freeze({ @@ -692,9 +685,9 @@ function initIcons() { }); } -async function initMpd() { +async function initMpd(app) { try { - await init(); + await init(app); return mpd; } catch (e) { return mpdMock; @@ -711,7 +704,7 @@ class App extends HTMLElement { } async connectedCallback() { - this.mpd = await initMpd(); + this.mpd = await initMpd(this); const children = Array.from(this.querySelectorAll("*")); const names = children.map(node => node.nodeName.toLowerCase()) @@ -742,7 +735,7 @@ class App extends HTMLElement { } get component() { return this.getAttribute("component"); } - set component(component) { return this.setAttribute("component", component); } + set component(component) { this.setAttribute("component", component); } } customElements.define("cyp-app", App); @@ -996,7 +989,7 @@ class Player extends Component { this._addEvents(); this._updateStatus(); this._updateCurrent(); - window.addEventListener("idle-change", this); + this._app.addEventListener("idle-change", this); setInterval(() => this._updateElapsed(), ELAPSED_PERIOD); } @@ -1233,10 +1226,8 @@ class Queue extends Component { } _onAppLoad() { - window.addEventListener("idle-change", this); - + this._app.addEventListener("idle-change", this); this._app.addEventListener("song-change", this); - this._sync(); } @@ -1343,7 +1334,7 @@ class Playlists extends Component { } _onAppLoad() { - window.addEventListener("idle-change", this); + this._app.addEventListener("idle-change", this); this._sync(); } diff --git a/app/js/elements/app.js b/app/js/elements/app.js index ca0cac5..86653df 100644 --- a/app/js/elements/app.js +++ b/app/js/elements/app.js @@ -11,9 +11,9 @@ function initIcons() { }); } -async function initMpd() { +async function initMpd(app) { try { - await mpd.init(); + await mpd.init(app); return mpd; } catch (e) { return mpdMock; @@ -30,7 +30,7 @@ class App extends HTMLElement { } async connectedCallback() { - this.mpd = await initMpd(); + this.mpd = await initMpd(this); const children = Array.from(this.querySelectorAll("*")); const names = children.map(node => node.nodeName.toLowerCase()) @@ -61,7 +61,7 @@ class App extends HTMLElement { } get component() { return this.getAttribute("component"); } - set component(component) { return this.setAttribute("component", component); } + set component(component) { this.setAttribute("component", component); } } customElements.define("cyp-app", App); diff --git a/app/js/elements/player.js b/app/js/elements/player.js index a52f775..81285f0 100644 --- a/app/js/elements/player.js +++ b/app/js/elements/player.js @@ -41,7 +41,7 @@ class Player extends Component { this._addEvents(); this._updateStatus(); this._updateCurrent(); - window.addEventListener("idle-change", this); + this._app.addEventListener("idle-change", this); setInterval(() => this._updateElapsed(), ELAPSED_PERIOD); } diff --git a/app/js/elements/playlists.js b/app/js/elements/playlists.js index d7614a6..a4fad04 100644 --- a/app/js/elements/playlists.js +++ b/app/js/elements/playlists.js @@ -18,7 +18,7 @@ class Playlists extends Component { } _onAppLoad() { - window.addEventListener("idle-change", this); + this._app.addEventListener("idle-change", this); this._sync(); } diff --git a/app/js/elements/queue.js b/app/js/elements/queue.js index 339f9ec..11cf797 100644 --- a/app/js/elements/queue.js +++ b/app/js/elements/queue.js @@ -37,10 +37,8 @@ class Queue extends Component { } _onAppLoad() { - window.addEventListener("idle-change", this); - + this._app.addEventListener("idle-change", this); this._app.addEventListener("song-change", this); - this._sync(); } diff --git a/app/js/mpd.js b/app/js/mpd.js index 7f4dce2..719f1b2 100644 --- a/app/js/mpd.js +++ b/app/js/mpd.js @@ -1,6 +1,6 @@ import * as parser from "./parser.js"; -let ws; +let ws, app; let commandQueue = []; let current; let canTerminateIdle = false; @@ -50,10 +50,7 @@ async function idle() { canTerminateIdle = false; let changed = parser.linesToStruct(lines).changed || []; changed = [].concat(changed); - if (changed.length > 0) { - // FIXME not on window - window.dispatchEvent(new CustomEvent("idle-change", {detail:changed})); - } + (changed.length > 0) && app.dispatchEvent(new CustomEvent("idle-change", {detail:changed})); } export async function command(cmd) { @@ -166,14 +163,13 @@ export function escape(str) { return str.replace(/(['"\\])/g, "\\$1"); } -export async function init() { +export async function init(a) { + app = a; let response = await fetch("/ticket", {method:"POST"}); let ticket = (await response.json()).ticket; - let resolve, reject; - let promise = new Promise((res, rej) => { - resolve = res; - reject = rej; + return new Promise((resolve, reject) => { + current = {resolve, reject}; try { let url = new URL(location.href); @@ -187,7 +183,4 @@ export async function init() { ws.addEventListener("message", onMessage); ws.addEventListener("close", onClose); }); - - current = {resolve, reject, promise}; - return Promise; }