summaryrefslogtreecommitdiff
path: root/client/js/api.js
diff options
context:
space:
mode:
authorrr- <rr-@sakuya.pl>2016-09-29 21:36:59 +0200
committerrr- <rr-@sakuya.pl>2016-09-29 21:55:20 +0200
commit7862fecbc990a580970aaeef9de5667da268cc9f (patch)
tree2fa6cac7d3bbda913d10db8328f93cf37d9cbf13 /client/js/api.js
parent049a0dc351db6a16c20cc23e8d63303e49b38bad (diff)
client/posts: add upload cancelling
Diffstat (limited to 'client/js/api.js')
-rw-r--r--client/js/api.js49
1 files changed, 34 insertions, 15 deletions
diff --git a/client/js/api.js b/client/js/api.js
index 535b880..fe0f4ed 100644
--- a/client/js/api.js
+++ b/client/js/api.js
@@ -64,11 +64,13 @@ class Api extends events.EventTarget {
_process(url, requestFactory, data, files, options) {
options = options || {};
const [fullUrl, query] = this._getFullUrl(url);
- return new Promise((resolve, reject) => {
- if (!options.noProgress) {
- nprogress.start();
- }
+
+ let abortFunction = null;
+
+ let promise = new Promise((resolve, reject) => {
let req = requestFactory(fullUrl);
+
+ req.set('Accept', 'application/json');
if (query) {
req.query(query);
}
@@ -94,18 +96,35 @@ class Api extends events.EventTarget {
title: 'Authentication error',
description: 'Malformed credentials'});
}
- req.set('Accept', 'application/json')
- .end((error, response) => {
- nprogress.done();
- if (error) {
- reject(response && response.body ? response.body : {
- title: 'Networking error',
- description: error.message});
- } else {
- resolve(response.body);
- }
- });
+
+ if (!options.noProgress) {
+ nprogress.start();
+ }
+
+ abortFunction = () => {
+ req.abort(); // does *NOT* call the callback passed in .end()
+ nprogress.done();
+ reject({
+ title: 'Cancelled',
+ description:
+ 'The request was aborted due to user cancel.'});
+ };
+
+ req.end((error, response) => {
+ nprogress.done();
+ if (error) {
+ reject(response && response.body ? response.body : {
+ title: 'Networking error',
+ description: error.message});
+ } else {
+ resolve(response.body);
+ }
+ });
});
+
+ promise.abort = () => abortFunction();
+
+ return promise;
}
hasPrivilege(lookup) {