@@ -2044,6 +2044,18 @@ const MapProject = () => {
20442044 } ) . then ( response => callback ( response , payload ) )
20452045 }
20462046
2047+ const normalizeCandidateRequestValue = value => {
2048+ if ( isArray ( value ) )
2049+ return orderBy ( map ( value , normalizeCandidateRequestValue ) )
2050+ if ( value && typeof value === 'object' )
2051+ return Object . fromEntries ( orderBy ( keys ( value ) ) . map ( key => [ key , normalizeCandidateRequestValue ( value [ key ] ) ] ) )
2052+ return value
2053+ }
2054+
2055+ const areCandidateRequestsEqual = ( left , right ) => JSON . stringify ( normalizeCandidateRequestValue ( left || { } ) ) === JSON . stringify ( normalizeCandidateRequestValue ( right || { } ) )
2056+
2057+ const getRequestedCandidateFilter = ( index , _filters ) => getFacetQueryParam ( isEmpty ( _filters ) ? appliedFacets [ index ] : _filters )
2058+
20472059 const fetchAllCandidatesForRow = ( algoId , _row , offset = 0 , _retired , scrollToBottom , _filters , forceReload = false ) => {
20482060 if ( loadingMatches )
20492061 return
@@ -2060,8 +2072,14 @@ const MapProject = () => {
20602072 let __row = isEmpty ( _row ) ? row : _row
20612073
20622074 const existingCandidates = find ( allCandidatesRef . current [ algoId ] , c => c . row . __index === __row . __index )
2063-
2064- if ( ! forceReload && offset === 0 && ! _retired && existingCandidates ?. results ?. length > 0 ) {
2075+ const requestedFilter = getRequestedCandidateFilter ( __row . __index , _filters )
2076+ const canReuseExistingCandidates = ! forceReload &&
2077+ offset === 0 &&
2078+ ! _retired &&
2079+ existingCandidates ?. results ?. length > 0 &&
2080+ areCandidateRequestsEqual ( existingCandidates ?. filter , requestedFilter )
2081+
2082+ if ( canReuseExistingCandidates ) {
20652083 markAlgo ( __row . __index , algoId , 1 )
20662084 setTimeout ( ( ) => highlightTexts ( ( existingCandidates ?. results || [ ] ) , null , false ) , 100 )
20672085 const nextAlgo = getNextAlgoDef ( algoId )
@@ -2071,7 +2089,7 @@ const MapProject = () => {
20712089 } else {
20722090 if ( ! [ 0 , 1 ] . includes ( get ( rowStageRef . current , `${ __row . __index } .rerank` ) ) && some ( getAllCandidatesForRow ( __row . __index ) , r => ! isNumber ( r . search_meta . search_rerank_score ) ) ) {
20732091 markAlgo ( __row . __index , 'rerank' , - 1 )
2074- setTimeout ( ( ) => rerank ( __row . __index ) , 1000 )
2092+ rerank ( __row . __index )
20752093 } else {
20762094 markAlgo ( __row . __index , 'rerank' , 1 )
20772095 }
@@ -2091,19 +2109,20 @@ const MapProject = () => {
20912109 markAlgo ( __row . __index , algoId , 1 )
20922110 let data = isArray ( response ) ? response : ( response ?. data || [ ] )
20932111 setAllCandidates ( prev => {
2112+ let nextCandidates
20942113 if ( offset === 0 ) {
2095- const newCandidates = { ...prev }
20962114 const results = algoId === 'ocl-scispacy-loinc' ? [ { row : __row , results : fromScispacyResultsToConcepts ( get ( response . data , __row . __index ) || [ ] ) } ] : data
2097- newCandidates [ algoId ] = [ ...reject ( prev [ algoId ] , c => c . row . __index === __row . __index ) , ...( results || [ ] ) ]
2115+ nextCandidates = { ... prev , [ algoId ] : [ ...reject ( prev [ algoId ] , c => c . row . __index === __row . __index ) , ...( results || [ ] ) ] }
20982116 lookupCandidates ( algoId , get ( results , '0.results' ) )
2099- return newCandidates
21002117 } else {
21012118 const newMatches = [ ...( prev [ algoId ] || [ ] ) ]
21022119 const index = findIndex ( newMatches , match => match . row . __index === __row . __index )
21032120 newMatches [ index ] . results = [ ...newMatches [ index ] . results , ...( get ( data , '0.results' ) || [ ] ) ]
21042121 lookupCandidates ( algoId , get ( data , '0.results' ) )
2105- return { ...prev , [ algoId ] : newMatches }
2122+ nextCandidates = { ...prev , [ algoId ] : newMatches }
21062123 }
2124+ allCandidatesRef . current = nextCandidates
2125+ return nextCandidates
21072126 } )
21082127 setIsLoadingInDecisionView ( false )
21092128 let items = get ( response ?. data , '0.results' ) || [ ]
@@ -2117,7 +2136,7 @@ const MapProject = () => {
21172136 fetchAllCandidatesForRow ( nextAlgo . id , __row , offset , _retired , scrollToBottom , _filters , forceReload )
21182137 } else {
21192138 markAlgo ( __row . __index , 'rerank' , - 1 )
2120- setTimeout ( ( ) => rerank ( __row . __index ) , 1000 )
2139+ rerank ( __row . __index )
21212140 }
21222141 if ( scrollToBottom ) {
21232142 setTimeout ( ( ) => {
@@ -2202,6 +2221,7 @@ const MapProject = () => {
22022221 }
22032222 }
22042223 } )
2224+ allCandidatesRef . current = newCandidates
22052225 return newCandidates
22062226 } )
22072227 markAlgo ( index , 'rerank' , 1 )
0 commit comments