react-chain-of-responsibility 0.2.1-main.d07f3ea → 0.3.0-main.cf60397

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,12 +1,10 @@
1
1
  # `react-chain-of-responsibility`
2
2
 
3
- [Chain of responsibility design pattern](https://refactoring.guru/design-patterns/chain-of-responsibility) for React component customization.
3
+ [Chain of responsibility design pattern](https://refactoring.guru/design-patterns/chain-of-responsibility) for compositing and customizing React component.
4
4
 
5
5
  ## Background
6
6
 
7
- This package is designed for React component developers to enable component customization via composition using the [chain of responsibility design pattern](https://refactoring.guru/design-patterns/chain-of-responsibility).
8
-
9
- Additional entrypoint and hook are provided to use with [Fluent UI as `IRenderFunction`](https://github.com/microsoft/fluentui/blob/master/packages/utilities/src/IRenderFunction.ts).
7
+ This package is designed for React component developers to enable component customization via composition using the [chain of responsibility design pattern](https://refactoring.guru/design-patterns/chain-of-responsibility). This pattern is also used in [Express](https://expressjs.com/) and [Redux](https://redux.js.org/).
10
8
 
11
9
  By composing customizations, they can be decoupled and published separately. App developers could import these published customizations and orchestrate them to their needs. This pattern encourages [separation of concerns](https://en.wikipedia.org/wiki/Separation_of_concerns) and enables economy of customizability.
12
10
 
@@ -14,50 +12,244 @@ By composing customizations, they can be decoupled and published separately. App
14
12
 
15
13
  Click here for [our live demo](https://compulim.github.io/react-chain-of-responsibility/).
16
14
 
17
- ## How to use
15
+ ## How to use?
18
16
 
19
- ### Using `<Proxy>` component
17
+ There are 3 steps to adopt the chain of responsibility pattern.
20
18
 
21
- ```jsx
19
+ 1. [Create a chain](#create-a-chain)
20
+ 1. [Register handlers in the chain](#register-handlers-in-the-chain)
21
+ 1. [Make a render request](#make-a-render-request)
22
+
23
+ In this sample, we will use chain of responsibility pattern to create a file preview UI to handle various file types.
24
+
25
+ ### Create a chain
26
+
27
+ A chain consists of multiple handlers (a.k.a. middleware) and each would handle rendering requests.
28
+
29
+ The request will be passed to the first handler and may traverse down the chain. The returning result will be a React component. If the chain decided not to render anything, it will return `undefined`.
30
+
31
+ ```tsx
22
32
  import { createChainOfResponsibility } from 'react-chain-of-responsibility';
23
33
 
24
- // Creates a <Provider> providing the chain of responsibility service.
25
- const { Provider, Proxy } = createChainOfResponsibility();
34
+ type Request = { contentType: string };
35
+ type Props = { url: string };
26
36
 
27
- // List of subcomponents.
28
- const Bold = ({ children }) => <strong>{children}</strong>;
29
- const Italic = ({ children }) => <i>{children}</i>;
30
- const Plain = ({ children }) => <>{children}</>;
37
+ const { asMiddleware, Provider, Proxy } = createChainOfResponsibility<Request, Props>();
38
+ ```
31
39
 
32
- // Constructs an array of middleware to handle the request and return corresponding subcomponents.
33
- const middleware = [
34
- () => next => request => (request === 'bold' ? Bold : next(request)),
35
- () => next => request => (request === 'italic' ? Italic : next(request)),
36
- () => () => () => Plain
37
- ];
40
+ In this sample, the `request` contains file type. And the `props` contains the URL of the file.
41
+
42
+ Tips: `request` is appearance, while `props` is for content.
43
+
44
+ ### Register handlers in the chain
45
+
46
+ Based on the rendering request, each middleware is called in turn and they will make decision:
47
+
48
+ - Will render
49
+ - Will render a component on its own
50
+ - Will render a component by compositing component from the next middleware in the chain
51
+ - Will not render
52
+ - Will not render anything at all
53
+ - Will not render, but let the next middleware in the chain to decide what to render
54
+
55
+ ```tsx
56
+ // Will handle request with content type `image/*`.
57
+ const Image = ({ middleware: { request, Next }, url }) =>
58
+ request.contentType.startsWith('image/') ? (
59
+ <img src={url} />
60
+ ) : (
61
+ <Next />
62
+ );
63
+
64
+ // Will handle request with content type `video/*`.
65
+ const Video = ({ middleware: { request, Next }, url }) =>
66
+ request.contentType.startsWith('video/') ? (
67
+ <video>
68
+ <source src={url} />
69
+ </video>
70
+ ) : (
71
+ <Next />
72
+ );
73
+
74
+ // Will handle everything.
75
+ const Binary = ({ url }) => <a href={url}>{url}</a>;
76
+
77
+ const middleware = [asMiddleware(Image), asMiddleware(Video), asMiddleware(Binary)];
78
+ ```
79
+
80
+ In this sample, 3 middleware will be registered in the chain. They will be called based on their order in the array:
81
+
82
+ 1. `<Image>` will render `<img>` if content type is `'image/*'`, otherwise, will pass to next middleware
83
+ 1. `<Video>` will render `<video>` if content type is `'video/*'`, otherwise, will pass to next middleware
84
+ 1. `<Binary>` is a catch-all and will render as a link
85
+
86
+ Notes: if props are passed to `<Next>`, they will override the original props. However, `request` cannot be overridden.
38
87
 
88
+ ### Make a render request
89
+
90
+ Before calling any components or hooks, the `<Provider>` component must be initialized with the chain.
91
+
92
+ When `<Proxy>` is being rendered, it will pass the `request` to the chain. The component returned from the chain will be rendered with `...props`. If no component is returned, it will render `undefined`.
93
+
94
+ ```tsx
39
95
  render(
40
96
  <Provider middleware={middleware}>
41
- <Proxy request="bold">This is bold.</Proxy>
42
- <Proxy request="italic">This is italic.</Proxy>
43
- <Proxy>This is plain.</Proxy>
97
+ <Proxy request={{ contentType: 'image/png' }} url="https://.../cat.png" />
98
+ <Proxy request={{ contentType: 'video/mp4' }} url="https://.../cat-jump.mp4" />
99
+ <Proxy request={{ contentType: 'application/octet-stream' }} url="https://.../cat.zip" />
44
100
  </Provider>
45
101
  );
46
102
  ```
47
103
 
48
- This sample will render:
104
+ The code above will render:
49
105
 
50
- > **This is bold.** _This is italic._ This is plain.
106
+ ```html
107
+ <img src="https://.../cat.png" />
108
+ <video>
109
+ <source src="https://.../cat-jump.mp4" />
110
+ </video>
111
+ <a href="https://.../cat.zip">https://.../cat.zip</a>
112
+ ```
51
113
 
52
- ```jsx
53
- <strong>This is bold.</strong>
54
- <i>This is italic.</i>
55
- <>This is plain.</>
114
+ For advanced scenario with precise rendering control, use the `useBuildComponentCallback` hook. This can be found in our live demo and [latter sections](#make-render-request-through-usebuildmiddlewarecallback).
115
+
116
+ ## How should I use?
117
+
118
+ Here are some recipes leveraging the chain of responsibility pattern for UI composition and customization.
119
+
120
+ ### Bring your own component
121
+
122
+ Customer of a component library can "bring your own" component by registering their component in the `<Provider>` component.
123
+
124
+ For example, in a date picker UI, using the chain of responsibility pattern enables app developer to bring their own "month picker" component.
125
+
126
+ ```diff
127
+ type MonthPickerProps = Readonly<{
128
+ onChange: (date: Date) => void;
129
+ value: Date
130
+ }>;
131
+
132
+ function MonthPicker(props: MonthPickerProps) {
133
+ return (
134
+ <div>
135
+ {props.value.toLocaleDateString(undefined, { month: 'long' })}
136
+ </div>
137
+ );
138
+ };
139
+
140
+ + const {
141
+ + asMiddleware: asMonthPickerMiddleware,
142
+ + Provider: MonthPickerProvider,
143
+ + Proxy: MonthPickerProxy
144
+ + } = createChainOfResponsibility<undefined, MonthPickerProps>();
145
+
146
+ type DatePickerProps = Readonly<{
147
+ + monthPickerComponent?: ComponentType<MonthPickerProps> | undefined;
148
+ onChange: (date: Date) => void;
149
+ value: Date;
150
+ }>;
151
+
152
+ + const monthPickerMiddleware = asMonthPickerMiddleware(MonthPicker);
153
+
154
+ function DatePicker(props: DatePickerProps) {
155
+ + const monthPickerMiddleware = useMemo(
156
+ + () =>
157
+ + props.monthPickerComponent
158
+ + ? [
159
+ + asMonthPickerMiddleware(props.monthPickerComponent),
160
+ + monthPickerMiddleware
161
+ + ]
162
+ + : [monthPickerMiddleware],
163
+ + [props.monthPickerComponent]
164
+ + );
165
+
166
+ return (
167
+ + <MonthPickerProvider middleware={monthPickerMiddleware}>
168
+ <div>
169
+ - <MonthPicker onChange={onChange} value={value} />
170
+ + <MonthPickerProxy onChange={onChange} value={value} />
171
+ <Calendar value={value} />
172
+ </div>
173
+ + </MonthPickerProvider>
174
+ );
175
+ }
176
+ ```
177
+
178
+ ### Customizing component
179
+
180
+ The "which component to render" decision in the middleware enables 4 key customization techniques:
181
+
182
+ - Add a new component
183
+ - Register a new `<Audio>` middleware component to handle content type of `audio/\*`
184
+ - Replace an existing component
185
+ - Register a new `<ImageV2>` middleware component to handle content type of `image/\*`
186
+ - The original `<Image>` will be replaced through starvation
187
+ - Remove an existing component
188
+ - Return `undefined` when handling content type of `video/\*`
189
+ - Decorate an existing component
190
+ - Return a component which render `<div class="my-border"><Next /></div>`
191
+
192
+ ### Improve load time through code splitting and lazy loading
193
+
194
+ After a bundle is lazy-loaded, register the component in the middleware.
195
+
196
+ When the chain of the `<Provider>` is updated, the lazy-loaded component will be rendered immediately.
197
+
198
+ This recipe can also help creating bundles with multiple flavors.
199
+
200
+ ## Advanced usage
201
+
202
+ ### Registering component via functional programming
203
+
204
+ The `asMiddleware()` is a helper function to turn a React component into a middleware for simpler registration. As it operates in render-time, there are disadvantages. For example, a VDOM node is always required.
205
+
206
+ If precise rendering control is required, consider registering the component natively using functional programming.
207
+
208
+ The following code snippet shows the conversion from the `<Image>` middleware component in our previous sample, into a component registered via functional programming.
209
+
210
+ ```diff
211
+ // Simplify the `<Image>` component by removing `middleware` props.
212
+ - const Image = ({ middleware: { request, Next }, url }) =>
213
+ - request.contentType.startsWith('image/') ? <img src={url} /> : <Next />;
214
+ + const Image = ({ url }) => <img src={url} />;
215
+
216
+ // Registering the `<Image>` component functionally.
217
+ const middleware = [
218
+ - asMiddleware(Image);
219
+ + () => next => request =>
220
+ + request.contentType.startsWith('image/') ? Image : next(request)
221
+ ];
222
+ ```
223
+
224
+ Notes: for performance reason, the return value of the `next(request)` should be a stable value. In the example above, the middleware return `Image`, which is a constant and is stable.
225
+
226
+ ### Make render request through `useBuildMiddlewareCallback()`
227
+
228
+ Similar the `asMiddleware`, the `<Proxy>` component is a helper component for easier rendering. It shares similar disadvantages.
229
+
230
+ The following code snippet shows the conversion from the `<Proxy>` component into the `useBuildMiddlewareCallback()` hook.
231
+
232
+ ```diff
233
+ function App() {
234
+ // Make a render request (a.k.a. build a component.)
235
+ + const buildMiddleware = useBuildMiddlewareCallback();
236
+ + const FilePreview = buildMiddleware({ contextType: 'image/png' });
237
+
238
+ return (
239
+ <Provider middleware={middleware}>
240
+ {/* Simplify the element by removing `request` props and handling `FilePreview` if it is `undefined`. */}
241
+ - <Proxy request={{ contentType: 'image/png' }} url="https://.../cat.png" />
242
+ + {FilePreview && <FilePreview url="https://.../cat.png" />}
243
+ </Provider>
244
+ );
245
+ }
56
246
  ```
57
247
 
58
- ### Using with Fluent UI as `IRenderFunction`
248
+ ### Using as `IRenderFunction` in Fluent UI v8
59
249
 
60
- The chain of responsibility design pattern can be used in Fluent UI.
250
+ > We are considering deprecating the `IRenderFunction` as Fluent UI no longer adopt this pattern.
251
+
252
+ The chain of responsibility design pattern can be used in Fluent UI v8.
61
253
 
62
254
  After calling `createChainOfResponsibilityForFluentUI`, it will return `useBuildRenderFunction` hook. This hook, when called, will return a function to use as [`IRenderFunction`](https://github.com/microsoft/fluentui/blob/master/packages/utilities/src/IRenderFunction.ts) in Fluent UI components.
63
255
 
@@ -106,70 +298,19 @@ There are subtle differences between the standard version and the Fluent UI vers
106
298
  - They are optional too, as defined in [`IRenderFunction`](https://github.com/microsoft/fluentui/blob/master/packages/utilities/src/IRenderFunction.ts)
107
299
  - Automatic fallback to `defaultRender`
108
300
 
109
- ### Decorating UI components
110
-
111
- One core feature of chain of responsibility design pattern is allowing middleware to control the execution flow. In other words, middleware can decorate the result of their `next()` middleware. The code snippet below shows how the wrapping could be done.
112
-
113
- The bold middleware uses traditional approach to wrap the `next()` result, which is a component named `<Next>`. The italic middleware uses [`react-wrap-with`](https://npmjs.com/package/react-wrap-with/) to simplify the wrapping code.
114
-
115
- ```jsx
116
- const middleware = [
117
- () => next => request => {
118
- const Next = next(request);
119
-
120
- if (request?.has('bold')) {
121
- return props => <Bold>{Next && <Next {...props} />}</Bold>;
122
- }
123
-
124
- return Next;
125
- },
126
- () => next => request => wrapWith(request?.has('italic') && Italic)(next(request)),
127
- () => () => () => Plain
128
- ];
129
-
130
- const App = () => (
131
- <Provider middleware={middleware}>
132
- <Proxy request={new Set(['bold'])}>This is bold.</Proxy>
133
- <Proxy request={new Set(['italic'])}>This is italic.</Proxy>
134
- <Proxy request={new Set(['bold', 'italic'])}>This is bold and italic.</Proxy>
135
- <Proxy>This is plain.</Proxy>
136
- </Provider>
137
- );
138
- ```
139
-
140
- This sample will render:
141
-
142
- > **This is bold.** _This is italic._ **_This is bold and italic._** This is plain.
143
-
144
- ```jsx
145
- <Bold>This is bold.</Bold>
146
- <Italic>This is italic.</Italic>
147
- <Bold><Italic>This is bold and italic.</Italic></Bold>
148
- <Plain>This is plain.</Plain>
149
- ```
150
-
151
301
  ### Nesting `<Provider>`
152
302
 
153
303
  If the `<Provider>` from the same chain appears nested in the tree, the `<Proxy>` will render using the middleware from the closest `<Provider>` and fallback up the chain. The following code snippet will render "Second First".
154
304
 
155
305
  ```jsx
156
- const { Provider, Proxy } = createChainOfResponsibility();
157
-
158
- const firstMiddleware = () => next => request => {
159
- const NextComponent = next(request);
160
-
161
- return () => <Fragment>First {NextComponent && <NextComponent />}</Fragment>;
162
- };
163
-
164
- const secondMiddleware = () => next => request => {
165
- const NextComponent = next(request);
306
+ const { asMiddleware, Provider, Proxy } = createChainOfResponsibility();
166
307
 
167
- return () => <Fragment>Second {NextComponent && <NextComponent />}</Fragment>;
168
- };
308
+ const First = ({ middleware: { Next } }) => <>First <Next /></>;
309
+ const Second = ({ middleware: { Next } }) => <>Second <Next /></>;
169
310
 
170
311
  render(
171
- <Provider middleware={[firstMiddleware]}>
172
- <Provider middleware={[secondMiddleware]}>
312
+ <Provider middleware={[asMiddleware(First)]}>
313
+ <Provider middleware={[asMiddleware(Second)]}>
173
314
  <Proxy /> <!-- Renders "Second First" -->
174
315
  </Provider>
175
316
  </Provider>
@@ -182,29 +323,41 @@ render(
182
323
  function createChainOfResponsibility<Request = undefined, Props = { children?: never }, Init = undefined>(
183
324
  options?: Options
184
325
  ): {
326
+ asMiddleware(
327
+ middlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>
328
+ ): ComponentMiddleware<Request, Props, Init>;
185
329
  Provider: ComponentType<ProviderProps<Request, Props, Init>>;
186
330
  Proxy: ComponentType<ProxyProps<Request, Props>>;
187
331
  types: {
188
332
  init: Init;
189
333
  middleware: ComponentMiddleware<Request, Props, Init>;
334
+ middlewareComponentProps: MiddlewareComponentProps<Request, Props, Init>;
190
335
  props: Props;
191
336
  request: Request;
192
337
  };
193
- useBuildComponentCallback: () => UseBuildComponent<Request, Props>;
338
+ useBuildComponentCallback(): (
339
+ request: Request,
340
+ options?: {
341
+ fallbackComponent?: ComponentType<Props> | undefined
342
+ }
343
+ ) => ComponentType<Props> | undefined;
194
344
  };
195
345
  ```
196
346
 
197
347
  ### Return value
198
348
 
199
- | Name | Type | Description |
200
- | --------------------------- | ------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
201
- | `Provider` | `React.ComponentType` | Entrypoint component, must wraps all usage of customizations |
202
- | `Proxy` | `React.ComponentType` | Proxy component, process the `request` from props and morph into the result component |
203
- | `types` | `{ init, middleware, props, request }` | TypeScript: shorthand types, all objects are `undefined` intentionally |
204
- | `useBuildComponentCallback` | `() => (request, options) => React.ComponentType \| false \| null \| undefined` | Callback hook which return a function to build the component for rendering the result |
349
+ | Name | Description |
350
+ | --------------------------- | -------------------------------------------------------------------------------------- |
351
+ | `asMiddleware` | A helper function to convert a React component into a middleware. |
352
+ | `Provider` | Entrypoint component, must wraps all usage of customizations |
353
+ | `Proxy` | Proxy component, process the `request` from props and morph into the result component |
354
+ | `types` | TypeScript: shorthand types, all objects are `undefined` intentionally |
355
+ | `useBuildComponentCallback` | Callback hook which return a function to build the component for rendering the request |
205
356
 
206
357
  ### Options
207
358
 
359
+ > `passModifiedRequest` is not supported by `asMiddleware`.
360
+
208
361
  ```ts
209
362
  type Options = {
210
363
  /** Allows a middleware to pass another request object to its next middleware. Default is false. */
@@ -212,24 +365,45 @@ type Options = {
212
365
  };
213
366
  ```
214
367
 
215
- If `passModifiedRequest` is default or `false`, middleware will not be allowed to pass another reference of `request` object to their `next()` middleware. Instead, the `request` object passed to `next()` will be ignored and the next middleware always receive the original `request` object. This behavior is similar to [ExpressJS](https://expressjs.com/) middleware.
368
+ If `passModifiedRequest` is default or `false`, middleware will not be allowed to pass another reference of `request` object to their `next()` middleware. Instead, the `request` object passed to `next()` will be ignored and the next middleware always receive the original `request` object. This behavior is similar to [Express](https://expressjs.com/) middleware.
369
+
370
+ Setting to `true` will enable advanced scenarios and allow one middleware to pass another instance of `request` object to influence their downstreamers.
216
371
 
217
- Setting to `true` will enable advanced scenarios and allow a middleware to influence their downstreamers.
372
+ When the option is default or `false` but the `request` object is mutable, one middleware could still modify the `request` object and influence their downstreamers. It is recommended to follow immutable pattern when handling the `request` object, or use deep [`Object.freeze()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) to guarantee immutability.
218
373
 
219
- When the option is default or `false`, middleware could still modify the `request` object and influence their downstreamers. It is recommended to follow immutable pattern when handling the `request` object, or use deep [`Object.freeze()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) to guarantee immutability.
374
+ ### API of `asMiddleware`
375
+
376
+ `asMiddleware` wraps a React component into a middleware. Build will be done through additional `middleware` prop.
377
+
378
+ ```ts
379
+ function asMiddleware(
380
+ middlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>
381
+ ): ComponentMiddleware<Request, Props, Init>;
382
+
383
+ type MiddlewareComponentProps<Request, Props, Init> = Props &
384
+ Readonly<{
385
+ middleware: Readonly<{
386
+ init: Init;
387
+ Next: ComponentType<Partial<Props>>;
388
+ request: Request;
389
+ }>
390
+ }>;
391
+ ```
220
392
 
221
393
  ### API of `useBuildComponentCallback`
222
394
 
223
395
  ```ts
224
- type UseBuildComponentCallbackOptions<Props> = { fallbackComponent?: ComponentType<Props> | false | null | undefined };
396
+ type UseBuildComponentCallbackOptions<Props> = { fallbackComponent?: ComponentType<Props> | undefined };
225
397
 
226
398
  type UseBuildComponentCallback<Request, Props> = (
227
399
  request: Request,
228
400
  options?: UseBuildComponentCallbackOptions<Props>
229
- ) => ComponentType<Props> | false | null | undefined;
401
+ ) => ComponentType<Props> | undefined;
230
402
  ```
231
403
 
232
- The `fallbackComponent` is a component which all unhandled requests will sink into, including calls without ancestral `<Provider>`.
404
+ For simplicity, instead of returning a React component or `false`/`null`/`undefined`, the `useBuildComponentCallback` will only return a React component or `undefined`.
405
+
406
+ The `fallbackComponent` is a component which all unhandled requests will sink into, including calls outside of `<Provider>`.
233
407
 
234
408
  ### API for Fluent UI
235
409
 
@@ -259,9 +433,24 @@ When rendering the element, `getKey` is called to compute the `key` attribute. T
259
433
 
260
434
  ## Designs
261
435
 
262
- ### Why the type of request and props can be different?
436
+ ### How can I choose between request and props?
437
+
438
+ - Request is for *appearance*, while props is for *content*
439
+ - Request is for *deciding which component to render*, while props is for *what to render*
263
440
 
264
- This approach may seem overkill at first.
441
+ For example:
442
+
443
+ - Button
444
+ - Request: button, link button, push button
445
+ - Props: icon, text
446
+ - File preview
447
+ - Request: image preview, video preview
448
+ - Props: file name, URL
449
+ - Input
450
+ - Request: text input, number input, password input
451
+ - Props: label, value, instruction
452
+
453
+ ### Why the type of request and props can be different?
265
454
 
266
455
  This is to support advanced scenarios where props are not ready until all rendering components are built.
267
456
 
@@ -302,26 +491,35 @@ This is for supporting multiple providers/proxies under a single app/tree.
302
491
 
303
492
  To reduce learning curve and likelihood of bugs, we disabled this feature until developers are more proficient with this package.
304
493
 
305
- If the `request` object passed to `next()` differs from the original `request` object, a reminder will be logged in the console.
494
+ With the default settings, if the `request` object passed to `next()` differs from the original `request` object, a reminder will be logged in the console.
495
+
496
+ ### Why `request` cannot be modified using `asMiddleware` even `passModifiedRequest` is enabled?
497
+
498
+ Request is for deciding which component to render. It is a build-time value.
499
+
500
+ For component registered using `asMiddleware()`, the `request` prop is a render-time value. A render-time value cannot be used to influence build phase.
501
+
502
+ To modify request, the middleware component must be converted to functional programming.
306
503
 
307
504
  ## Behaviors
308
505
 
309
506
  ### `<Proxy>` vs. `useBuildComponentCallback`
310
507
 
311
- Most of the time, use `<Proxy>`.
508
+ Most of the time, use `<Proxy>` unless precise rendering is needed.
312
509
 
313
510
  Behind the scene, `<Proxy>` call `useBuildComponentCallback()` to build the component it would morph into.
314
511
 
315
512
  You can use the following decision tree to know when to use `<Proxy>` vs. `useBuildComponentCallback`
316
513
 
317
- - If you want to know what component will render before actual render happen, use `useBuildComponentCallback()`
318
- - For example, using `useBuildComponentCallback()` allow you to know if the middleware will skip rendering the request
514
+ - If you need to know what kind of component will be rendered before actual render happen, use `useBuildComponentCallback()`
515
+ - For example, using `useBuildComponentCallback()` allow you to know if the middleware would skip rendering the request
319
516
  - If your component use `request` prop which is conflict with `<Proxy>`, use `useBuildComponentCallback()`
517
+ - Also consider using a wrapping component to rename `request` prop
320
518
  - Otherwise, use `<Proxy>`
321
519
 
322
520
  ### Calling `next()` multiple times
323
521
 
324
- It is possible to call `next()` multiple times to render multiple copies of UI. Middleware should be written as a stateless function.
522
+ It is possible to call `next()` multiple times. However, the return value should be stable, calling it multiple times without a different request should yield the same result.
325
523
 
326
524
  This is best used with options `passModifiedRequest` set to `true`. This combination allow a middleware to render the UI multiple times with some variations, such as rendering content and minimap at the same time.
327
525
 
@@ -335,11 +533,13 @@ This is because React does not allow asynchronous render. An exception will be t
335
533
 
336
534
  When writing middleware, keep them as stateless as possible and do not relies on data outside of the `request` object. The way it is writing should be similar to React function components.
337
535
 
338
- ### Good middleware returns false to skip rendering
536
+ When using functional programming to register the middleware, the return value should be stable.
537
+
538
+ ### Good middleware returns `false`/`null`/`undefined` to skip rendering
339
539
 
340
540
  If the middleware wants to skip rendering a request, return `false`/`null`/`undefined` directly. Do not return `() => false`, `<Fragment />`, or any other invisible components.
341
541
 
342
- This helps the component which send the request to the chain of responsibility to determine whether the request could be rendered or not.
542
+ This helps the hosting component to determine whether the request would be rendered or not.
343
543
 
344
544
  ### Typing a component which expect no props to be passed
345
545
 
@@ -349,27 +549,25 @@ In TypeScript, `{}` literally means any objects. Components of type `ComponentTy
349
549
 
350
550
  Although `Record<any, never>` means empty object, it is not extensible. Thus, [`Record<any, never> & { className: string }` means `Record<any, never>`](https://www.typescriptlang.org/play?#code/C4TwDgpgBACgTgezAZygXigJQgYwXAEwB4BDAOxABooyIA3COAPgG4AoUSKAZQFcAjeElQYhKKADIoAbyg4ANiWTIAciQC2EAFxRkwOAEsyAcygBfdmzxk9UMIhQ6+ghyJlzFytZp0BydSRGvubsQA).
351
551
 
352
- We believe the best way to type a component which does not allow any props, is `ComponentType<{ children?: undefined }>`.
353
-
354
552
  ## Inspirations
355
553
 
356
554
  This package is heavily inspired by [Redux](https://redux.js.org/) middleware, especially [`applyMiddleware()`](https://github.com/reduxjs/redux/blob/master/docs/api/applyMiddleware.md) and [`compose()`](https://github.com/reduxjs/redux/blob/master/docs/api/compose.md). We read [this article](https://medium.com/@jacobp100/you-arent-using-redux-middleware-enough-94ffe991e6) to understand the concept, followed by some more readings on functional programming topics.
357
555
 
358
556
  Over multiple years, the chain of responsibility design pattern is proven to be very flexible and extensible in [Bot Framework Web Chat](https://github.co/microsoft/BotFramework-WebChat/). Internal parts of Web Chat is written as middleware consumed by itself. Multiple bundles with various sizes can be offered by removing some middleware and treeshaking them off.
359
557
 
360
- Middleware and router in [ExpressJS](https://expressjs.com/) also inspired us to learn more about this pattern. Their fallback middleware always returns 404 is an innovative approach.
558
+ Middleware and router in [Express](https://expressjs.com/) also inspired us to learn more about this pattern. Their fallback middleware always returns 404 is an innovative approach.
361
559
 
362
- [Bing chat](https://bing.com/chat/) helped us understand and experiment with different naming.
560
+ [Microsoft Copilot](https://copilot.microsoft.com/) helped us understand and experiment with different naming.
363
561
 
364
- ### Differences from Redux and ExpressJS
562
+ ### Differences from Redux and Express
365
563
 
366
- The chain of responsibility design pattern implemented in [Redux](https://redux.js.org/) and [ExpressJS](https://expressjs.com/) prefers fire-and-forget execution (a.k.a. unidirectional): the result from the last middleware will not bubble up back to the first middleware. Instead, the caller may only collect the result from the last middleware. Sometimes, middleware may interrupt the execution and never return any results.
564
+ The chain of responsibility design pattern implemented in [Redux](https://redux.js.org/) and [Express](https://expressjs.com/) prefers fire-and-forget execution (a.k.a. unidirectional): the result from the last middleware will not bubble up back to the first middleware. Instead, the caller may only collect the result from the last middleware, or via asynchronous and intermediate `dispatch()` calls. Sometimes, middleware may interrupt the execution and never return any results.
367
565
 
368
- However, the chain of responsibility design pattern implemented in this package prefers call-and-return execution: the result from the last middleware will propagate back to the first middleware before returning to the caller. This gives every middleware a chance to manipulate the result from downstreamers before sending it back.
566
+ However, the chain of responsibility design pattern implemented in this package prefers synchronous call-and-return execution: the result from the last middleware will propagate back to the first middleware before returning to the caller. This gives every middleware a chance to manipulate the result from downstreamers before sending it back.
369
567
 
370
568
  ## Plain English
371
569
 
372
- This package implements the _chain of responsibility_ design pattern. Based on _request_, the chain of responsibility will be asked to _build a React component_. The middleware will _form a chain_ and request is _passed to the first one in the chain_. If the chain decided to render it, a component will be returned, otherwise, `false`/`null`/`undefined`.
570
+ This package implements the _chain of responsibility_ design pattern. Based on _request_, the chain of responsibility will be asked to _build a React component_. The middleware would _form a chain_ and request is _passed to the first one in the chain_. If the chain decided to render it, a component will be returned, otherwise, `false`/`null`/`undefined`.
373
571
 
374
572
  ## Contributions
375
573