Files
trweb/js/trdom_torrentmanager.js
Fehér Roland cabc0fbaf1 Huge rewrite
Auto-refresh and sort are disabled for now
2025-10-13 15:12:50 +02:00

57 lines
1.8 KiB
JavaScript

"use strict";
class trdom_torrentmanager extends EventTarget {
#logcb;
#log(loglevel, msg) {
if (this.#logcb !== null) {
this.#logcb('trdom_torrentmanager', loglevel, msg);
}
}
#element = document.createElement('div');
getElement() {
return this.#element;
}
#torrentDB = {};
get torrents() {
return this.#torrentDB;
}
constructor(logcb = null) {
super();
this.#logcb = logcb;
this.#element.classList.add('trweb_torrentlistview', 'container-fluid');
}
handleEvent(e) {
this.#log(6, `Handling event of type ${e.type}`);
switch (e.type) {
case 'torrent-created':
let initHash = e.detail.torrentHash;
if (this.#torrentDB[initHash] == null) {
let torrentCreated = new trtorrent(initHash);
this.#torrentDB[initHash] = torrentCreated;
this.#element.appendChild(torrentCreated.element);
for (const [srv, control] of Object.entries(e.detail.torrentControls)) {
torrentCreated.addControl(srv, control)
}
}
else {
this.#log(1, 'This event should never ever fire for a torrent that already exists');
window.alert('We be ded, check console');
}
break;
case 'torrentserver-added':
let addedServer = e.detail.serverObject;
for (const [hash, torrent] of Object.entries(this.#torrentDB)) {
torrent.addControl(addedServer.name, new trdom_torrentcontrol(hash, addedServer, null, this.#logcb))
}
break;
default:
this.#log(5, `Event type ${e.type} not supported`);
break;
}
}
}