This commit is contained in:
Ondřej Žára 2020-05-07 20:06:43 +02:00
parent 8d89fc8ab9
commit 7ed5317756
6 changed files with 26 additions and 44 deletions

View file

@ -237,7 +237,7 @@ function pathContents(lines) {
return result; return result;
} }
let ws; let ws, app;
let commandQueue = []; let commandQueue = [];
let current; let current;
let canTerminateIdle = false; let canTerminateIdle = false;
@ -287,10 +287,7 @@ async function idle() {
canTerminateIdle = false; canTerminateIdle = false;
let changed = linesToStruct(lines).changed || []; let changed = linesToStruct(lines).changed || [];
changed = [].concat(changed); changed = [].concat(changed);
if (changed.length > 0) { (changed.length > 0) && app.dispatchEvent(new CustomEvent("idle-change", {detail:changed}));
// FIXME not on window
window.dispatchEvent(new CustomEvent("idle-change", {detail:changed}));
}
} }
async function command(cmd) { async function command(cmd) {
@ -403,14 +400,13 @@ function escape(str) {
return str.replace(/(['"\\])/g, "\\$1"); return str.replace(/(['"\\])/g, "\\$1");
} }
async function init() { async function init(a) {
app = a;
let response = await fetch("/ticket", {method:"POST"}); let response = await fetch("/ticket", {method:"POST"});
let ticket = (await response.json()).ticket; let ticket = (await response.json()).ticket;
let resolve, reject; return new Promise((resolve, reject) => {
let promise = new Promise((res, rej) => { current = {resolve, reject};
resolve = res;
reject = rej;
try { try {
let url = new URL(location.href); let url = new URL(location.href);
@ -424,9 +420,6 @@ async function init() {
ws.addEventListener("message", onMessage); ws.addEventListener("message", onMessage);
ws.addEventListener("close", onClose); ws.addEventListener("close", onClose);
}); });
current = {resolve, reject, promise};
return Promise;
} }
var mpd = /*#__PURE__*/Object.freeze({ var mpd = /*#__PURE__*/Object.freeze({
@ -692,9 +685,9 @@ function initIcons() {
}); });
} }
async function initMpd() { async function initMpd(app) {
try { try {
await init(); await init(app);
return mpd; return mpd;
} catch (e) { } catch (e) {
return mpdMock; return mpdMock;
@ -711,7 +704,7 @@ class App extends HTMLElement {
} }
async connectedCallback() { async connectedCallback() {
this.mpd = await initMpd(); this.mpd = await initMpd(this);
const children = Array.from(this.querySelectorAll("*")); const children = Array.from(this.querySelectorAll("*"));
const names = children.map(node => node.nodeName.toLowerCase()) const names = children.map(node => node.nodeName.toLowerCase())
@ -742,7 +735,7 @@ class App extends HTMLElement {
} }
get component() { return this.getAttribute("component"); } 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); customElements.define("cyp-app", App);
@ -996,7 +989,7 @@ class Player extends Component {
this._addEvents(); this._addEvents();
this._updateStatus(); this._updateStatus();
this._updateCurrent(); this._updateCurrent();
window.addEventListener("idle-change", this); this._app.addEventListener("idle-change", this);
setInterval(() => this._updateElapsed(), ELAPSED_PERIOD); setInterval(() => this._updateElapsed(), ELAPSED_PERIOD);
} }
@ -1233,10 +1226,8 @@ class Queue extends Component {
} }
_onAppLoad() { _onAppLoad() {
window.addEventListener("idle-change", this); this._app.addEventListener("idle-change", this);
this._app.addEventListener("song-change", this); this._app.addEventListener("song-change", this);
this._sync(); this._sync();
} }
@ -1343,7 +1334,7 @@ class Playlists extends Component {
} }
_onAppLoad() { _onAppLoad() {
window.addEventListener("idle-change", this); this._app.addEventListener("idle-change", this);
this._sync(); this._sync();
} }

View file

@ -11,9 +11,9 @@ function initIcons() {
}); });
} }
async function initMpd() { async function initMpd(app) {
try { try {
await mpd.init(); await mpd.init(app);
return mpd; return mpd;
} catch (e) { } catch (e) {
return mpdMock; return mpdMock;
@ -30,7 +30,7 @@ class App extends HTMLElement {
} }
async connectedCallback() { async connectedCallback() {
this.mpd = await initMpd(); this.mpd = await initMpd(this);
const children = Array.from(this.querySelectorAll("*")); const children = Array.from(this.querySelectorAll("*"));
const names = children.map(node => node.nodeName.toLowerCase()) const names = children.map(node => node.nodeName.toLowerCase())
@ -61,7 +61,7 @@ class App extends HTMLElement {
} }
get component() { return this.getAttribute("component"); } 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); customElements.define("cyp-app", App);

View file

@ -41,7 +41,7 @@ class Player extends Component {
this._addEvents(); this._addEvents();
this._updateStatus(); this._updateStatus();
this._updateCurrent(); this._updateCurrent();
window.addEventListener("idle-change", this); this._app.addEventListener("idle-change", this);
setInterval(() => this._updateElapsed(), ELAPSED_PERIOD); setInterval(() => this._updateElapsed(), ELAPSED_PERIOD);
} }

View file

@ -18,7 +18,7 @@ class Playlists extends Component {
} }
_onAppLoad() { _onAppLoad() {
window.addEventListener("idle-change", this); this._app.addEventListener("idle-change", this);
this._sync(); this._sync();
} }

View file

@ -37,10 +37,8 @@ class Queue extends Component {
} }
_onAppLoad() { _onAppLoad() {
window.addEventListener("idle-change", this); this._app.addEventListener("idle-change", this);
this._app.addEventListener("song-change", this); this._app.addEventListener("song-change", this);
this._sync(); this._sync();
} }

View file

@ -1,6 +1,6 @@
import * as parser from "./parser.js"; import * as parser from "./parser.js";
let ws; let ws, app;
let commandQueue = []; let commandQueue = [];
let current; let current;
let canTerminateIdle = false; let canTerminateIdle = false;
@ -50,10 +50,7 @@ async function idle() {
canTerminateIdle = false; canTerminateIdle = false;
let changed = parser.linesToStruct(lines).changed || []; let changed = parser.linesToStruct(lines).changed || [];
changed = [].concat(changed); changed = [].concat(changed);
if (changed.length > 0) { (changed.length > 0) && app.dispatchEvent(new CustomEvent("idle-change", {detail:changed}));
// FIXME not on window
window.dispatchEvent(new CustomEvent("idle-change", {detail:changed}));
}
} }
export async function command(cmd) { export async function command(cmd) {
@ -166,14 +163,13 @@ export function escape(str) {
return str.replace(/(['"\\])/g, "\\$1"); return str.replace(/(['"\\])/g, "\\$1");
} }
export async function init() { export async function init(a) {
app = a;
let response = await fetch("/ticket", {method:"POST"}); let response = await fetch("/ticket", {method:"POST"});
let ticket = (await response.json()).ticket; let ticket = (await response.json()).ticket;
let resolve, reject; return new Promise((resolve, reject) => {
let promise = new Promise((res, rej) => { current = {resolve, reject};
resolve = res;
reject = rej;
try { try {
let url = new URL(location.href); let url = new URL(location.href);
@ -187,7 +183,4 @@ export async function init() {
ws.addEventListener("message", onMessage); ws.addEventListener("message", onMessage);
ws.addEventListener("close", onClose); ws.addEventListener("close", onClose);
}); });
current = {resolve, reject, promise};
return Promise;
} }