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,31 +132,58 @@ export const saveNote = ({ commit, dispatch }, noteData) => { ...@@ -131,31 +132,58 @@ 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()) const { notesById } = getters;
.then((res) => {
if (res.notes.length) { resp.notes.forEach((note) => {
const { notesById } = getters; if (notesById[note.id]) {
commit(types.UPDATE_NOTE, note);
res.notes.forEach((note) => { } else if (note.type === constants.DISCUSSION_NOTE) {
if (notesById[note.id]) { const discussion = utils.findNoteObjectById(state.notes, note.discussion_id);
commit(types.UPDATE_NOTE, note);
} else if (note.type === constants.DISCUSSION_NOTE) { if (discussion) {
const discussion = utils.findNoteObjectById(state.notes, note.discussion_id); commit(types.ADD_NEW_REPLY_TO_DISCUSSION, note);
if (discussion) {
commit(types.ADD_NEW_REPLY_TO_DISCUSSION, note);
} else {
commit(types.ADD_NEW_NOTE, note);
}
} else { } else {
commit(types.ADD_NEW_NOTE, note); commit(types.ADD_NEW_NOTE, note);
} }
}); } else {
commit(types.ADD_NEW_NOTE, note);
}
});
}
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();
} }
return res;
}); });
};
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;
......
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