cleaning up polling code
This commit is contained in:
@@ -94,7 +94,7 @@ export default {
|
|||||||
vm.$data.loading = 0;
|
vm.$data.loading = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
helper.enabledPolling = 0;
|
helper.disablePolling();
|
||||||
contentTable.getInstance().loading();
|
contentTable.getInstance().loading();
|
||||||
|
|
||||||
let url = formWrapper.getAttribute("action");
|
let url = formWrapper.getAttribute("action");
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import helper from '../utils/helper'
|
import helper from '../utils/helper'
|
||||||
import eventHandler from '../lib/eventHandler';
|
import eventHandler from '../lib/eventHandler';
|
||||||
import Http from '../lib/http'
|
|
||||||
|
|
||||||
const basePath = "/apps/ncdownloader/status/";
|
const basePath = "/apps/ncdownloader/status/";
|
||||||
const tableContainer = ".table";
|
const tableContainer = ".table";
|
||||||
export default {
|
export default {
|
||||||
@@ -18,18 +16,16 @@ export default {
|
|||||||
}
|
}
|
||||||
let name = type + "-downloads";
|
let name = type + "-downloads";
|
||||||
//avoid repeated click
|
//avoid repeated click
|
||||||
if (currentType === name && helper.enabledPolling) {
|
if (currentType === name && helper.isPolling()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
helper.enabledPolling = 1;
|
|
||||||
//$(tableContainer).removeClass().addClass("table " + name);
|
|
||||||
container.setAttribute("type", name);
|
container.setAttribute("type", name);
|
||||||
container.className = "table " + name;
|
container.className = "table " + name;
|
||||||
let delay = 15000;
|
let delay = 15000;
|
||||||
if (['active', 'youtube-dl'].includes(type)) {
|
if (['active', 'youtube-dl'].includes(type)) {
|
||||||
delay = 1500;
|
delay = 1500;
|
||||||
}
|
}
|
||||||
helper.loop(helper.refresh, delay, ...[path])
|
helper.polling(delay, path);
|
||||||
};
|
};
|
||||||
eventHandler.add("click", ".waiting-downloads a", event => clickHandler(event, 'waiting'));
|
eventHandler.add("click", ".waiting-downloads a", event => clickHandler(event, 'waiting'));
|
||||||
eventHandler.add("click", ".complete-downloads a", event => clickHandler(event, 'complete'));
|
eventHandler.add("click", ".complete-downloads a", event => clickHandler(event, 'complete'));
|
||||||
@@ -38,13 +34,12 @@ export default {
|
|||||||
eventHandler.add("click", ".youtube-dl-downloads a", event => clickHandler(event, 'youtube-dl'));
|
eventHandler.add("click", ".youtube-dl-downloads a", event => clickHandler(event, 'youtube-dl'));
|
||||||
eventHandler.add("click", "#ncdownloader-table-wrapper", ".download-file-folder", function (event) {
|
eventHandler.add("click", "#ncdownloader-table-wrapper", ".download-file-folder", function (event) {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
const path = "/apps/ncdownloader/update";
|
event.preventDefault();
|
||||||
let url = helper.generateUrl(path);
|
let ele = event.target;
|
||||||
Http.getInstance(url).setMethod('GET').send();
|
let url = ele.getAttribute("href");
|
||||||
|
helper.scanFolder().then(() => {
|
||||||
|
helper.redirect(url);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
helper.polling(function (url) {
|
|
||||||
url = helper.generateUrl(url);
|
|
||||||
Http.getInstance(url).setMethod('GET').send();
|
|
||||||
}, 60000, "/apps/ncdownloader/update");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ import "toastify-js/src/toastify.css"
|
|||||||
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
|
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
|
||||||
import contentTable from '../lib/contentTable';
|
import contentTable from '../lib/contentTable';
|
||||||
import Http from '../lib/http'
|
import Http from '../lib/http'
|
||||||
|
import Polling from "../lib/polling";
|
||||||
|
|
||||||
const helper = {
|
const helper = {
|
||||||
vue: {},
|
vue: {},
|
||||||
@@ -16,12 +17,42 @@ const helper = {
|
|||||||
return helper.vue[name];
|
return helper.vue[name];
|
||||||
},
|
},
|
||||||
generateUrl: generateUrl,
|
generateUrl: generateUrl,
|
||||||
loop(callback, delay, ...args) {
|
isPolling() {
|
||||||
callback(...args);
|
return Polling.create().isEnabled();
|
||||||
clearTimeout(helper.timeoutID);
|
},
|
||||||
this.polling(callback, delay, ...args);
|
enabePolling() {
|
||||||
|
Polling.create().enable();
|
||||||
|
},
|
||||||
|
disablePolling() {
|
||||||
|
Polling.create().disable().clear();
|
||||||
|
},
|
||||||
|
polling(delay = 1000, path) {
|
||||||
|
Polling.create().setDelay(delay).run(helper.refresh, path);
|
||||||
|
},
|
||||||
|
scanFolder(path = "/apps/ncdownloader/scanfolder") {
|
||||||
|
let url = helper.generateUrl(path);
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
Http.getInstance(url).setMethod('GET').setHandler(function (data) {
|
||||||
|
resolve(data.status);
|
||||||
|
}).send();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
pollingFolder(delay = 1000) {
|
||||||
|
Polling.create().setDelay(delay).run(helper.scanFolder);
|
||||||
|
},
|
||||||
|
refresh(path) {
|
||||||
|
path = path || "/apps/ncdownloader/status/active";
|
||||||
|
let url = helper.generateUrl(path);
|
||||||
|
Http.getInstance(url).setHandler(function (data) {
|
||||||
|
if (data && data.row) {
|
||||||
|
contentTable.getInstance(data.title, data.row).create();
|
||||||
|
} else {
|
||||||
|
contentTable.getInstance().noData();
|
||||||
|
}
|
||||||
|
if (data.counter)
|
||||||
|
helper.updateCounter(data.counter);
|
||||||
|
}).send();
|
||||||
},
|
},
|
||||||
enabledPolling: 0,
|
|
||||||
trim(string, char) {
|
trim(string, char) {
|
||||||
return string.split(char).filter(Boolean).join(char)
|
return string.split(char).filter(Boolean).join(char)
|
||||||
},
|
},
|
||||||
@@ -32,15 +63,6 @@ const helper = {
|
|||||||
ucfirst(string) {
|
ucfirst(string) {
|
||||||
return string.charAt(0).toUpperCase() + string.slice(1)
|
return string.charAt(0).toUpperCase() + string.slice(1)
|
||||||
},
|
},
|
||||||
polling(callback, delay, ...args) {
|
|
||||||
self = this;
|
|
||||||
helper.timeoutID = setTimeout(function () {
|
|
||||||
if (self.enabledPolling) {
|
|
||||||
callback(...args);
|
|
||||||
self.polling(callback, delay, ...args);
|
|
||||||
}
|
|
||||||
}, delay);
|
|
||||||
},
|
|
||||||
isURL(url) {
|
isURL(url) {
|
||||||
let regex = '^((https?|ftp)://)([a-z0-9-]+\.)?(?:[-a-zA-Z0-9()@:%_\+.~#?&/=]+)$';
|
let regex = '^((https?|ftp)://)([a-z0-9-]+\.)?(?:[-a-zA-Z0-9()@:%_\+.~#?&/=]+)$';
|
||||||
const pattern = new RegExp(regex, 'i');
|
const pattern = new RegExp(regex, 'i');
|
||||||
@@ -115,19 +137,6 @@ const helper = {
|
|||||||
counter.innerHTML = '<div class="number">' + data[key] + '</div>';
|
counter.innerHTML = '<div class="number">' + data[key] + '</div>';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
refresh(path) {
|
|
||||||
path = path || "/apps/ncdownloader/status/active";
|
|
||||||
let url = helper.generateUrl(path);
|
|
||||||
Http.getInstance(url).setHandler(function (data) {
|
|
||||||
if (data && data.row) {
|
|
||||||
contentTable.getInstance(data.title, data.row).create();
|
|
||||||
} else {
|
|
||||||
contentTable.getInstance().noData();
|
|
||||||
}
|
|
||||||
if (data.counter)
|
|
||||||
helper.updateCounter(data.counter);
|
|
||||||
}).send();
|
|
||||||
},
|
|
||||||
html2DOM: function (htmlString) {
|
html2DOM: function (htmlString) {
|
||||||
const parser = new window.DOMParser();
|
const parser = new window.DOMParser();
|
||||||
let doc = parser.parseFromString(htmlString, "text/html")
|
let doc = parser.parseFromString(htmlString, "text/html")
|
||||||
@@ -253,6 +262,9 @@ const helper = {
|
|||||||
resetSearch: function (vm) {
|
resetSearch: function (vm) {
|
||||||
vm.$data.loading = 0;
|
vm.$data.loading = 0;
|
||||||
contentTable.getInstance([], []).clear();
|
contentTable.getInstance([], []).clear();
|
||||||
|
},
|
||||||
|
redirect(url) {
|
||||||
|
window.location.href = url;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user