summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes/stores
diff options
context:
space:
mode:
authorFilipa Lacerda <filipa@gitlab.com>2017-08-02 11:45:52 +0100
committerFilipa Lacerda <filipa@gitlab.com>2017-08-02 11:45:52 +0100
commit60f6b596da4373c253c8387d5ebb555ea56d1759 (patch)
tree5e5abda46850d14d5a68aa70dae6c7554cea0e9f /app/assets/javascripts/notes/stores
parent6d50b7529bcb1720aad34f9de90c2140b2744204 (diff)
downloadgitlab-ce-60f6b596da4373c253c8387d5ebb555ea56d1759.tar.gz
[ci skip] Use eTag polling with Poll utility to allow stoping polling when visibily changes
Diffstat (limited to 'app/assets/javascripts/notes/stores')
-rw-r--r--app/assets/javascripts/notes/stores/actions.js70
1 files changed, 49 insertions, 21 deletions
diff --git a/app/assets/javascripts/notes/stores/actions.js b/app/assets/javascripts/notes/stores/actions.js
index d7059f462a4..f0d176e858c 100644
--- a/app/assets/javascripts/notes/stores/actions.js
+++ b/app/assets/javascripts/notes/stores/actions.js
@@ -1,5 +1,6 @@
/* global Flash */
-
+import Visibility from 'visibilityjs';
+import Poll from '../../lib/utils/poll';
import * as types from './mutation_types';
import * as utils from './utils';
import * as constants from '../constants';
@@ -131,31 +132,58 @@ export const saveNote = ({ commit, dispatch }, noteData) => {
});
};
-export const poll = ({ commit, state, getters }) => service
- .poll(state.notesData.notesPath, state.lastFetchedAt)
- .then(res => res.json())
- .then((res) => {
- if (res.notes.length) {
- const { notesById } = getters;
-
- res.notes.forEach((note) => {
- if (notesById[note.id]) {
- commit(types.UPDATE_NOTE, note);
- } else if (note.type === constants.DISCUSSION_NOTE) {
- const discussion = utils.findNoteObjectById(state.notes, note.discussion_id);
-
- if (discussion) {
- commit(types.ADD_NEW_REPLY_TO_DISCUSSION, note);
- } else {
- commit(types.ADD_NEW_NOTE, note);
- }
+const pollSuccessCallBack = (resp, commit, state, getters) => {
+ if (resp.notes.length) {
+ const { notesById } = getters;
+
+ resp.notes.forEach((note) => {
+ if (notesById[note.id]) {
+ commit(types.UPDATE_NOTE, note);
+ } else if (note.type === constants.DISCUSSION_NOTE) {
+ const discussion = utils.findNoteObjectById(state.notes, note.discussion_id);
+
+ if (discussion) {
+ commit(types.ADD_NEW_REPLY_TO_DISCUSSION, note);
} else {
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) => {
const { endpoint, awardName, noteId, skipMutalityCheck } = data;