diff --git a/client/js/api.js b/client/js/api.js index 21e25702..156823ed 100644 --- a/client/js/api.js +++ b/client/js/api.js @@ -1,10 +1,10 @@ 'use strict'; -const nprogress = require('nprogress'); const cookies = require('js-cookie'); const request = require('superagent'); const config = require('./config.js'); const events = require('./events.js'); +const progress = require('./util/progress.js'); class Api extends events.EventTarget { constructor() { @@ -259,18 +259,18 @@ class Api extends events.EventTarget { } if (!options.noProgress) { - nprogress.start(); + progress.start(); } abortFunction = () => { req.abort(); // does *NOT* call the callback passed in .end() - nprogress.done(); + progress.done(); reject( new Error('The request was aborted due to user cancel.')); }; req.end((error, response) => { - nprogress.done(); + progress.done(); abortFunction = () => {}; if (error) { if (response && response.body) { diff --git a/client/js/controllers/post_upload_controller.js b/client/js/controllers/post_upload_controller.js index 815ea14d..cba0918b 100644 --- a/client/js/controllers/post_upload_controller.js +++ b/client/js/controllers/post_upload_controller.js @@ -3,6 +3,7 @@ const api = require('../api.js'); const router = require('../router.js'); const misc = require('../util/misc.js'); +const progress = require('../util/progress.js'); const topNavigation = require('../models/top_navigation.js'); const Post = require('../models/post.js'); const PostUploadView = require('../views/post_upload_view.js'); @@ -83,6 +84,7 @@ class PostUploadController { } _uploadSinglePost(uploadable, skipDuplicates) { + progress.start(); let reverseSearchPromise = Promise.resolve(); if (!uploadable.lookalikesConfirmed) { reverseSearchPromise = @@ -121,8 +123,12 @@ class PostUploadController { }); this._lastCancellablePromise = savePromise; return savePromise; - }).catch(error => { + }).then(result => { + progress.done(); + return Promise.resolve(result); + }, error => { error.uploadable = uploadable; + progress.done(); return Promise.reject(error); }); } diff --git a/client/js/util/progress.js b/client/js/util/progress.js new file mode 100644 index 00000000..365d7ae9 --- /dev/null +++ b/client/js/util/progress.js @@ -0,0 +1,24 @@ +const nprogress = require('nprogress'); + +let nesting = 0; + +function start() { + if (!nesting) { + nprogress.start(); + } + nesting++; +} + +function done() { + nesting--; + if (nesting > 0) { + nprogress.inc(); + } else { + nprogress.done(); + } +} + +module.exports = { + start: start, + done: done, +};