Skip to content

Commit a5b2656

Browse files
committed
docs(alm): add missing ts docs of ListenerEffectAPI
Other changes: - update toolkit version in @examples-action-listener/counter - add lint:ts script in @examples-action-listener/counter
1 parent 431e399 commit a5b2656

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

examples/action-listener/counter/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"private": true,
55
"dependencies": {
6-
"@reduxjs/toolkit": "^1.8.0-rc.0",
6+
"@reduxjs/toolkit": "^1.8.0",
77
"@types/node": "^12.0.0",
88
"@types/react": "^17.0.0",
99
"@types/react-dom": "^17.0.0",
@@ -16,7 +16,8 @@
1616
},
1717
"scripts": {
1818
"start": "react-scripts start",
19-
"build": "react-scripts build"
19+
"build": "react-scripts build",
20+
"lint:ts": "tsc --noEmit"
2021
},
2122
"eslintConfig": {
2223
"extends": [

packages/toolkit/src/listenerMiddleware/types.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export interface ListenerEffectAPI<
149149
* ```ts
150150
* middleware.startListening({
151151
* predicate: () => true,
152-
* async listener(_, { getOriginalState }) {
152+
* async effect(_, { getOriginalState }) {
153153
* getOriginalState(); // sync: OK!
154154
*
155155
* setTimeout(getOriginalState, 0); // async: throws Error
@@ -162,10 +162,67 @@ export interface ListenerEffectAPI<
162162
* ```
163163
*/
164164
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+
*/
165170
unsubscribe(): void
171+
/**
172+
* It will subscribe a listener if it was previously removed, noop otherwise.
173+
*/
166174
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+
*/
167197
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+
*/
168222
take: TakePattern<State>
223+
/**
224+
* Cancels all other running instances of this same listener except for the one that made this call.
225+
*/
169226
cancelActiveListeners: () => void
170227
/**
171228
* An abort signal whose `aborted` property is set to `true`
@@ -189,7 +246,6 @@ export interface ListenerEffectAPI<
189246
* @param promise
190247
*/
191248
pause<M>(promise: Promise<M>): Promise<M>
192-
// TODO Figure out how to pass this through the other types correctly
193249
extra: ExtraArgument
194250
}
195251

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,7 +3672,7 @@ __metadata:
36723672
version: 0.0.0-use.local
36733673
resolution: "@examples-action-listener/counter@workspace:examples/action-listener/counter"
36743674
dependencies:
3675-
"@reduxjs/toolkit": ^1.8.0-rc.0
3675+
"@reduxjs/toolkit": ^1.8.0
36763676
"@types/node": ^12.0.0
36773677
"@types/react": ^17.0.0
36783678
"@types/react-dom": ^17.0.0
@@ -5462,7 +5462,7 @@ __metadata:
54625462
languageName: node
54635463
linkType: hard
54645464

5465-
"@reduxjs/toolkit@^1.6.0, @reduxjs/toolkit@^1.6.0-rc.1, @reduxjs/toolkit@^1.8.0-rc.0, @reduxjs/toolkit@workspace:packages/toolkit":
5465+
"@reduxjs/toolkit@^1.6.0, @reduxjs/toolkit@^1.6.0-rc.1, @reduxjs/toolkit@^1.8.0, @reduxjs/toolkit@workspace:packages/toolkit":
54665466
version: 0.0.0-use.local
54675467
resolution: "@reduxjs/toolkit@workspace:packages/toolkit"
54685468
dependencies:

0 commit comments

Comments
 (0)