Skip to content

Commit 55af183

Browse files
committed
Add API docs for clearListeners() method
1 parent 18e4f78 commit 55af183

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

packages/action-listener-middleware/README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# RTK Incubator - Action Listener Middleware
22

3-
This package provides an experimental callback-based Redux middleware that we hope to include in Redux Toolkit directly in a future release. We're publishing it as a standalone package to allow users to try it out separately and give us feedback on its API design.
3+
This package provides a callback-based Redux middleware that we plan to include in Redux Toolkit directly in a future release. We're publishing it as a standalone package to allow users to try it out separately and give us feedback on its API design.
44

5-
This middleware lets you define callbacks that will run in response to specific actions being dispatched. It's intended to be a lightweight alternative to more widely used Redux async middleware like sagas and observables. While similar to thunks in level of complexity and concept, it can be used to replicate some common saga usage patterns.
5+
This middleware lets you define "listener" callbacks that will run in response to specific actions being dispatched. It's intended to be a lightweight alternative to more widely used Redux async middleware like sagas and observables. While similar to thunks in level of complexity and concept, it can be used to replicate some common saga usage patterns.
66

77
## Installation
88

@@ -106,7 +106,7 @@ Listeners can be defined statically by calling `listenerMiddleware.addListener()
106106

107107
### `createActionListenerMiddleware: (options?: CreateMiddlewareOptions) => Middleware`
108108

109-
Creates an instance of the middleware, which should then be added to the store via the `middleware` parameter.
109+
Creates an instance of the middleware, which should then be added to the store via `configureStore`'s `middleware` parameter.
110110

111111
Current options are:
112112

@@ -166,7 +166,6 @@ middleware.addListener({
166166
})
167167
```
168168

169-
It throws error if listener is not a function.
170169
The ["matcher" utility functions included in RTK](https://redux-toolkit.js.org/api/matching-utilities) are acceptable as predicates.
171170

172171
The return value is a standard `unsubscribe()` callback that will remove this listener. If you try to add a listener entry but another entry with this exact function reference already exists, no new entry will be added, and the existing `unsubscribe` method will be returned.
@@ -177,7 +176,8 @@ All listener predicates and callbacks are checked _after_ the root reducer has a
177176

178177
### `listenerMiddleware.removeListener(options: AddListenerOptions): boolean`
179178

180-
Removes a given listener. Accepts the same arguments as `middleware.addListener()` and throws error if listener is not a function.
179+
Removes a given listener. It accepts the same arguments as `middleware.addListener()`. It checks for an existing listener entry by comparing the function references of `listener` and the provided `actionCreator/matcher/predicate` function or `type` string.
180+
181181
Returns `true` if the `options.listener` listener has been removed, `false` if no subscription matching the input provided has been found.
182182

183183
```ts
@@ -186,19 +186,18 @@ middleware.removeListener({ type: 'todos/todoAdded', listener })
186186
// 2) RTK action creator
187187
middleware.removeListener({ actionCreator: todoAdded, listener })
188188
// 3) RTK matcher function
189-
middleware.removeListener({ matcher: isAnyOf(todoAdded, todoToggled), listener })
189+
middleware.removeListener({ matcher, listener })
190190
// 4) Listener predicate
191-
middleware.removeListener({
192-
predicate: (action, currentState, previousState) => {
193-
// return true when the listener should run
194-
},
195-
listener,
196-
})
191+
middleware.removeListener({ predicate, listener })
197192
```
198193

194+
### `listenerMiddleware.clearListeners(): void`
195+
196+
Removes all current listener entries. This is most likely useful for test scenarios where a single middleware or store instance might be used in multiple tests, as well as some app cleanup situations.
197+
199198
### `addListenerAction`
200199

201-
A standard RTK action creator that tells the middleware to dynamically add a new listener at runtime. It accepts exactly the same options as `middleware.addListener()`
200+
A standard RTK action creator. Dispatching this action tells the middleware to dynamically add a new listener at runtime. It accepts exactly the same options as `middleware.addListener()`
202201

203202
Dispatching this action returns an `unsubscribe()` callback from `dispatch`.
204203

@@ -209,9 +208,9 @@ const unsubscribe = store.dispatch(addListenerAction({ predicate, listener }))
209208

210209
### `removeListenerAction`
211210

212-
A standard RTK action creator that tells the middleware to remove a listener at runtime. Accepts the same arguments as `middleware.removeListener()`:
213-
Returns `true` if the `options.listener` listener has been removed, `false` if no subscription matching the input provided has been found.
211+
A standard RTK action creator. Dispatching this action tells the middleware to dynamically remove a listener at runtime. Accepts the same arguments as `middleware.removeListener()`.
214212

213+
Returns `true` if the `options.listener` listener has been removed, `false` if no subscription matching the input provided has been found.
215214

216215
```js
217216
store.dispatch(removeListenerAction({ predicate, listener }))

0 commit comments

Comments
 (0)