Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
b5290ce0
Commit
b5290ce0
authored
Feb 24, 2017
by
Filipa Lacerda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify backoff algorithm
parent
29c284cb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
15 deletions
+12
-15
app/assets/javascripts/lib/utils/common_utils.js.es6
app/assets/javascripts/lib/utils/common_utils.js.es6
+12
-15
No files found.
app/assets/javascripts/lib/utils/common_utils.js.es6
View file @
b5290ce0
...
...
@@ -307,40 +307,37 @@
* @example
* ```
* backOff(function (next, stop) {
* // Let's perform this function repeatedly for 60s
*
doSomething()
*
.then(function (importantValue) {
*
// importantValue is not exactly what we
* //
need so we need to try again
* // Let's perform this function repeatedly for 60s
or for the timeout provided.
*
*
ourFunction()
*
.then(function (result) {
* //
continue if result is not what we need
* next();
*
* // importantValue is exactly what we are expecting so
* // let's stop with the repetions and jump out of the cycle
* stop(importantValue);
* // when result is what we need let's stop with the repetions and jump out of the cycle
* stop(result);
* })
* .catch(function (error) {
* //
there was an error, let's stop this with an error too
* //
if there is an error, we need to stop this with an error.
* stop(error);
* })
* }, 60000)
* .then(function (
importantValue
) {})
* .then(function (
result
) {})
* .catch(function (error) {
* // deal with errors passed to stop()
* })
* ```
*/
w.gl.utils.backOff = (fn, timeout = 60000
0
) => {
w.gl.utils.backOff = (fn, timeout = 60000) => {
let nextInterval = 2000;
const checkTimedOut = (timeoutMax, startTime) => currentTime =>
(currentTime - startTime > timeoutMax);
const hasTimedOut = checkTimedOut(timeout, (+new Date()));
const startTime = (+new Date());
return new Promise((resolve, reject) => {
const stop = arg => ((arg instanceof Error) ? reject(arg) : resolve(arg));
const next = () => {
if (
!hasTimedOut((+new Date()))
) {
if (
(+new Date()) - startTime < timeout
) {
setTimeout(fn.bind(null, next, stop), nextInterval);
nextInterval *= 2;
} else {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment