Commit 60f6b596 authored by Filipa Lacerda's avatar Filipa Lacerda

[ci skip] Use eTag polling with Poll utility to allow stoping polling when visibily changes

parent 6d50b752
...@@ -88,26 +88,12 @@ ...@@ -88,26 +88,12 @@
this.checkLocationHash(); this.checkLocationHash();
}); });
}) })
.catch((error) => { .catch((error) => Flash('Something went wrong while fetching issue comments. Please try again.'));
console.log(error)
Flash('Something went wrong while fetching issue comments. Please try again.')
});
}, },
initPolling() { initPolling() {
this.setLastFetchedAt(this.getNotesDataByProp('lastFetchedAt')); this.setLastFetchedAt(this.getNotesDataByProp('lastFetchedAt'));
// FIXME: @fatihacet Implement real polling mechanism this.poll();
// TODO: FILIPA: DEAL WITH THIS
setInterval(() => {
this.poll()
.then((res) => {
this.setLastFetchedAt(res.lastFetchedAt);
})
.catch((error) =>{
console.log(error)
Flash('Something went wrong while fetching latest comments.')
} );
}, 15000);
}, },
bindEventHubListeners() { bindEventHubListeners() {
this.$el.parentElement.addEventListener('toggleAward', (event) => { this.$el.parentElement.addEventListener('toggleAward', (event) => {
......
...@@ -19,7 +19,8 @@ export default { ...@@ -19,7 +19,8 @@ export default {
createNewNote(endpoint, data) { createNewNote(endpoint, data) {
return Vue.http.post(endpoint, data, { emulateJSON: true }); return Vue.http.post(endpoint, data, { emulateJSON: true });
}, },
poll(endpoint, lastFetchedAt) { poll(data = {}) {
const { endpoint, lastFetchedAt } = data;
const options = { const options = {
headers: { headers: {
'X-Last-Fetched-At': lastFetchedAt, 'X-Last-Fetched-At': lastFetchedAt,
......
/* global Flash */ /* global Flash */
import Visibility from 'visibilityjs';
import Poll from '../../lib/utils/poll';
import * as types from './mutation_types'; import * as types from './mutation_types';
import * as utils from './utils'; import * as utils from './utils';
import * as constants from '../constants'; import * as constants from '../constants';
...@@ -131,14 +132,11 @@ export const saveNote = ({ commit, dispatch }, noteData) => { ...@@ -131,14 +132,11 @@ export const saveNote = ({ commit, dispatch }, noteData) => {
}); });
}; };
export const poll = ({ commit, state, getters }) => service const pollSuccessCallBack = (resp, commit, state, getters) => {
.poll(state.notesData.notesPath, state.lastFetchedAt) if (resp.notes.length) {
.then(res => res.json())
.then((res) => {
if (res.notes.length) {
const { notesById } = getters; const { notesById } = getters;
res.notes.forEach((note) => { resp.notes.forEach((note) => {
if (notesById[note.id]) { if (notesById[note.id]) {
commit(types.UPDATE_NOTE, note); commit(types.UPDATE_NOTE, note);
} else if (note.type === constants.DISCUSSION_NOTE) { } else if (note.type === constants.DISCUSSION_NOTE) {
...@@ -154,9 +152,39 @@ export const poll = ({ commit, state, getters }) => service ...@@ -154,9 +152,39 @@ export const poll = ({ commit, state, getters }) => service
} }
}); });
} }
return res;
commit(types.SET_LAST_FETCHED_AT, resp.lastFetchedAt);
return resp;
};
export const poll = ({ commit, state, getters }) => {
const requestData = { endpoint: state.notesData.notesPath, lastFetchedAt: state.lastFetchedAt };
const eTagPoll = new Poll({
resource: service,
method: 'poll',
data: requestData,
successCallback: resp => resp.json()
.then(data => pollSuccessCallBack(data, commit, state, getters)),
errorCallback: () => Flash('Something went wrong while fetching latest comments.'),
}); });
if (!Visibility.hidden()) {
eTagPoll.makeRequest();
} else {
this.service.poll(requestData);
}
Visibility.change(() => {
if (!Visibility.hidden()) {
eTagPoll.restart();
} else {
eTagPoll.stop();
}
});
};
export const toggleAward = ({ commit, getters, dispatch }, data) => { export const toggleAward = ({ commit, getters, dispatch }, data) => {
const { endpoint, awardName, noteId, skipMutalityCheck } = data; const { endpoint, awardName, noteId, skipMutalityCheck } = data;
const note = getters.notesById[noteId]; const note = getters.notesById[noteId];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment