2025-10-09 10:06:29 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
2025-10-09 15:42:31 +02:00
|
|
|
class trdom_torrentmanager extends EventTarget {
|
|
|
|
|
#logcb;
|
|
|
|
|
#log(loglevel, msg) {
|
|
|
|
|
if (this.#logcb !== null) {
|
|
|
|
|
this.#logcb('trdom_torrentmanager', loglevel, msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-09 10:06:29 +02:00
|
|
|
#element = document.createElement('div');
|
|
|
|
|
getElement() {
|
|
|
|
|
return this.#element;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-09 15:42:31 +02:00
|
|
|
#torrentDB = {};
|
|
|
|
|
get torrents() {
|
|
|
|
|
return this.#torrentDB;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(logcb = null) {
|
|
|
|
|
super();
|
|
|
|
|
this.#logcb = logcb;
|
2025-10-09 10:06:29 +02:00
|
|
|
this.#element.classList.add('trweb_torrentlistview', 'container-fluid');
|
|
|
|
|
}
|
2025-10-09 15:42:31 +02:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-09 10:06:29 +02:00
|
|
|
}
|