Skip to content
This repository was archived by the owner on Oct 22, 2021. It is now read-only.

Commit 04e434e

Browse files
authored
Fix TypeError with toplist modal (#930)
* Fix TypeError with toplist modal * Remove check if modal is closed and use clearInterval to stop calling of function * Redo check so if modal is removed it stops calling the functions
1 parent a329b36 commit 04e434e

File tree

1 file changed

+72
-64
lines changed

1 file changed

+72
-64
lines changed

src/classes/toplist.class.js

Lines changed: 72 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Toplist {
5454
processList(){
5555
let sortKey;
5656
let ascending = false;
57+
let removed = false;
5758

5859
function setSortKey(fieldName){
5960
if (sortKey === fieldName){
@@ -89,16 +90,16 @@ class Toplist {
8990
return `${days < 10 ? "0" : ""}${days}:${hours < 10 ? "0" : ""}${hours}:${minutes < 10 ? "0" : ""}${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
9091
}
9192

92-
function updateProcessList(){
93+
function updateProcessList() {
9394
window.si.processes().then(data => {
9495
if (window.settings.excludeThreadsFromToplist === true) {
9596
data.list = data.list.sort((a, b) => {
96-
return (a.pid-b.pid);
97+
return (a.pid - b.pid);
9798
}).filter((e, index, a) => {
9899
let i = a.findIndex(x => x.name === e.name);
99100
if (i !== -1 && i !== index) {
100-
a[i].pcpu = a[i].pcpu+e.pcpu;
101-
a[i].pmem = a[i].pmem+e.pmem;
101+
a[i].pcpu = a[i].pcpu + e.pcpu;
102+
a[i].pmem = a[i].pmem + e.pmem;
102103
return false;
103104
}
104105
return true;
@@ -111,69 +112,72 @@ class Toplist {
111112

112113
let list = data.list.sort((a, b) => {
113114
switch (sortKey) {
114-
case "PID":
115-
if (ascending) return a.pid - b.pid;
116-
else return b.pid - a.pid;
117-
case "Name":
118-
if (ascending){
119-
if (a.name > b.name) return -1;
120-
if (a.name < b.name) return 1;
115+
case "PID":
116+
if (ascending) return a.pid - b.pid;
117+
else return b.pid - a.pid;
118+
case "Name":
119+
if (ascending) {
120+
if (a.name > b.name) return -1;
121+
if (a.name < b.name) return 1;
122+
return 0;
123+
}
124+
else {
125+
if (a.name < b.name) return -1;
126+
if (a.name > b.name) return 1;
127+
return 0;
128+
}
129+
case "User":
130+
if (ascending) {
131+
if (a.user > b.user) return -1;
132+
if (a.user < b.user) return 1;
133+
return 0;
134+
}
135+
else {
136+
if (a.user < b.user) return -1;
137+
if (a.user > b.user) return 1;
138+
return 0;
139+
}
140+
case "CPU":
141+
if (ascending) return a.pcpu - b.pcpu;
142+
else return b.pcpu - a.pcpu;
143+
case "Memory":
144+
if (ascending) return a.pmem - b.pmem;
145+
else return b.pmem - a.pmem;
146+
case "State":
147+
if (a.state < b.state) return -1;
148+
if (a.state > b.state) return 1;
121149
return 0;
122-
}
123-
else {
124-
if (a.name < b.name) return -1;
125-
if (a.name > b.name) return 1;
126-
return 0;
127-
}
128-
case "User":
129-
if (ascending){
130-
if (a.user > b.user) return -1;
131-
if (a.user < b.user) return 1;
132-
return 0;
133-
}
134-
else {
135-
if (a.user < b.user) return -1;
136-
if (a.user > b.user) return 1;
137-
return 0;
138-
}
139-
case "CPU":
140-
if (ascending) return a.pcpu - b.pcpu;
141-
else return b.pcpu - a.pcpu;
142-
case "Memory":
143-
if (ascending) return a.pmem - b.pmem;
144-
else return b.pmem - a.pmem;
145-
case "State":
146-
if (a.state < b.state) return -1;
147-
if (a.state > b.state) return 1;
148-
return 0;
149-
case "Started":
150-
if (ascending) return Date.parse(a.started) - Date.parse(b.started);
151-
else return Date.parse(b.started) - Date.parse(a.started);
152-
case "Runtime":
153-
if (ascending) return a.runtime - b.runtime;
154-
else return b.runtime - a.runtime;
155-
default:
156-
// default to the same sorting as the toplist
157-
return ((b.pcpu-a.pcpu)*100 + b.pmem-a.pmem);
150+
case "Started":
151+
if (ascending) return Date.parse(a.started) - Date.parse(b.started);
152+
else return Date.parse(b.started) - Date.parse(a.started);
153+
case "Runtime":
154+
if (ascending) return a.runtime - b.runtime;
155+
else return b.runtime - a.runtime;
156+
default:
157+
// default to the same sorting as the toplist
158+
return ((b.pcpu - a.pcpu) * 100 + b.pmem - a.pmem);
158159
}
159160
});
161+
162+
if (removed) clearInterval(updateInterval);
163+
else {
164+
document.querySelectorAll("#processList > tr").forEach(el => {
165+
el.remove();
166+
});
160167

161-
document.querySelectorAll("#processList > tr").forEach(el => {
162-
el.remove();
163-
});
164-
165-
list.forEach(proc => {
166-
let el = document.createElement("tr");
167-
el.innerHTML = `<td class="pid">${proc.pid}</td>
168-
<td class="name">${proc.name}</td>
169-
<td class="user">${proc.user}</td>
170-
<td class="cpu">${Math.round(proc.pcpu*10)/10}%</td>
171-
<td class="mem">${Math.round(proc.pmem*10)/10}%</td>
172-
<td class="state">${proc.state}</td>
173-
<td class="started">${proc.started}</td>
174-
<td class="runtime">${formatRuntime(proc.runtime)}</td>`;
175-
document.getElementById("processList").append(el);
176-
});
168+
list.forEach(proc => {
169+
let el = document.createElement("tr");
170+
el.innerHTML = `<td class="pid">${proc.pid}</td>
171+
<td class="name">${proc.name}</td>
172+
<td class="user">${proc.user}</td>
173+
<td class="cpu">${Math.round(proc.pcpu * 10) / 10}%</td>
174+
<td class="mem">${Math.round(proc.pmem * 10) / 10}%</td>
175+
<td class="state">${proc.state}</td>
176+
<td class="started">${proc.started}</td>
177+
<td class="runtime">${formatRuntime(proc.runtime)}</td>`;
178+
document.getElementById("processList").append(el);
179+
});
180+
}
177181
});
178182
}
179183

@@ -199,6 +203,10 @@ class Toplist {
199203
<tbody id=\"processList\">
200204
</tbody>
201205
</table>`,
206+
},
207+
() => {
208+
removed = true;
209+
//clearInterval(updateInterval);
202210
}
203211
);
204212

@@ -219,7 +227,7 @@ class Toplist {
219227
updateProcessList();
220228
window.keyboard.attach();
221229
window.term[window.currentTerm].term.focus();
222-
setInterval(updateProcessList, 1000);
230+
var updateInterval = setInterval(updateProcessList, 1000);
223231
}
224232
}
225233

0 commit comments

Comments
 (0)