summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib
diff options
context:
space:
mode:
authorFilipa Lacerda <filipa@gitlab.com>2017-03-24 16:59:14 +0000
committerFilipa Lacerda <filipa@gitlab.com>2017-03-24 16:59:14 +0000
commit9f7c19b352b4a5753b9ff15a629ba85b2b38357f (patch)
tree6f1065e3bef58945c78bc942d9151f66df606b2f /app/assets/javascripts/lib
parent7ac4127e4c69df86d309ac6e101c8e5acc263ab4 (diff)
downloadgitlab-ce-9f7c19b352b4a5753b9ff15a629ba85b2b38357f.tar.gz
Adds restart method and auxiliar callback to polling class
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r--app/assets/javascripts/lib/utils/poll.js45
1 files changed, 35 insertions, 10 deletions
diff --git a/app/assets/javascripts/lib/utils/poll.js b/app/assets/javascripts/lib/utils/poll.js
index c30a1fcb5da..1d86ec19454 100644
--- a/app/assets/javascripts/lib/utils/poll.js
+++ b/app/assets/javascripts/lib/utils/poll.js
@@ -5,23 +5,37 @@ import httpStatusCodes from './http_status';
* Service for vue resouce and method need to be provided as props
*
* @example
- * new poll({
+ * new Poll({
* resource: resource,
* method: 'name',
* data: {page: 1, scope: 'all'},
* successCallback: () => {},
* errorCallback: () => {},
+ * auxiliarCallback: () => {},
* }).makeRequest();
*
- * this.service = new BoardsService(endpoint);
- * new poll({
- * resource: this.service,
- * method: 'get',
- * data: {page: 1, scope: 'all'},
- * successCallback: () => {},
- * errorCallback: () => {},
- * }).makeRequest();
+ * Usage in pipelines table with visibility lib:
+ *
+ * const poll = new Poll({
+ * resource: this.service,
+ * method: 'getPipelines',
+ * data: { page: pageNumber, scope },
+ * successCallback: this.successCallback,
+ * errorCallback: this.errorCallback,
+ * auxiliarCallback: this.updateLoading,
+ * });
+ *
+ * if (!Visibility.hidden()) {
+ * poll.makeRequest();
+ * }
*
+ * Visibility.change(() => {
+ * if (!Visibility.hidden()) {
+ * poll.restart();
+ * } else {
+ * poll.stop();
+ * }
+* });
*
* 1. Checks for response and headers before start polling
* 2. Interval is provided by `Poll-Interval` header.
@@ -54,7 +68,10 @@ export default class Poll {
}
makeRequest() {
- const { resource, method, data, errorCallback } = this.options;
+ const { resource, method, data, errorCallback, auxiliarCallback } = this.options;
+
+ // It's called everytime a new request is made. Useful to update the status.
+ auxiliarCallback(true);
return resource[method](data)
.then(response => this.checkConditions(response))
@@ -70,4 +87,12 @@ export default class Poll {
this.canPoll = false;
clearTimeout(this.timeoutID);
}
+
+ /**
+ * Restarts polling after it has been stoped
+ */
+ restart() {
+ this.canPoll = true;
+ this.makeRequest();
+ }
}