Huge rewrite

Auto-refresh and sort are disabled for now
This commit is contained in:
2025-10-09 15:42:31 +02:00
parent b2f155c0b4
commit cabc0fbaf1
7 changed files with 350 additions and 168 deletions

View File

@@ -1,33 +1,53 @@
"use strict";
class trserver extends EventTarget {
#logcb;
#log(loglevel, msg) {
if (this.#logcb !== null) {
this.#logcb(`server:${this.name}`, loglevel, msg);
}
}
#name;
get name() {
return this.#name;
}
#rpcurl;
#authHeader;
#sessionHeader;
#requests = [];
#torrents = {};
#torrentControls = {};
addControl(hash, control) {
this.#torrentControls[hash] = control;
}
getControl(hash) {
return this.#torrentControls[hash];
}
#isOnline = false;
#setOnline(online) {
if (this.isOnline == online) {
return;
}
this.#isOnline = online;
let detail = {
};
let etype;
if (online) {
etype = 'torrentserver-online';
}
else {
etype = 'torrentserver-offline';
}
this.dispatchEvent(new CustomEvent(etype, detail));
}
#logger = function(msg) {};
#torrentCallback = function(hash, torrentInfo) {
let e = new CustomEvent('torrent-update', {
'detail': {
'torrentHash': hash,
'torrentInfo': torrentInfo
}
});
this.dispatchEvent(e);
};
constructor (initdata, logger = null, tcb = null) {
constructor (name, initdata, logcb = null) {
super();
this.#logcb = logcb;
this.#name = name;
this.#rpcurl = initdata.rpcurl;
if (logger != null) {
this.#logger = logger;
}
if (tcb != null) {
this.#torrentCallback = tcb;
}
this.#authHeader = initdata.auth;
this.#sessionHeader = null;
}
@@ -63,7 +83,7 @@ class trserver extends EventTarget {
try {
rpc.send(JSON.stringify(request.payload));
} catch (error) {
this.#logger(error);
this.#log(2, error);
}
}
@@ -83,9 +103,9 @@ class trserver extends EventTarget {
switch (e.target.status) {
case 200:
if (!this.#isOnline) {
this.#logger('Server API connection estabilished');
this.#log(4, 'Server API connection estabilished');
this.#isOnline = true;
this.#forceUpdateTorrents();
//this.#forceUpdateTorrents();
}
//request.response = JSON.parse(e.target.responseText);
if (request.success != null) {
@@ -108,42 +128,40 @@ class trserver extends EventTarget {
break;
case 'timeout':
if (this.#isOnline) {
this.#logger('Server API connection timed out');
this.#log(3, 'Server API connection timed out');
this.#isOnline = false;
this.#forceUpdateTorrents();
}
break;
case 'error':
if (this.#isOnline) {
this.#logger('Server API refuses connection');
this.#log(3, 'Server API refuses connection');
this.#isOnline = false;
this.#forceUpdateTorrents();
}
this.#logger(JSON.stringify(e));
this.#log(5, JSON.stringify(e));
break;
}
}
#forceUpdateTorrents() {
for (const control of Object.values(this.#torrentControls)) {
control.updateDOM();
}
}
refreshSession (){
//let tag = this.#rpccall_prepare('session-get', null);
//this.#rpccall(tag);
}
#parseTorrents(response) {
// mark everything as deleted but clean
for (const [key, value] of Object.entries(this.#torrents)) {
value.wasDeleted = value.deleted;
value.deleted = true;
value.dirty = false;
}
let updatedHashes = [];
// Update torrent data
for (const torrent of response.torrents) {
let hash = torrent.hashString;
if (!(hash in this.#torrents)) {
this.#torrents[hash] = {'dirty': true};
}
let knownTorrent = this.#torrents[hash];
for (const [key, value] of Object.entries(torrent)) {
for (const fromserver of response.torrents) {
let hash = fromserver.hashString;
let initdata = {};
for (const [key, value] of Object.entries(fromserver)) {
let parsedValue = undefined;
switch (key) {
case 'hashString':
@@ -156,30 +174,51 @@ class trserver extends EventTarget {
break;
}
if (parsedValue != undefined && knownTorrent[key] !== parsedValue) {
knownTorrent[key] = parsedValue;
knownTorrent.dirty = true;
if (parsedValue != undefined) {
initdata[key] = parsedValue;
}
}
knownTorrent.deleted = false;
}
//check for changes
for (const [key, value] of Object.entries(this.#torrents)) {
if (value.deleted != value.wasDeleted || value.dirty) {
// call UI update
this.#torrentCallback(key, value);
}
}
}
#forceUpdateTorrents() {
for (const [key, value] of Object.entries(this.#torrents)) {
// call UI update
this.#torrentCallback(key, value);
let control;
if (!(hash in this.#torrentControls)) {
control = new trdom_torrentcontrol(hash, this, initdata, this.#logcb);
let createControls = new CustomEvent('torrent-initialize', {
detail: {
torrentHash: hash,
torrentControl: control,
serverObject: this
}
});
this.dispatchEvent(createControls);
}
else {
control = this.getControl(hash);
}
let e = new CustomEvent('torrent-updated', {
detail: {
torrentHash: hash,
torrentInfo: initdata
}
});
control.dispatchEvent(e);
updatedHashes.push(hash);
}
for (const [hash, control] of Object.entries(this.#torrentControls)) {
if (!updatedHashes.includes(hash) && (control.exists)) {
let e = new CustomEvent('torrent-deleted', {
detail: {
torrentHash: hash
}
});
control.dispatchEvent(e);
}
}
}
refreshTorrentList() {
let tag = this.#rpccall_prepare('torrent-get', {'fields': ['id', 'hashString', 'name', 'status', 'percentDone', 'comment', 'magnetLink']});
this.#requests[tag].success = this.#parseTorrents.bind(this);
this.#rpccall(tag);