11/** 
2-  * 
32 * @param  {Collection } dataCollection 
43 * @param  {int } total 
54 * @param  {Document[] } documents 
65 * @param  {object } [aggregations] 
76 * @param  {object } [searchArgs] 
8-  * @param  {KuzzleSearchResult } [previous] 
7+  * @param  previous 
8+  * @property  {Collection } dataCollection 
9+  * @property  {number } fetchedDocument 
910 * @constructor  
1011 */ 
1112function  KuzzleSearchResult  ( dataCollection ,  total ,  documents ,  aggregations ,  searchArgs ,  previous )  { 
@@ -32,31 +33,18 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
3233    } , 
3334    // writable properties 
3435    fetchedDocument : { 
35-       value : documents . length , 
36+       value : previous   instanceof   KuzzleSearchResult  ?  documents . length   +   previous . fetchedDocument  :  documents . length , 
3637      enumerable : true , 
3738      writable : true 
38-     } , 
39-     _previous : { 
40-       value : previous  ||  null , 
41-       writable : true 
42-     } , 
43-     _next : { 
44-       value : null , 
45-       writable : true 
4639    } 
4740  } ) ; 
4841
49-   if  ( this . _previous  instanceof  KuzzleSearchResult )  { 
50-     this . _previous . _next  =  this ; 
51-     this . fetchedDocument  +=  this . _previous . fetchedDocument ; 
52-   } 
53- 
5442  // promisifying 
5543  if  ( this . dataCollection . kuzzle . bluebird )  { 
5644    return  this . dataCollection . kuzzle . bluebird . promisifyAll ( this ,  { 
5745      suffix : 'Promise' , 
5846      filter : function  ( name ,  func ,  target ,  passes )  { 
59-         var  whitelist  =  [ 'previous'  ,   ' next'] ; 
47+         var  whitelist  =  [ 'next' ] ; 
6048
6149        return  passes  &&  whitelist . indexOf ( name )  !==  - 1 ; 
6250      } 
@@ -66,107 +54,56 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
6654  return  this ; 
6755} 
6856
69- 
70- /** 
71-  * @param  cb 
72-  * @returns  {* } 
73-  */ 
74- KuzzleSearchResult . prototype . previous  =  function  ( cb )  { 
75-   cb ( null ,  this . _previous ) ; 
76- 
77-   return  this ; 
78- } ; 
79- 
8057/** 
8158 * @param  {function } cb 
8259 */ 
8360KuzzleSearchResult . prototype . next  =  function  ( cb )  { 
8461  var 
8562    filters , 
86-     options  =  Object . assign ( { } ,  this . searchArgs . options ) , 
87-     self  =  this ; 
88- 
89-   if  ( ! this . _next )  { 
90-     // retrieve next results with scroll if original search use it 
91-     if  ( options . scrollId )  { 
92-       if  ( this . fetchedDocument  >=  this . total )  { 
93-         cb ( null ,  null ) ; 
94-         return ; 
95-       } 
96- 
97-       // from and size parameters are not valid for a scroll action 
98-       if  ( typeof  options . from  !==  'undefined' )  { 
99-         delete  options . from ; 
100-       } 
101- 
102-       if  ( options . size )  { 
103-         delete  options . size ; 
104-       } 
63+     options  =  Object . assign ( { } ,  this . searchArgs . options ) ; 
64+   
65+   options . previous  =  this ; 
66+ 
67+   // retrieve next results with scroll if original search use it 
68+   if  ( options . scrollId )  { 
69+     if  ( this . fetchedDocument  >=  this . total )  { 
70+       cb ( null ,  null ) ; 
71+       return ; 
72+     } 
10573
106-       this . dataCollection . scroll ( 
107-         options . scrollId , 
108-         options , 
109-         this . searchArgs . filters  ||  { } , 
110-         function ( error ,  newSearchResults )  { 
111-           handleNextSearchResults ( error ,  self ,  newSearchResults ,  cb ) ; 
112-         } 
113-       ) ; 
74+     // from and size parameters are not valid for a scroll action 
75+     if  ( typeof  options . from  !==  'undefined' )  { 
76+       delete  options . from ; 
77+     } 
11478
115-       return ; 
79+     if  ( options . size )  { 
80+       delete  options . size ; 
11681    } 
117-     // retrieve next results with from/size if original search use it 
118-     else  if  ( options . from  !==  undefined  &&  options . size  !==  undefined )  { 
119-       filters  =  Object . assign ( { } ,  this . searchArgs . filters ) ; 
12082
121-       // check if we need to do next request to fetch all matching documents 
122-       options . from  +=  options . size ; 
83+     this . dataCollection . scroll ( options . scrollId ,  options ,  this . searchArgs . filters  ||  { } ,  cb ) ; 
12384
124-        if   ( options . from   >=   this . total )   { 
125-          cb ( null ,   null ) ; 
85+     return ; 
86+   } 
12687
127-         return ; 
128-       } 
88+   // retrieve next results with from/size if original search use it 
89+   if  ( options . from  !==  undefined  &&  options . size  !==  undefined )  { 
90+     filters  =  Object . assign ( { } ,  this . searchArgs . filters ) ; 
91+ 
92+     // check if we need to do next request to fetch all matching documents 
93+     options . from  +=  options . size ; 
12994
130-       this . dataCollection . search ( 
131-         filters , 
132-         options , 
133-         function ( error ,  newSearchResults )  { 
134-           handleNextSearchResults ( error ,  self ,  newSearchResults ,  cb ) ; 
135-         } 
136-       ) ; 
95+     if  ( options . from  >=  this . total )  { 
96+       cb ( null ,  null ) ; 
13797
13898      return ; 
13999    } 
140-   } 
141100
142-   if  ( this . _next  instanceof  KuzzleSearchResult )  { 
143-     cb ( null ,  this . _next ) ; 
101+     this . dataCollection . search ( filters ,  options ,  cb ) ; 
144102
145103    return ; 
146104  } 
147105
148106  cb ( new  Error ( 'Unable to retrieve next results from search: missing scrollId or from/size params' ) ) ; 
149107} ; 
150108
151- /** 
152-  * @param  {Error } error 
153-  * @param  {KuzzleSearchResult } currentSearchResults 
154-  * @param  {KuzzleSearchResult } newSearchResults 
155-  * @param  {Function } cb 
156-  */ 
157- function  handleNextSearchResults  ( error ,  currentSearchResults ,  newSearchResults ,  cb )  { 
158-   if  ( error )  { 
159-     cb ( error ) ; 
160-     return ; 
161-   } 
162- 
163-   newSearchResults . fetchedDocument  +=  currentSearchResults . fetchedDocument ; 
164- 
165-   newSearchResults . _previous  =  currentSearchResults ; 
166-   currentSearchResults . _next  =  newSearchResults ; 
167- 
168- 
169-   cb ( null ,  newSearchResults ) ; 
170- } 
171- 
172109module . exports  =  KuzzleSearchResult ; 
0 commit comments