cleaning up polling code
This commit is contained in:
@@ -6,6 +6,7 @@ import "toastify-js/src/toastify.css"
|
||||
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
|
||||
import contentTable from '../lib/contentTable';
|
||||
import Http from '../lib/http'
|
||||
import Polling from "../lib/polling";
|
||||
|
||||
const helper = {
|
||||
vue: {},
|
||||
@@ -16,12 +17,42 @@ const helper = {
|
||||
return helper.vue[name];
|
||||
},
|
||||
generateUrl: generateUrl,
|
||||
loop(callback, delay, ...args) {
|
||||
callback(...args);
|
||||
clearTimeout(helper.timeoutID);
|
||||
this.polling(callback, delay, ...args);
|
||||
isPolling() {
|
||||
return Polling.create().isEnabled();
|
||||
},
|
||||
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) {
|
||||
return string.split(char).filter(Boolean).join(char)
|
||||
},
|
||||
@@ -32,15 +63,6 @@ const helper = {
|
||||
ucfirst(string) {
|
||||
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) {
|
||||
let regex = '^((https?|ftp)://)([a-z0-9-]+\.)?(?:[-a-zA-Z0-9()@:%_\+.~#?&/=]+)$';
|
||||
const pattern = new RegExp(regex, 'i');
|
||||
@@ -115,19 +137,6 @@ const helper = {
|
||||
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) {
|
||||
const parser = new window.DOMParser();
|
||||
let doc = parser.parseFromString(htmlString, "text/html")
|
||||
@@ -253,6 +262,9 @@ const helper = {
|
||||
resetSearch: function (vm) {
|
||||
vm.$data.loading = 0;
|
||||
contentTable.getInstance([], []).clear();
|
||||
},
|
||||
redirect(url) {
|
||||
window.location.href = url;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user