@@ -86,6 +86,54 @@ class Store extends BaseProtocol implements IStore {
86
86
this . options = options ?? { } ;
87
87
}
88
88
89
+ /**
90
+ * Processes messages based on the provided callback and options.
91
+ * @private
92
+ */
93
+ private async processMessages < T extends IDecodedMessage > (
94
+ messages : Promise < T | undefined > [ ] ,
95
+ callback : ( message : T ) => Promise < void | boolean > | boolean | void ,
96
+ options ?: QueryOptions
97
+ ) : Promise < boolean > {
98
+ let abort = false ;
99
+ const messagesOrUndef : Array < T | undefined > = await Promise . all ( messages ) ;
100
+ let processedMessages : Array < T > = messagesOrUndef . filter ( isDefined ) ;
101
+
102
+ if ( this . shouldReverseOrder ( options ) ) {
103
+ processedMessages = processedMessages . reverse ( ) ;
104
+ }
105
+
106
+ await Promise . all (
107
+ processedMessages . map ( async ( msg ) => {
108
+ if ( msg && ! abort ) {
109
+ abort = Boolean ( await callback ( msg ) ) ;
110
+ }
111
+ } )
112
+ ) ;
113
+
114
+ return abort ;
115
+ }
116
+
117
+ /**
118
+ * Determines whether to reverse the order of messages based on the provided options.
119
+ *
120
+ * Messages in pages are ordered from oldest (first) to most recent (last).
121
+ * https://github.com/vacp2p/rfc/issues/533
122
+ *
123
+ * @private
124
+ */
125
+ private shouldReverseOrder ( options ?: QueryOptions ) : boolean {
126
+ return (
127
+ typeof options ?. pageDirection === "undefined" ||
128
+ options ?. pageDirection === PageDirection . BACKWARD
129
+ ) ;
130
+ }
131
+
132
+ /**
133
+ * @deprecated Use `queryWithOrderedCallback` instead
134
+ **/
135
+ queryOrderedCallback = this . queryWithOrderedCallback ;
136
+
89
137
/**
90
138
* Do a query to a Waku Store to retrieve historical/missed messages.
91
139
*
@@ -103,42 +151,20 @@ class Store extends BaseProtocol implements IStore {
103
151
* or if an error is encountered when processing the reply,
104
152
* or if two decoders with the same content topic are passed.
105
153
*/
106
- async queryOrderedCallback < T extends IDecodedMessage > (
154
+ async queryWithOrderedCallback < T extends IDecodedMessage > (
107
155
decoders : IDecoder < T > [ ] ,
108
156
callback : ( message : T ) => Promise < void | boolean > | boolean | void ,
109
157
options ?: QueryOptions
110
158
) : Promise < void > {
111
- let abort = false ;
112
159
for await ( const promises of this . queryGenerator ( decoders , options ) ) {
113
- if ( abort ) break ;
114
- const messagesOrUndef : Array < T | undefined > = await Promise . all ( promises ) ;
115
-
116
- let messages : Array < T > = messagesOrUndef . filter ( isDefined ) ;
117
-
118
- // Messages in pages are ordered from oldest (first) to most recent (last).
119
- // https://github.com/vacp2p/rfc/issues/533
120
- if (
121
- typeof options ?. pageDirection === "undefined" ||
122
- options ?. pageDirection === PageDirection . BACKWARD
123
- ) {
124
- messages = messages . reverse ( ) ;
125
- }
126
-
127
- await Promise . all (
128
- messages . map ( async ( msg ) => {
129
- if ( msg && ! abort ) {
130
- abort = Boolean ( await callback ( msg ) ) ;
131
- }
132
- } )
133
- ) ;
160
+ if ( await this . processMessages ( promises , callback , options ) ) break ;
134
161
}
135
162
}
136
163
137
164
/**
138
165
* Do a query to a Waku Store to retrieve historical/missed messages.
139
- *
140
166
* The callback function takes a `Promise<WakuMessage>` in input,
141
- * useful if messages needs to be decrypted and performance matters.
167
+ * useful if messages need to be decrypted and performance matters.
142
168
*
143
169
* The order of the messages passed to the callback is as follows:
144
170
* - within a page, messages are expected to be ordered from oldest to most recent
@@ -152,25 +178,23 @@ class Store extends BaseProtocol implements IStore {
152
178
* or if an error is encountered when processing the reply,
153
179
* or if two decoders with the same content topic are passed.
154
180
*/
155
- async queryCallbackOnPromise < T extends IDecodedMessage > (
181
+ async queryWithPromiseCallback < T extends IDecodedMessage > (
156
182
decoders : IDecoder < T > [ ] ,
157
183
callback : (
158
184
message : Promise < T | undefined >
159
185
) => Promise < void | boolean > | boolean | void ,
160
186
options ?: QueryOptions
161
187
) : Promise < void > {
162
188
let abort = false ;
163
- let promises : Promise < void > [ ] = [ ] ;
164
189
for await ( const page of this . queryGenerator ( decoders , options ) ) {
165
- const _promises = page . map ( async ( msg ) => {
166
- if ( ! abort ) {
167
- abort = Boolean ( await callback ( msg ) ) ;
168
- }
190
+ const _promises = page . map ( async ( msgPromise ) => {
191
+ if ( abort ) return ;
192
+ abort = Boolean ( await callback ( msgPromise ) ) ;
169
193
} ) ;
170
194
171
- promises = promises . concat ( _promises ) ;
195
+ await Promise . all ( _promises ) ;
196
+ if ( abort ) break ;
172
197
}
173
- await Promise . all ( promises ) ;
174
198
}
175
199
176
200
/**
@@ -183,9 +207,6 @@ class Store extends BaseProtocol implements IStore {
183
207
* as follows:
184
208
* - within a page, messages SHOULD be ordered from oldest to most recent
185
209
* - pages direction depends on { @link QueryOptions.pageDirection }
186
- *
187
- * However, there is no way to guarantee the behavior of the remote node.
188
- *
189
210
* @throws If not able to reach a Waku Store peer to query,
190
211
* or if an error is encountered when processing the reply,
191
212
* or if two decoders with the same content topic are passed.
0 commit comments