This is an automated email from the ASF dual-hosted git repository.

amaranhao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb-fauxton.git


The following commit(s) were added to refs/heads/main by this push:
     new c11deadb [Bug fix] Changes feed duplicating results (#1410)
c11deadb is described below

commit c11deadb98b44ce055dfc68edd13001875deea03
Author: Margaret Harrigan <[email protected]>
AuthorDate: Fri Sep 1 20:47:01 2023 -0400

    [Bug fix] Changes feed duplicating results (#1410)
    
    * add since to query
    
    * reset state for new databases
    
    * limit list to 100 changes, order is not guarenteed
    
    ---------
    
    Co-authored-by: Margaret Harrigan <[email protected]>
---
 app/addons/documents/changes/actions.js            | 26 +++++++++++++---------
 .../documents/changes/components/ChangesScreen.js  |  2 +-
 app/addons/documents/changes/reducers.js           | 20 +++++++++--------
 3 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/app/addons/documents/changes/actions.js 
b/app/addons/documents/changes/actions.js
index f6f72d4d..56b500ff 100644
--- a/app/addons/documents/changes/actions.js
+++ b/app/addons/documents/changes/actions.js
@@ -17,8 +17,7 @@ import { get } from '../../../core/ajax';
 import ActionTypes from './actiontypes';
 import Helpers from '../helpers';
 
-const pollingTimeout = 60000;
-let currentRequest;
+let currentDatabase, latestSeqNum;
 
 const addFilter = (filter) => (dispatch) => {
   dispatch({
@@ -35,20 +34,22 @@ const removeFilter = (filter) => (dispatch) => {
 };
 
 const loadChanges = (databaseName) => (dispatch) => {
-  currentRequest = null;
   getLatestChanges(dispatch, databaseName);
 };
 
-const getLatestChanges = (dispatch, databaseName, lastSeqNum) => {
+const getLatestChanges = (dispatch, databaseName) => {
   const params = {
     limit: 100
   };
 
-  // after the first request for the changes list has been made, switch to 
longpoll
-  if (currentRequest) {
-    params.since = lastSeqNum;
-    params.timeout = pollingTimeout;
-    params.feed = 'longpoll';
+  // trigger a reset of the state if the current database is switched
+  if (!currentDatabase || currentDatabase !== databaseName) {
+    currentDatabase = databaseName;
+    latestSeqNum = null;
+  }
+  // otherwise only query for new changes since the most recent ones
+  if (latestSeqNum) {
+    params.since = latestSeqNum;
   }
 
   const query = app.utils.queryParams(params);
@@ -69,11 +70,14 @@ const getLatestChanges = (dispatch, databaseName, 
lastSeqNum) => {
 };
 
 const updateChanges = (json, dispatch) => {
-  const latestSeqNum = Helpers.getSeqNum(json.last_seq);
+  // if latestSeqNum is null, the state should be reset
+  const resetChanges = !latestSeqNum;
+  latestSeqNum = Helpers.getSeqNum(json.last_seq);
   dispatch({
     type: ActionTypes.UPDATE_CHANGES,
     changes: json.results,
-    seqNum: latestSeqNum
+    seqNum: latestSeqNum,
+    resetChanges: resetChanges
   });
 };
 
diff --git a/app/addons/documents/changes/components/ChangesScreen.js 
b/app/addons/documents/changes/components/ChangesScreen.js
index 8a78d379..3f4bfce3 100644
--- a/app/addons/documents/changes/components/ChangesScreen.js
+++ b/app/addons/documents/changes/components/ChangesScreen.js
@@ -26,7 +26,7 @@ export default class ChangesScreen extends React.Component {
     let msg = '';
     if (isShowingSubset) {
       let numChanges = changes.length;
-      msg = <p className="changes-result-limit">Limiting results to latest 
<b>{numChanges}</b> changes.</p>;
+      msg = <p className="changes-result-limit">Limiting results to 
<b>{numChanges}</b> changes.</p>;
     }
     return msg;
   }
diff --git a/app/addons/documents/changes/reducers.js 
b/app/addons/documents/changes/reducers.js
index 8bcf9f63..ca6bbfc1 100644
--- a/app/addons/documents/changes/reducers.js
+++ b/app/addons/documents/changes/reducers.js
@@ -24,13 +24,19 @@ const initialState = {
   lastSequenceNum: null
 };
 
-function updateChanges(state, seqNum, changes) {
-  const newState = {
-    ...state,
+function updateChanges(state, seqNum, changes, reset) {
+  // reset to the initial state when switching between databases so changes 
aren't carried over
+  const newState = (reset) ? {
+    ...initialState,
     // make a note of the most recent sequence number. This is used for a 
point of reference for polling for new changes
     lastSequenceNum: seqNum,
     isLoaded: true
-  };
+  } :
+    {
+      ...state,
+      lastSequenceNum: seqNum,
+      isLoaded: true
+    };
 
   // mark any additional changes that come after first page load as "new" so 
we can add a nice highlight effect
   // when the new row is rendered
@@ -108,11 +114,7 @@ export default function changes (state = initialState, 
action) {
   switch (action.type) {
 
     case ActionTypes.UPDATE_CHANGES:
-      // only bother updating the list of changes if the seq num has changed
-      if (state.lastSequenceNum !== action.seqNum) {
-        return updateChanges(state, action.seqNum, action.changes);
-      }
-      return state;
+      return updateChanges(state, action.seqNum, action.changes, 
action.resetChanges);
 
     case ActionTypes.ADD_CHANGES_FILTER_ITEM:
       return addFilter(state, action.filter);

Reply via email to