Skip to content

Commit d224a77

Browse files
committed
feat(middleware): register middleware settings
1 parent b9a8b29 commit d224a77

File tree

8 files changed

+58
-31
lines changed

8 files changed

+58
-31
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ You may also register actions which resolve the newly created state with a promi
288288
## Middleware
289289
A middleware is similar to an action, with the difference that it may return void as well. Middlewares can be executed before the dispatched action, thus potentially manipulating the current state which will be passed to the action, or afterwards, thus modifying the returned value from the action. Either way the middleware reducer can be sync as well as async.
290290

291-
You register a middleware by it as the first parameter to `store.registerMiddleware` and the placement `before` or `after` as second.
291+
You register a middleware by it as the first parameter to `store.registerMiddleware` and the placement `before` or `after` as second, followed by an optional third
292+
settings parameter. This will be passed into your middleware itself during execution.
293+
292294
```typescript
293295
const customLogMiddleware = (currentState) => console.log(currentState);
294296
// In TypeScript you can use the exported MiddlewarePlacement string enum
@@ -317,6 +319,13 @@ const decreaseBefore = (currentState: TestState, originalState: TestState) => {
317319
}
318320
```
319321

322+
Last but not least a potential third argument can be any settings as mentioned during middleware registration.
323+
324+
```typescript
325+
const customLogMiddleware = (currentState, originalState, settings) => console[settings.logType](currentState);
326+
store.registerMiddleware(customLogMiddleware, MiddlewarePlacement.After, { logType: "log" });
327+
```
328+
320329
### Propagating errors
321330
By default errors thrown by middlewares will be thrown in order to guarantee continues states. If you would like to stop state propagation you need to pass in the `propagateError` option:
322331

dist/commonjs/middleware.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export declare type Middleware<T> = (state: T, originalState?: T) => T | Promise<T | undefined> | void;
1+
export declare type Middleware<T> = (state: T, originalState?: T, settings?: any) => T | Promise<T | undefined> | void;
22
export declare enum MiddlewarePlacement {
33
Before = "before",
44
After = "after",

dist/commonjs/store.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export declare class Store<T> {
2626
private options;
2727
private dispatchQueue;
2828
constructor(initialState: T, options?: Partial<StoreOptions>);
29-
registerMiddleware(reducer: Middleware<T>, placement: MiddlewarePlacement): void;
29+
registerMiddleware(reducer: Middleware<T>, placement: MiddlewarePlacement, settings?: any): void;
3030
unregisterMiddleware(reducer: Middleware<T>): void;
3131
registerAction(name: string, reducer: Reducer<T>): void;
3232
unregisterAction(reducer: Reducer<T>): void;

dist/commonjs/store.js

Lines changed: 17 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)