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
0
Merge Requests
0
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
Boxiang Sun
gitlab-ce
Commits
9f7c19b3
Commit
9f7c19b3
authored
Mar 24, 2017
by
Filipa Lacerda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds restart method and auxiliar callback to polling class
parent
7ac4127e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
10 deletions
+75
-10
app/assets/javascripts/lib/utils/poll.js
app/assets/javascripts/lib/utils/poll.js
+35
-10
spec/javascripts/lib/utils/poll_spec.js
spec/javascripts/lib/utils/poll_spec.js
+40
-0
No files found.
app/assets/javascripts/lib/utils/poll.js
View file @
9f7c19b3
...
...
@@ -5,23 +5,37 @@ import httpStatusCodes from './http_status';
* Service for vue resouce and method need to be provided as props
*
* @example
* new
p
oll({
* new
P
oll({
* resource: resource,
* method: 'name',
* data: {page: 1, scope: 'all'},
* successCallback: () => {},
* errorCallback: () => {},
* auxiliarCallback: () => {},
* }).makeRequest();
*
* this.service = new BoardsService(endpoint);
* new poll({
* Usage in pipelines table with visibility lib:
*
* const poll = new Poll({
* resource: this.service,
* method: 'get',
* data: {page: 1, scope: 'all'},
* successCallback: () => {},
* errorCallback: () => {},
* }).makeRequest();
* 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
();
}
}
spec/javascripts/lib/utils/poll_spec.js
View file @
9f7c19b3
...
...
@@ -160,4 +160,44 @@ describe('Poll', () => {
Vue
.
http
.
interceptors
=
_
.
without
(
Vue
.
http
.
interceptors
,
pollInterceptor
);
});
});
describe
(
'
restart
'
,
()
=>
{
it
(
'
should restart polling when its called
'
,
(
done
)
=>
{
const
pollInterceptor
=
(
request
,
next
)
=>
{
next
(
request
.
respondWith
(
JSON
.
stringify
([]),
{
status
:
200
,
headers
:
{
'
poll-interval
'
:
2
}
}));
};
Vue
.
http
.
interceptors
.
push
(
pollInterceptor
);
const
service
=
new
ServiceMock
(
'
endpoint
'
);
spyOn
(
service
,
'
fetch
'
).
and
.
callThrough
();
const
Polling
=
new
Poll
({
resource
:
service
,
method
:
'
fetch
'
,
data
:
{
page
:
1
},
successCallback
:
()
=>
{
Polling
.
stop
();
setTimeout
(()
=>
{
Polling
.
restart
();
},
0
);
},
errorCallback
:
callbacks
.
error
,
});
spyOn
(
Polling
,
'
stop
'
).
and
.
callThrough
();
Polling
.
makeRequest
();
setTimeout
(()
=>
{
expect
(
service
.
fetch
.
calls
.
count
()).
toEqual
(
2
);
expect
(
service
.
fetch
).
toHaveBeenCalledWith
({
page
:
1
});
expect
(
Polling
.
stop
).
toHaveBeenCalled
();
done
();
},
10
);
Vue
.
http
.
interceptors
=
_
.
without
(
Vue
.
http
.
interceptors
,
pollInterceptor
);
});
});
});
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