@@ -149,7 +149,7 @@ export interface ListenerEffectAPI<
149
149
* ```ts
150
150
* middleware.startListening({
151
151
* predicate: () => true,
152
- * async listener (_, { getOriginalState }) {
152
+ * async effect (_, { getOriginalState }) {
153
153
* getOriginalState(); // sync: OK!
154
154
*
155
155
* setTimeout(getOriginalState, 0); // async: throws Error
@@ -162,10 +162,67 @@ export interface ListenerEffectAPI<
162
162
* ```
163
163
*/
164
164
getOriginalState : ( ) => State
165
+ /**
166
+ * Removes the listener entry from the middleware and prevent future instances of the listener from running.
167
+ *
168
+ * It does **not** cancel any active instances.
169
+ */
165
170
unsubscribe ( ) : void
171
+ /**
172
+ * It will subscribe a listener if it was previously removed, noop otherwise.
173
+ */
166
174
subscribe ( ) : void
175
+ /**
176
+ * Returns a promise that resolves when the input predicate returns `true` or
177
+ * rejects if the listener has been cancelled or is completed.
178
+ *
179
+ * The return value is `true` if the predicate succeeds or `false` if a timeout is provided and expires first.
180
+ *
181
+ * ### Example
182
+ *
183
+ * ```ts
184
+ * const updateBy = createAction<number>('counter/updateBy');
185
+ *
186
+ * middleware.startListening({
187
+ * actionCreator: updateBy,
188
+ * async effect(_, { condition }) {
189
+ * // wait at most 3s for `updateBy` actions.
190
+ * if(await condition(updateBy.match, 3_000)) {
191
+ * // `updateBy` has been dispatched twice in less than 3s.
192
+ * }
193
+ * }
194
+ * })
195
+ * ```
196
+ */
167
197
condition : ConditionFunction < State >
198
+ /**
199
+ * Returns a promise that resolves when the input predicate returns `true` or
200
+ * rejects if the listener has been cancelled or is completed.
201
+ *
202
+ * The return value is the `[action, currentState, previousState]` combination that the predicate saw as arguments.
203
+ *
204
+ * The promise resolves to null if a timeout is provided and expires first,
205
+ *
206
+ * ### Example
207
+ *
208
+ * ```ts
209
+ * const updateBy = createAction<number>('counter/updateBy');
210
+ *
211
+ * middleware.startListening({
212
+ * actionCreator: updateBy,
213
+ * async effect(_, { take }) {
214
+ * const [{ payload }] = await take(updateBy.match);
215
+ * console.log(payload); // logs 5;
216
+ * }
217
+ * })
218
+ *
219
+ * store.dispatch(updateBy(5));
220
+ * ```
221
+ */
168
222
take : TakePattern < State >
223
+ /**
224
+ * Cancels all other running instances of this same listener except for the one that made this call.
225
+ */
169
226
cancelActiveListeners : ( ) => void
170
227
/**
171
228
* An abort signal whose `aborted` property is set to `true`
@@ -189,7 +246,6 @@ export interface ListenerEffectAPI<
189
246
* @param promise
190
247
*/
191
248
pause < M > ( promise : Promise < M > ) : Promise < M >
192
- // TODO Figure out how to pass this through the other types correctly
193
249
extra : ExtraArgument
194
250
}
195
251
0 commit comments