Skip to content

Commit

Permalink
doitinbackground feature
Browse files Browse the repository at this point in the history
  • Loading branch information
liquid36 committed Nov 13, 2017
1 parent 434c970 commit 578b7b7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@movilizame/networking",
"version": "0.1.0",
"version": "0.2.0",
"description": "Network utilities for Vue-UI",
"main": "main.js",
"repository": "https://github.com/movilizame/networking",
Expand Down
22 changes: 22 additions & 0 deletions src/Network.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,27 @@ export class Network {
);
return NetworkResponse.processResponse(response, source);
}

static doItInBackground (action, { retries = 10, interval = 5000, runImmediately = true }) {
return new Promise((resolve, reject) => {
let threadAction = function ({ next, times, data, register }) {
action().then((data) => {
resolve(data);
}).catch((err) => {
if (!err) {
if (times < retries) {
next();
} else {
reject();
}
} else {
reject(err);
}
});
};
let t = new Thread({ interval, runImmediately });
t.action(threadAction).run();
});
}
}

2 changes: 1 addition & 1 deletion src/Thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Thread {
return this;
}

runImmediately() {
runNow() {
this.runImmediately = true;
return this;
}
Expand Down

0 comments on commit 578b7b7

Please sign in to comment.