Skip to content

Commit

Permalink
new field init system, debugging playlists field async problems
Browse files Browse the repository at this point in the history
  • Loading branch information
hinanaya committed Jul 4, 2024
1 parent f41f1cc commit e9feb57
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
4 changes: 4 additions & 0 deletions ui/base/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class OBElement extends HTMLElement {
constructor() {
super();

this.initialized = new Promise((resolve) => {
this.resolveInitialized = resolve;
});

const shadowRoot = this.attachShadow({ mode: 'open' });

const scss = `
Expand Down
8 changes: 8 additions & 0 deletions ui/base/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { html, render } from '../vendor.js';
import { OBElement } from '../base/element.js';

export class OBField extends OBElement {
async connectedCallback() {
if (this.connected) {
await this.connected();
}

this.resolveInitialized();
}

renderView() {
render(html`
${this.value}
Expand Down
81 changes: 44 additions & 37 deletions ui/fields/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@ class OBFieldPlaylist extends OBField {

#playlistItems;
#playlistContent;
#init;

async connectedCallback() {
async connected() {
this.#playlistItems = [];
this.#playlistContent = {};

this.renderComponent();

if (!this.#init) {
this.#init = true;

this.renderComponent().then(() => {
this.addEventListener("dragstart", this.onDragStart.bind(this));
this.addEventListener("dragend", this.onDragEnd.bind(this));
}
});
}

renderEdit() {
Expand Down Expand Up @@ -140,7 +135,6 @@ class OBFieldPlaylist extends OBField {
if (! window.dragHelperData || ! window.dragHelperData[0].classList.contains("sidebar_search_playlist_result")) {
return false;
}

var selectedPlaylist = this.#playlistItems;

Object.keys(window.dragHelperData).forEach((key) => {
Expand All @@ -159,20 +153,26 @@ class OBFieldPlaylist extends OBField {
}

async playlistContent() {
return Promise.all(this.#playlistItems.map(async (playlistItem) => {
if (this.#playlistContent[playlistItem]) {
return;
}
let playlistItemPromises = this.#playlistItems.map((playlistItem) => {
return new Promise((resolve) => {
if (this.#playlistContent[playlistItem]) {
resolve();
return;
}

const result = await OB.API.postPromise('playlists', 'get', { id: playlistItem });
if (!result.status) {
return;
}
OB.API.postPromise('playlists', 'get', { id: playlistItem }).then((result) => {
if (!result.status) {
resolve();
return;
}

const data = result.data;
this.#playlistContent[playlistItem] = data.name;
this.refresh();
}));
const data = result.data;
this.#playlistContent[playlistItem] = data.name;
resolve();
});
});
});
return Promise.all(playlistItemPromises);
}

playlistRemove(event) {
Expand All @@ -187,28 +187,35 @@ class OBFieldPlaylist extends OBField {
}

set value(value) {
if (!Array.isArray(value)) {
value = [value];
}
this.initialized.then(() => {
if (!Array.isArray(value)) {
value = [value];
}

value = value.map((x) => parseInt(x));
value = value.map((x) => parseInt(x));

if (!value.every(Number.isInteger)) {
return false;
}
if (!value.every(Number.isInteger)) {
return false;
}

if (this.dataset.hasOwnProperty('single')) {
value = value.slice(-1);
}
if (this.dataset.hasOwnProperty('single')) {
value = value.slice(-1);
}

this.#playlistItems = value;
this.playlistContent().then(() => {
this.#playlistItems = this.#playlistItems.filter((item) => {
return Object.keys(this.#playlistContent).includes(item.toString());
this.#playlistItems = value;
this.playlistContent().then(() => {

console.log(this, this.#playlistItems);
console.log(this, this.#playlistContent);

this.#playlistItems = this.#playlistItems.filter((item) => {
return Object.keys(this.#playlistContent).includes(item.toString());
});
this.renderComponent();

this.dispatchEvent(new Event('change'));
});
this.refresh();

this.dispatchEvent(new Event('change'));
});
}
}
Expand Down

0 comments on commit e9feb57

Please sign in to comment.