Skip to content

Commit

Permalink
Update aria2worker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jc3213 authored Aug 21, 2024
1 parent 8a573c8 commit 7a2f13c
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions worker/aria2worker.js → aria2worker.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
let aria2c = {};
let jsonrpc = {};
let manager = {};
let version = '0.1';
let initFailed;
let syncPeriod;

self.addEventListener('message', (event) => {
let {action, params} = event.data;
switch (action) {
case 'aria2c-setup':
webWorker(params);
workerInit(params);
break;
case 'aria2c-purge':
sessionPurge();
case 'aria2c-version':
workerVersion();
break;
case 'aria2c-manager':
jsonrpcManager();
Expand All @@ -22,10 +23,13 @@ self.addEventListener('message', (event) => {
case 'aria2c-session':
sessionStatus(params);
break;
case 'aria2c-purge':
sessionPurge();
break;
}
});

function webWorker({jsonrpc, secret, interval = 10}) {
function workerInit({jsonrpc, secret, interval = 10}) {
aria2c.url = jsonrpc;
aria2c.interval = interval * 1000;
aria2c.params = secret ? ['token:' + secret] : [];
Expand All @@ -34,6 +38,9 @@ function webWorker({jsonrpc, secret, interval = 10}) {
initiator();
}

function workerVersion() {
self.postMessage({action: 'aria2c-jsonrpc-status', params: version});
}

function workerOpen(jsonrpc) {
return new Promise((resolve) => {
Expand Down Expand Up @@ -63,14 +70,14 @@ function workerMessage(...args) {
});
}

function jsonrpcStatus() {
self.postMessage({action: 'aria2c-jsonrpc-status', params: jsonrpc});
}

function jsonrpcManager() {
self.postMessage({action: 'aria2c-manager-status', params: manager});
}

function jsonrpcStatus() {
self.postMessage({action: 'aria2c-jsonrpc-status', params: jsonrpc});
}

async function sessionPurge() {
let response = await aria2c.call({method: 'aria2.sessionPurgeResult'});
manager.all = {...manager.active, ...manager.waiting};
Expand Down Expand Up @@ -113,7 +120,7 @@ function initiator(interval) {
let [global, version, stats, active, waiting, stopped] = response;
jsonrpc.options = global.result;
jsonrpc.version = version.result;
manager.stat = stats.result;
jsonrpc.stats = stats.result;
active.result.forEach((result) => manager.active[result.gid] = manager.all[result.gid] = result);
waiting.result.forEach((result) => manager.waiting[result.gid] = manager.all[result.gid] = result);
stopped.result.forEach((result) => manager.stopped[result.gid] = manager.all[result.gid] = result);
Expand All @@ -123,7 +130,7 @@ function initiator(interval) {

async function syncActiveStatus() {
let [stats, active] = await aria2c.call({method: 'aria2.getGlobalStat'}, {method: 'aria2.tellActive'});
manager.stat = stats.result;
jsonrpc.stats = stats.result;
active.result.forEach((result) => manager.active[result.gid] = manager.all[result.gid] = result);
}

Expand Down

0 comments on commit 7a2f13c

Please sign in to comment.