react-chain-of-responsibility 0.3.0 → 0.3.1-main.932fc50
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 +13 -1
- package/dist/{chunk-RAUACXXD.mjs → chunk-CLWNNXXB.mjs} +19 -20
- package/dist/chunk-CLWNNXXB.mjs.map +1 -0
- package/dist/index-Cx9W9BxD.d.mts +70 -0
- package/dist/index-Cx9W9BxD.d.ts +70 -0
- package/dist/react-chain-of-responsibility.d.mts +2 -72
- package/dist/react-chain-of-responsibility.d.ts +2 -72
- package/dist/react-chain-of-responsibility.fluentUI.d.mts +5 -2
- package/dist/react-chain-of-responsibility.fluentUI.d.ts +5 -2
- package/dist/react-chain-of-responsibility.fluentUI.js +23 -20
- package/dist/react-chain-of-responsibility.fluentUI.js.map +1 -1
- package/dist/react-chain-of-responsibility.fluentUI.mjs +8 -4
- package/dist/react-chain-of-responsibility.fluentUI.mjs.map +1 -1
- package/dist/react-chain-of-responsibility.js +18 -19
- package/dist/react-chain-of-responsibility.js.map +1 -1
- package/dist/react-chain-of-responsibility.mjs +3 -3
- package/package.json +6 -5
- package/dist/chunk-RAUACXXD.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -356,12 +356,14 @@ function createChainOfResponsibility<Request = undefined, Props = { children?: n
|
|
|
356
356
|
|
|
357
357
|
### Options
|
|
358
358
|
|
|
359
|
+
#### `passModifiedRequest`
|
|
360
|
+
|
|
359
361
|
> `passModifiedRequest` is not supported by `asMiddleware`.
|
|
360
362
|
|
|
361
363
|
```ts
|
|
362
364
|
type Options = {
|
|
363
365
|
/** Allows a middleware to pass another request object to its next middleware. Default is false. */
|
|
364
|
-
passModifiedRequest?: boolean;
|
|
366
|
+
passModifiedRequest?: boolean | undefined;
|
|
365
367
|
};
|
|
366
368
|
```
|
|
367
369
|
|
|
@@ -431,6 +433,14 @@ type UseBuildRenderFunction<Props> = (options?: UseBuildRenderFunctionOptions<Pr
|
|
|
431
433
|
|
|
432
434
|
When rendering the element, `getKey` is called to compute the `key` attribute. This is required for some `onRenderXXX` props. These props are usually used to render more than one elements, such as [`DetailsList.onRenderField`](https://developer.microsoft.com/en-us/fluentui#/controls/web/detailslist#implementation), which renders every field (a.k.a. cell) in the [`<DetailsList>`](https://developer.microsoft.com/en-us/fluentui#/controls/web/detailslist).
|
|
433
435
|
|
|
436
|
+
### `withBuildProps` higher-order helper function
|
|
437
|
+
|
|
438
|
+
`withBuildProps` is a higher-order function that extends the chain-of-responsibility with props transformation capability. It modified the `<Proxy>` and `useBuildComponentCallback` hook so that request can be hoisted and accessible in props.
|
|
439
|
+
|
|
440
|
+
However, this should be used with care. Given an identical request, the returning component from the `useBuildComponentCallback()` hook will never be stable. Explicit memoization is required for stabilizing the result.
|
|
441
|
+
|
|
442
|
+
For `<Proxy>`, memoization is provided automatically through React `memo()` function. No explicity memoization is required.
|
|
443
|
+
|
|
434
444
|
## Designs
|
|
435
445
|
|
|
436
446
|
### How can I choose between request and props?
|
|
@@ -450,6 +460,8 @@ For example:
|
|
|
450
460
|
- Request: text input, number input, password input
|
|
451
461
|
- Props: label, value, instruction
|
|
452
462
|
|
|
463
|
+
In some cases, you may want to have request accessible via props. The `withBuildProps` higher-order helper function can be used to hoist the request object in props. However, currently, `withBuildProps` does not honor the `passModifiedRequest` option.
|
|
464
|
+
|
|
453
465
|
### Why the type of request and props can be different?
|
|
454
466
|
|
|
455
467
|
This is to support advanced scenarios where props are not ready until all rendering components are built.
|
|
@@ -10,15 +10,13 @@ import React, {
|
|
|
10
10
|
|
|
11
11
|
// src/isReactComponent.ts
|
|
12
12
|
function isConsumer(component) {
|
|
13
|
-
|
|
14
|
-
return ((_a = component == null ? void 0 : component.$$typeof) == null ? void 0 : _a.toString()) === "Symbol(react.context)";
|
|
13
|
+
return component?.$$typeof?.toString() === "Symbol(react.context)";
|
|
15
14
|
}
|
|
16
15
|
function isProvider(component) {
|
|
17
|
-
|
|
18
|
-
return ((_a = component == null ? void 0 : component.$$typeof) == null ? void 0 : _a.toString()) === "Symbol(react.provider)";
|
|
16
|
+
return component?.$$typeof?.toString() === "Symbol(react.provider)";
|
|
19
17
|
}
|
|
20
18
|
function isFragment(component) {
|
|
21
|
-
return
|
|
19
|
+
return component?.toString() === "Symbol(react.fragment)";
|
|
22
20
|
}
|
|
23
21
|
function isFunctionComponent(component) {
|
|
24
22
|
if (typeof component === "function") {
|
|
@@ -27,11 +25,10 @@ function isFunctionComponent(component) {
|
|
|
27
25
|
return isPureFunctionComponent(component);
|
|
28
26
|
}
|
|
29
27
|
function isPureFunctionComponent(component) {
|
|
30
|
-
|
|
31
|
-
return ((_a = component == null ? void 0 : component.$$typeof) == null ? void 0 : _a.toString()) === "Symbol(react.memo)" && isFunctionComponent(component.type);
|
|
28
|
+
return component?.$$typeof?.toString() === "Symbol(react.memo)" && isFunctionComponent(component.type);
|
|
32
29
|
}
|
|
33
30
|
function isComponentClass(component) {
|
|
34
|
-
return typeof component === "object" && typeof
|
|
31
|
+
return typeof component === "object" && typeof component?.["render"] === "function";
|
|
35
32
|
}
|
|
36
33
|
function isReactComponent(component) {
|
|
37
34
|
return isFunctionComponent(component) || isComponentClass(component) || isFragment(component) || isConsumer(component) || isProvider(component);
|
|
@@ -56,11 +53,11 @@ function createChainOfResponsibility(options = {}) {
|
|
|
56
53
|
get enhancer() {
|
|
57
54
|
return void 0;
|
|
58
55
|
},
|
|
59
|
-
useBuildComponentCallback(
|
|
60
|
-
if (options2
|
|
61
|
-
|
|
56
|
+
useBuildComponentCallback(_request, options2) {
|
|
57
|
+
if (!options2?.fallbackComponent) {
|
|
58
|
+
throw new Error("This component/hook cannot be used outside of its corresponding <Provider>");
|
|
62
59
|
}
|
|
63
|
-
|
|
60
|
+
return options2.fallbackComponent;
|
|
64
61
|
}
|
|
65
62
|
};
|
|
66
63
|
const context = createContext(defaultUseBuildComponentCallback);
|
|
@@ -117,10 +114,10 @@ function createChainOfResponsibility(options = {}) {
|
|
|
117
114
|
return /* @__PURE__ */ React.createElement(context.Provider, { value: contextValue }, children);
|
|
118
115
|
}
|
|
119
116
|
const useBuildComponentCallback = () => useContext(context).useBuildComponentCallback;
|
|
120
|
-
function Proxy({
|
|
117
|
+
function Proxy({ fallbackComponent, request, ...props }) {
|
|
121
118
|
const enhancer = useBuildComponentCallback();
|
|
122
119
|
const Component = enhancer(request, { fallbackComponent });
|
|
123
|
-
return Component ? /* @__PURE__ */ React.createElement(Component, { ...props }
|
|
120
|
+
return Component ? /* @__PURE__ */ React.createElement(Component, { ...props }) : null;
|
|
124
121
|
}
|
|
125
122
|
const asMiddleware = (MiddlewareComponent) => (init) => (next) => (request) => {
|
|
126
123
|
const RawNextComponent = next(request);
|
|
@@ -140,22 +137,24 @@ function createChainOfResponsibility(options = {}) {
|
|
|
140
137
|
MiddlewareOf.displayName = `MiddlewareOf<${MiddlewareComponent.displayName || ""}>`;
|
|
141
138
|
return memo(MiddlewareOf);
|
|
142
139
|
};
|
|
143
|
-
return {
|
|
140
|
+
return Object.freeze({
|
|
144
141
|
asMiddleware,
|
|
145
142
|
Provider: memo(ChainOfResponsibilityProvider),
|
|
146
143
|
Proxy: memo(Proxy),
|
|
147
|
-
types: {
|
|
144
|
+
types: Object.freeze({
|
|
148
145
|
middlewareComponentProps: void 0,
|
|
149
146
|
init: void 0,
|
|
150
147
|
middleware: void 0,
|
|
151
148
|
props: void 0,
|
|
149
|
+
proxyProps: void 0,
|
|
152
150
|
request: void 0
|
|
153
|
-
},
|
|
151
|
+
}),
|
|
154
152
|
useBuildComponentCallback
|
|
155
|
-
};
|
|
153
|
+
});
|
|
156
154
|
}
|
|
155
|
+
var createChainOfResponsibility_default = createChainOfResponsibility;
|
|
157
156
|
|
|
158
157
|
export {
|
|
159
|
-
|
|
158
|
+
createChainOfResponsibility_default
|
|
160
159
|
};
|
|
161
|
-
//# sourceMappingURL=chunk-
|
|
160
|
+
//# sourceMappingURL=chunk-CLWNNXXB.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/createChainOfResponsibility.tsx","../src/isReactComponent.ts","../src/private/compose.ts","../src/private/applyMiddleware.ts"],"sourcesContent":["import React, {\n createContext,\n isValidElement,\n memo,\n useCallback,\n useContext,\n useMemo,\n type ComponentType,\n type PropsWithChildren\n} from 'react';\n\nimport isReactComponent from './isReactComponent.ts';\nimport applyMiddleware, { type Enhancer } from './private/applyMiddleware.ts';\nimport { type ComponentMiddleware } from './types.ts';\n\n// TODO: Simplify to ComponentType<Props> | undefined.\ntype ResultComponent<Props> = ComponentType<Props> | false | null | undefined;\n\ntype UseBuildComponentCallbackOptions<Props> = {\n fallbackComponent?: ResultComponent<Props> | undefined;\n};\n\ninterface UseBuildComponentCallback<Request, Props> {\n (request: Request, options?: undefined | UseBuildComponentCallbackOptions<Props>): ComponentType<Props> | undefined;\n}\n\ntype ProviderContext<Request, Props> = {\n get enhancer(): Enhancer<[Request], ResultComponent<Props>> | undefined;\n useBuildComponentCallback: UseBuildComponentCallback<Request, Props>;\n};\n\ntype ProviderProps<Request, Props, Init> = PropsWithChildren<{\n middleware: readonly ComponentMiddleware<Request, Props, Init>[];\n}> &\n (Init extends never | void\n ? { readonly init?: undefined }\n : Init extends undefined | void\n ? { readonly init?: Init }\n : { readonly init: Init });\n\ntype ProxyProps<Request, Props extends object> = Props & {\n readonly fallbackComponent?: ComponentType<Props> | undefined;\n readonly request: Request;\n};\n\ntype CreateChainOfResponsibilityOptions = {\n /**\n * Allows a middleware to pass another request object when calling its next middleware. Default is false.\n *\n * However, middleware could modify the request object before calling its next middleware. It is recommended\n * to use Object.freeze() to prevent middleware from modifying the request object.\n */\n readonly passModifiedRequest?: boolean | undefined;\n};\n\ntype AsMiddlewareProps<Request, Props, Init> = {\n readonly init: Init;\n readonly Next: ComponentType<Partial<Props>>;\n readonly request: Request;\n};\n\ntype AsMiddlewareComponentProps<Request, Props, Init> = Props & {\n readonly middleware: AsMiddlewareProps<Request, Props, Init>;\n};\n\ntype ChainOfResponsibility<Request, Props extends object, Init> = {\n readonly asMiddleware: (\n middlewareComponent: ComponentType<AsMiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init>;\n readonly Provider: ComponentType<ProviderProps<Request, Props, Init>>;\n readonly Proxy: ComponentType<ProxyProps<Request, Props>>;\n readonly types: {\n readonly init: Init;\n readonly middleware: ComponentMiddleware<Request, Props, Init>;\n readonly middlewareComponentProps: AsMiddlewareComponentProps<Request, Props, Init>;\n readonly props: Props;\n readonly proxyProps: ProxyProps<Request, Props>;\n readonly request: Request;\n };\n readonly useBuildComponentCallback: () => UseBuildComponentCallback<Request, Props>;\n};\n\nfunction createChainOfResponsibility<Request = void, Props extends object = { readonly children?: never }, Init = void>(\n options: CreateChainOfResponsibilityOptions = {}\n): ChainOfResponsibility<Request, Props, Init> {\n const defaultUseBuildComponentCallback: ProviderContext<Request, Props> = {\n get enhancer() {\n return undefined;\n },\n useBuildComponentCallback(_request, options): ComponentType<Props> {\n if (!options?.fallbackComponent) {\n throw new Error('This component/hook cannot be used outside of its corresponding <Provider>');\n }\n\n return options.fallbackComponent;\n }\n };\n\n const context = createContext<ProviderContext<Request, Props>>(defaultUseBuildComponentCallback);\n\n function ChainOfResponsibilityProvider({ children, init, middleware }: ProviderProps<Request, Props, Init>) {\n // TODO: Related to https://github.com/microsoft/TypeScript/issues/17002.\n // typescript@5.2.2 has a bug, Array.isArray() is a type predicate but only works with mutable array, not readonly array.\n // After removing \"as unknown\", `middleware` on the next line become `any[]`.\n if (!Array.isArray(middleware as unknown) || middleware.some(middleware => typeof middleware !== 'function')) {\n throw new Error('middleware prop must be an array of functions');\n }\n\n const patchedMiddleware: readonly ComponentMiddleware<Request, Props, Init>[] = Object.freeze(\n middleware\n ? middleware.map(fn => (init: Init) => {\n const enhancer = fn(init);\n\n return (next: (request: Request) => ComponentType<Props> | false | null | undefined) =>\n (originalRequest: Request) => {\n // False positive: although we did not re-assign the variable from true, it was initialized as undefined.\n // eslint-disable-next-line prefer-const\n let hasReturned: boolean;\n\n const returnValue = enhancer(nextRequest => {\n if (hasReturned) {\n throw new Error('next() cannot be called after the function had returned synchronously');\n }\n\n !options.passModifiedRequest &&\n nextRequest !== originalRequest &&\n console.warn(\n 'react-chain-of-responsibility: \"options.passModifiedRequest\" must be set to true to pass a different request object to next().'\n );\n\n return next(options.passModifiedRequest ? nextRequest : originalRequest);\n })(originalRequest);\n\n hasReturned = true;\n\n if (isValidElement(returnValue)) {\n throw new Error('middleware must not return React element directly');\n } else if (\n returnValue !== false &&\n returnValue !== null &&\n typeof returnValue !== 'undefined' &&\n !isReactComponent(returnValue)\n ) {\n throw new Error(\n 'middleware must return false, null, undefined, function component, or class component'\n );\n }\n\n return returnValue;\n };\n })\n : []\n );\n\n const { enhancer: parentEnhancer } = useContext(context);\n\n const enhancer = useMemo(\n () =>\n // We are reversing because it is easier to read:\n // - With reverse, [a, b, c] will become a(b(c(fn)))\n // - Without reverse, [a, b, c] will become c(b(a(fn)))\n applyMiddleware<[Request], ResultComponent<Props>, [Init]>(\n ...[...patchedMiddleware, ...(parentEnhancer ? [() => parentEnhancer] : [])].reverse()\n )(init as Init),\n [init, middleware, parentEnhancer]\n );\n\n const useBuildComponentCallback = useCallback<UseBuildComponentCallback<Request, Props>>(\n (request, options = {}) => enhancer(() => options.fallbackComponent)(request) || undefined,\n [enhancer]\n );\n\n const contextValue = useMemo<ProviderContext<Request, Props>>(\n () => ({ enhancer, useBuildComponentCallback }),\n [enhancer, useBuildComponentCallback]\n );\n\n return <context.Provider value={contextValue}>{children}</context.Provider>;\n }\n\n const useBuildComponentCallback = () => useContext(context).useBuildComponentCallback;\n\n function Proxy({ fallbackComponent, request, ...props }: ProxyProps<Request, Props>) {\n const enhancer = useBuildComponentCallback();\n\n const Component = enhancer(request as Request, { fallbackComponent });\n\n return Component ? <Component {...(props as Props)} /> : null;\n }\n\n const asMiddleware: (\n middlewareComponent: ComponentType<AsMiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init> =\n (\n MiddlewareComponent: ComponentType<AsMiddlewareComponentProps<Request, Props, Init>>\n ): ComponentMiddleware<Request, Props, Init> =>\n init =>\n next =>\n request => {\n const RawNextComponent = next(request);\n\n // TODO: Can we pre-build this component during init?\n const MiddlewareOf = (props: Props) => {\n const middleware = useMemo(\n () =>\n Object.freeze({\n init,\n Next: memo<Partial<Props>>(\n RawNextComponent\n ? (overridingProps: Partial<Props>) => <RawNextComponent {...props} {...overridingProps} />\n : () => null\n ),\n request\n }),\n []\n );\n\n return <MiddlewareComponent {...props} middleware={middleware} />;\n };\n\n MiddlewareOf.displayName = `MiddlewareOf<${MiddlewareComponent.displayName || ''}>`;\n\n return memo<Props>(MiddlewareOf);\n };\n\n return Object.freeze({\n asMiddleware,\n Provider: memo<ProviderProps<Request, Props, Init>>(ChainOfResponsibilityProvider),\n Proxy: memo<ProxyProps<Request, Props>>(Proxy),\n types: Object.freeze({\n middlewareComponentProps: undefined as unknown as AsMiddlewareComponentProps<Request, Props, Init>,\n init: undefined as unknown as Init,\n middleware: undefined as unknown as ComponentMiddleware<Request, Props, Init>,\n props: undefined as unknown as Props,\n proxyProps: undefined as unknown as ProxyProps<Request, Props>,\n request: undefined as unknown as Request\n }),\n useBuildComponentCallback\n });\n}\n\nexport default createChainOfResponsibility;\nexport {\n type ChainOfResponsibility,\n type CreateChainOfResponsibilityOptions,\n type ProxyProps,\n type UseBuildComponentCallback\n};\n","import {\n type ComponentClass,\n type ComponentType,\n type Consumer,\n type Fragment,\n type FunctionComponent,\n type Provider\n} from 'react';\n\nfunction isConsumer(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Consumer<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.context)';\n}\n\nfunction isProvider(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Provider<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.provider)';\n}\n\nfunction isFragment(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is typeof Fragment {\n return component?.toString() === 'Symbol(react.fragment)';\n}\n\nfunction isFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n if (typeof component === 'function') {\n return true;\n }\n\n return isPureFunctionComponent(component);\n}\n\nfunction isPureFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n return component?.$$typeof?.toString() === 'Symbol(react.memo)' && isFunctionComponent(component.type);\n}\n\nfunction isComponentClass(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentClass {\n return typeof component === 'object' && typeof component?.['render'] === 'function';\n}\n\n// There are no definitive ways to check if an object is a React component or not.\n// We are checking if the object has a render function (classic component).\n// Note: \"forwardRef()\" returns plain object, not class instance.\nexport default function isReactComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentType {\n return (\n isFunctionComponent(component) ||\n isComponentClass(component) ||\n isFragment(component) ||\n isConsumer(component) ||\n isProvider(component)\n );\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function compose<P extends any[], R>(...fns: Enhancer<P, R>[]): Enhancer<P, R> {\n return (fn: Fn<P, R>): Fn<P, R> => fns.reduce((chain, fn) => fn(chain), fn);\n}\n","import compose from './compose.ts';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Middleware<P extends any[], R, S extends any[]> = (...init: S) => Enhancer<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function applyMiddleware<P extends any[], R, S extends any[]>(\n ...arrayOfMiddleware: Middleware<P, R, S>[]\n) {\n return (...init: S) => {\n const chain = arrayOfMiddleware.map(middleware => middleware(...init));\n\n return compose(...chain);\n };\n}\n"],"mappings":";AAAA,OAAO;AAAA,EACL;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAGK;;;ACAP,SAAS,WAEP,WACgC;AAChC,SAAO,WAAW,UAAU,SAAS,MAAM;AAC7C;AAEA,SAAS,WAEP,WACgC;AAChC,SAAO,WAAW,UAAU,SAAS,MAAM;AAC7C;AAEA,SAAS,WAEP,WAC8B;AAC9B,SAAO,WAAW,SAAS,MAAM;AACnC;AAEA,SAAS,oBAEP,WACgC;AAChC,MAAI,OAAO,cAAc,YAAY;AACnC,WAAO;AAAA,EACT;AAEA,SAAO,wBAAwB,SAAS;AAC1C;AAEA,SAAS,wBAEP,WACgC;AAChC,SAAO,WAAW,UAAU,SAAS,MAAM,wBAAwB,oBAAoB,UAAU,IAAI;AACvG;AAEA,SAAS,iBAEP,WAC6B;AAC7B,SAAO,OAAO,cAAc,YAAY,OAAO,YAAY,QAAQ,MAAM;AAC3E;AAKe,SAAR,iBAEL,WAC4B;AAC5B,SACE,oBAAoB,SAAS,KAC7B,iBAAiB,SAAS,KAC1B,WAAW,SAAS,KACpB,WAAW,SAAS,KACpB,WAAW,SAAS;AAExB;;;AC9De,SAAR,WAAgD,KAAuC;AAC5F,SAAO,CAAC,OAA2B,IAAI,OAAO,CAAC,OAAOA,QAAOA,IAAG,KAAK,GAAG,EAAE;AAC5E;;;ACGe,SAAR,mBACF,mBACH;AACA,SAAO,IAAI,SAAY;AACrB,UAAM,QAAQ,kBAAkB,IAAI,gBAAc,WAAW,GAAG,IAAI,CAAC;AAErE,WAAO,QAAQ,GAAG,KAAK;AAAA,EACzB;AACF;;;AH8DA,SAAS,4B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AAoB,eAAe,EAAE;AAEhF,WAAO,KAAY,YAAY;AAAA,EACjC;AAEF,SAAO,OAAO,OAAO;AAAA,IACnB;AAAA,IACA,UAAU,KAA0C,6BAA6B;AAAA,IACjF,OAAO,KAAiC,KAAK;AAAA,IAC7C,OAAO,OAAO,OAAO;AAAA,MACnB,0BAA0B;AAAA,MAC1B,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,OAAO;AAAA,MACP,YAAY;AAAA,MACZ,SAAS;AAAA,IACX,CAAC;AAAA,IACD;AAAA,EACF,CAAC;AACH;AAEA,IAAO,sCAAQ;","names":["fn","options","middleware","init","enhancer","useBuildComponentCallback"]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { ComponentType, PropsWithChildren } from 'react';
|
|
2
|
+
|
|
3
|
+
type Fn<P extends any[], R> = (...args: P) => R;
|
|
4
|
+
type Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;
|
|
5
|
+
type Middleware<P extends any[], R, S extends any[]> = (...init: S) => Enhancer<P, R>;
|
|
6
|
+
|
|
7
|
+
type ComponentMiddleware<Request, Props = {
|
|
8
|
+
children?: never;
|
|
9
|
+
}, Init = undefined> = Middleware<[
|
|
10
|
+
Request
|
|
11
|
+
], ComponentType<Props> | false | null | undefined, [
|
|
12
|
+
Init
|
|
13
|
+
]>;
|
|
14
|
+
|
|
15
|
+
type ResultComponent<Props> = ComponentType<Props> | false | null | undefined;
|
|
16
|
+
type UseBuildComponentCallbackOptions<Props> = {
|
|
17
|
+
fallbackComponent?: ResultComponent<Props> | undefined;
|
|
18
|
+
};
|
|
19
|
+
interface UseBuildComponentCallback<Request, Props> {
|
|
20
|
+
(request: Request, options?: undefined | UseBuildComponentCallbackOptions<Props>): ComponentType<Props> | undefined;
|
|
21
|
+
}
|
|
22
|
+
type ProviderProps<Request, Props, Init> = PropsWithChildren<{
|
|
23
|
+
middleware: readonly ComponentMiddleware<Request, Props, Init>[];
|
|
24
|
+
}> & (Init extends never | void ? {
|
|
25
|
+
readonly init?: undefined;
|
|
26
|
+
} : Init extends undefined | void ? {
|
|
27
|
+
readonly init?: Init;
|
|
28
|
+
} : {
|
|
29
|
+
readonly init: Init;
|
|
30
|
+
});
|
|
31
|
+
type ProxyProps<Request, Props extends object> = Props & {
|
|
32
|
+
readonly fallbackComponent?: ComponentType<Props> | undefined;
|
|
33
|
+
readonly request: Request;
|
|
34
|
+
};
|
|
35
|
+
type CreateChainOfResponsibilityOptions = {
|
|
36
|
+
/**
|
|
37
|
+
* Allows a middleware to pass another request object when calling its next middleware. Default is false.
|
|
38
|
+
*
|
|
39
|
+
* However, middleware could modify the request object before calling its next middleware. It is recommended
|
|
40
|
+
* to use Object.freeze() to prevent middleware from modifying the request object.
|
|
41
|
+
*/
|
|
42
|
+
readonly passModifiedRequest?: boolean | undefined;
|
|
43
|
+
};
|
|
44
|
+
type AsMiddlewareProps<Request, Props, Init> = {
|
|
45
|
+
readonly init: Init;
|
|
46
|
+
readonly Next: ComponentType<Partial<Props>>;
|
|
47
|
+
readonly request: Request;
|
|
48
|
+
};
|
|
49
|
+
type AsMiddlewareComponentProps<Request, Props, Init> = Props & {
|
|
50
|
+
readonly middleware: AsMiddlewareProps<Request, Props, Init>;
|
|
51
|
+
};
|
|
52
|
+
type ChainOfResponsibility<Request, Props extends object, Init> = {
|
|
53
|
+
readonly asMiddleware: (middlewareComponent: ComponentType<AsMiddlewareComponentProps<Request, Props, Init>>) => ComponentMiddleware<Request, Props, Init>;
|
|
54
|
+
readonly Provider: ComponentType<ProviderProps<Request, Props, Init>>;
|
|
55
|
+
readonly Proxy: ComponentType<ProxyProps<Request, Props>>;
|
|
56
|
+
readonly types: {
|
|
57
|
+
readonly init: Init;
|
|
58
|
+
readonly middleware: ComponentMiddleware<Request, Props, Init>;
|
|
59
|
+
readonly middlewareComponentProps: AsMiddlewareComponentProps<Request, Props, Init>;
|
|
60
|
+
readonly props: Props;
|
|
61
|
+
readonly proxyProps: ProxyProps<Request, Props>;
|
|
62
|
+
readonly request: Request;
|
|
63
|
+
};
|
|
64
|
+
readonly useBuildComponentCallback: () => UseBuildComponentCallback<Request, Props>;
|
|
65
|
+
};
|
|
66
|
+
declare function createChainOfResponsibility<Request = void, Props extends object = {
|
|
67
|
+
readonly children?: never;
|
|
68
|
+
}, Init = void>(options?: CreateChainOfResponsibilityOptions): ChainOfResponsibility<Request, Props, Init>;
|
|
69
|
+
|
|
70
|
+
export { type CreateChainOfResponsibilityOptions as C, type ComponentMiddleware as a, createChainOfResponsibility as c };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { ComponentType, PropsWithChildren } from 'react';
|
|
2
|
+
|
|
3
|
+
type Fn<P extends any[], R> = (...args: P) => R;
|
|
4
|
+
type Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;
|
|
5
|
+
type Middleware<P extends any[], R, S extends any[]> = (...init: S) => Enhancer<P, R>;
|
|
6
|
+
|
|
7
|
+
type ComponentMiddleware<Request, Props = {
|
|
8
|
+
children?: never;
|
|
9
|
+
}, Init = undefined> = Middleware<[
|
|
10
|
+
Request
|
|
11
|
+
], ComponentType<Props> | false | null | undefined, [
|
|
12
|
+
Init
|
|
13
|
+
]>;
|
|
14
|
+
|
|
15
|
+
type ResultComponent<Props> = ComponentType<Props> | false | null | undefined;
|
|
16
|
+
type UseBuildComponentCallbackOptions<Props> = {
|
|
17
|
+
fallbackComponent?: ResultComponent<Props> | undefined;
|
|
18
|
+
};
|
|
19
|
+
interface UseBuildComponentCallback<Request, Props> {
|
|
20
|
+
(request: Request, options?: undefined | UseBuildComponentCallbackOptions<Props>): ComponentType<Props> | undefined;
|
|
21
|
+
}
|
|
22
|
+
type ProviderProps<Request, Props, Init> = PropsWithChildren<{
|
|
23
|
+
middleware: readonly ComponentMiddleware<Request, Props, Init>[];
|
|
24
|
+
}> & (Init extends never | void ? {
|
|
25
|
+
readonly init?: undefined;
|
|
26
|
+
} : Init extends undefined | void ? {
|
|
27
|
+
readonly init?: Init;
|
|
28
|
+
} : {
|
|
29
|
+
readonly init: Init;
|
|
30
|
+
});
|
|
31
|
+
type ProxyProps<Request, Props extends object> = Props & {
|
|
32
|
+
readonly fallbackComponent?: ComponentType<Props> | undefined;
|
|
33
|
+
readonly request: Request;
|
|
34
|
+
};
|
|
35
|
+
type CreateChainOfResponsibilityOptions = {
|
|
36
|
+
/**
|
|
37
|
+
* Allows a middleware to pass another request object when calling its next middleware. Default is false.
|
|
38
|
+
*
|
|
39
|
+
* However, middleware could modify the request object before calling its next middleware. It is recommended
|
|
40
|
+
* to use Object.freeze() to prevent middleware from modifying the request object.
|
|
41
|
+
*/
|
|
42
|
+
readonly passModifiedRequest?: boolean | undefined;
|
|
43
|
+
};
|
|
44
|
+
type AsMiddlewareProps<Request, Props, Init> = {
|
|
45
|
+
readonly init: Init;
|
|
46
|
+
readonly Next: ComponentType<Partial<Props>>;
|
|
47
|
+
readonly request: Request;
|
|
48
|
+
};
|
|
49
|
+
type AsMiddlewareComponentProps<Request, Props, Init> = Props & {
|
|
50
|
+
readonly middleware: AsMiddlewareProps<Request, Props, Init>;
|
|
51
|
+
};
|
|
52
|
+
type ChainOfResponsibility<Request, Props extends object, Init> = {
|
|
53
|
+
readonly asMiddleware: (middlewareComponent: ComponentType<AsMiddlewareComponentProps<Request, Props, Init>>) => ComponentMiddleware<Request, Props, Init>;
|
|
54
|
+
readonly Provider: ComponentType<ProviderProps<Request, Props, Init>>;
|
|
55
|
+
readonly Proxy: ComponentType<ProxyProps<Request, Props>>;
|
|
56
|
+
readonly types: {
|
|
57
|
+
readonly init: Init;
|
|
58
|
+
readonly middleware: ComponentMiddleware<Request, Props, Init>;
|
|
59
|
+
readonly middlewareComponentProps: AsMiddlewareComponentProps<Request, Props, Init>;
|
|
60
|
+
readonly props: Props;
|
|
61
|
+
readonly proxyProps: ProxyProps<Request, Props>;
|
|
62
|
+
readonly request: Request;
|
|
63
|
+
};
|
|
64
|
+
readonly useBuildComponentCallback: () => UseBuildComponentCallback<Request, Props>;
|
|
65
|
+
};
|
|
66
|
+
declare function createChainOfResponsibility<Request = void, Props extends object = {
|
|
67
|
+
readonly children?: never;
|
|
68
|
+
}, Init = void>(options?: CreateChainOfResponsibilityOptions): ChainOfResponsibility<Request, Props, Init>;
|
|
69
|
+
|
|
70
|
+
export { type CreateChainOfResponsibilityOptions as C, type ComponentMiddleware as a, createChainOfResponsibility as c };
|
|
@@ -1,72 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
type Fn<P extends any[], R> = (...args: P) => R;
|
|
4
|
-
type Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;
|
|
5
|
-
type Middleware<P extends any[], R, S extends any[]> = (...init: S) => Enhancer<P, R>;
|
|
6
|
-
|
|
7
|
-
type ComponentMiddleware<Request, Props = {
|
|
8
|
-
children?: never;
|
|
9
|
-
}, Init = undefined> = Middleware<[
|
|
10
|
-
Request
|
|
11
|
-
], ComponentType<Props> | false | null | undefined, [
|
|
12
|
-
Init
|
|
13
|
-
]>;
|
|
14
|
-
|
|
15
|
-
type ResultComponent<Props> = ComponentType<Props> | false | null | undefined;
|
|
16
|
-
type UseBuildComponentCallbackOptions<Props> = {
|
|
17
|
-
fallbackComponent?: ResultComponent<Props>;
|
|
18
|
-
};
|
|
19
|
-
type UseBuildComponentCallback<Request, Props> = (request: Request, options?: UseBuildComponentCallbackOptions<Props>) => ComponentType<Props> | undefined;
|
|
20
|
-
type ProviderProps<Request, Props, Init> = PropsWithChildren<{
|
|
21
|
-
middleware: readonly ComponentMiddleware<Request, Props, Init>[];
|
|
22
|
-
}> & (Init extends never | void ? {
|
|
23
|
-
init?: undefined;
|
|
24
|
-
} : Init extends undefined ? {
|
|
25
|
-
init?: Init;
|
|
26
|
-
} : {
|
|
27
|
-
init: Init;
|
|
28
|
-
});
|
|
29
|
-
type ProxyProps<Request, Props extends object> = PropsWithChildren<Request extends never | void ? Props & {
|
|
30
|
-
fallbackComponent?: ComponentType<Props>;
|
|
31
|
-
request?: undefined;
|
|
32
|
-
} : Request extends undefined ? Props & {
|
|
33
|
-
fallbackComponent?: ComponentType<Props>;
|
|
34
|
-
request?: Request;
|
|
35
|
-
} : Props & {
|
|
36
|
-
fallbackComponent?: ComponentType<Props>;
|
|
37
|
-
request: Request;
|
|
38
|
-
}>;
|
|
39
|
-
type Options = {
|
|
40
|
-
/**
|
|
41
|
-
* Allows a middleware to pass another request object when calling its next middleware. Default is false.
|
|
42
|
-
*
|
|
43
|
-
* However, middleware could modify the request object before calling its next middleware. It is recommended
|
|
44
|
-
* to use Object.freeze() to prevent middleware from modifying the request object.
|
|
45
|
-
*/
|
|
46
|
-
passModifiedRequest?: boolean;
|
|
47
|
-
};
|
|
48
|
-
type MiddlewareProps<Request, Props, Init> = Readonly<{
|
|
49
|
-
init: Init;
|
|
50
|
-
Next: ComponentType<Partial<Props>>;
|
|
51
|
-
request: Request;
|
|
52
|
-
}>;
|
|
53
|
-
type MiddlewareComponentProps<Request, Props, Init> = Props & Readonly<{
|
|
54
|
-
middleware: MiddlewareProps<Request, Props, Init>;
|
|
55
|
-
}>;
|
|
56
|
-
declare function createChainOfResponsibility<Request = undefined, Props extends object = Readonly<{
|
|
57
|
-
children?: never;
|
|
58
|
-
}>, Init = undefined>(options?: Options): {
|
|
59
|
-
asMiddleware: (middlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>) => ComponentMiddleware<Request, Props, Init>;
|
|
60
|
-
Provider: ComponentType<ProviderProps<Request, Props, Init>>;
|
|
61
|
-
Proxy: ComponentType<ProxyProps<Request, Props>>;
|
|
62
|
-
types: {
|
|
63
|
-
init: Init;
|
|
64
|
-
middleware: ComponentMiddleware<Request, Props, Init>;
|
|
65
|
-
middlewareComponentProps: MiddlewareComponentProps<Request, Props, Init>;
|
|
66
|
-
props: Props;
|
|
67
|
-
request: Request;
|
|
68
|
-
};
|
|
69
|
-
useBuildComponentCallback: () => UseBuildComponentCallback<Request, Props>;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export { type ComponentMiddleware, createChainOfResponsibility };
|
|
1
|
+
export { a as ComponentMiddleware, c as createChainOfResponsibility } from './index-Cx9W9BxD.mjs';
|
|
2
|
+
import 'react';
|
|
@@ -1,72 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
type Fn<P extends any[], R> = (...args: P) => R;
|
|
4
|
-
type Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;
|
|
5
|
-
type Middleware<P extends any[], R, S extends any[]> = (...init: S) => Enhancer<P, R>;
|
|
6
|
-
|
|
7
|
-
type ComponentMiddleware<Request, Props = {
|
|
8
|
-
children?: never;
|
|
9
|
-
}, Init = undefined> = Middleware<[
|
|
10
|
-
Request
|
|
11
|
-
], ComponentType<Props> | false | null | undefined, [
|
|
12
|
-
Init
|
|
13
|
-
]>;
|
|
14
|
-
|
|
15
|
-
type ResultComponent<Props> = ComponentType<Props> | false | null | undefined;
|
|
16
|
-
type UseBuildComponentCallbackOptions<Props> = {
|
|
17
|
-
fallbackComponent?: ResultComponent<Props>;
|
|
18
|
-
};
|
|
19
|
-
type UseBuildComponentCallback<Request, Props> = (request: Request, options?: UseBuildComponentCallbackOptions<Props>) => ComponentType<Props> | undefined;
|
|
20
|
-
type ProviderProps<Request, Props, Init> = PropsWithChildren<{
|
|
21
|
-
middleware: readonly ComponentMiddleware<Request, Props, Init>[];
|
|
22
|
-
}> & (Init extends never | void ? {
|
|
23
|
-
init?: undefined;
|
|
24
|
-
} : Init extends undefined ? {
|
|
25
|
-
init?: Init;
|
|
26
|
-
} : {
|
|
27
|
-
init: Init;
|
|
28
|
-
});
|
|
29
|
-
type ProxyProps<Request, Props extends object> = PropsWithChildren<Request extends never | void ? Props & {
|
|
30
|
-
fallbackComponent?: ComponentType<Props>;
|
|
31
|
-
request?: undefined;
|
|
32
|
-
} : Request extends undefined ? Props & {
|
|
33
|
-
fallbackComponent?: ComponentType<Props>;
|
|
34
|
-
request?: Request;
|
|
35
|
-
} : Props & {
|
|
36
|
-
fallbackComponent?: ComponentType<Props>;
|
|
37
|
-
request: Request;
|
|
38
|
-
}>;
|
|
39
|
-
type Options = {
|
|
40
|
-
/**
|
|
41
|
-
* Allows a middleware to pass another request object when calling its next middleware. Default is false.
|
|
42
|
-
*
|
|
43
|
-
* However, middleware could modify the request object before calling its next middleware. It is recommended
|
|
44
|
-
* to use Object.freeze() to prevent middleware from modifying the request object.
|
|
45
|
-
*/
|
|
46
|
-
passModifiedRequest?: boolean;
|
|
47
|
-
};
|
|
48
|
-
type MiddlewareProps<Request, Props, Init> = Readonly<{
|
|
49
|
-
init: Init;
|
|
50
|
-
Next: ComponentType<Partial<Props>>;
|
|
51
|
-
request: Request;
|
|
52
|
-
}>;
|
|
53
|
-
type MiddlewareComponentProps<Request, Props, Init> = Props & Readonly<{
|
|
54
|
-
middleware: MiddlewareProps<Request, Props, Init>;
|
|
55
|
-
}>;
|
|
56
|
-
declare function createChainOfResponsibility<Request = undefined, Props extends object = Readonly<{
|
|
57
|
-
children?: never;
|
|
58
|
-
}>, Init = undefined>(options?: Options): {
|
|
59
|
-
asMiddleware: (middlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>) => ComponentMiddleware<Request, Props, Init>;
|
|
60
|
-
Provider: ComponentType<ProviderProps<Request, Props, Init>>;
|
|
61
|
-
Proxy: ComponentType<ProxyProps<Request, Props>>;
|
|
62
|
-
types: {
|
|
63
|
-
init: Init;
|
|
64
|
-
middleware: ComponentMiddleware<Request, Props, Init>;
|
|
65
|
-
middlewareComponentProps: MiddlewareComponentProps<Request, Props, Init>;
|
|
66
|
-
props: Props;
|
|
67
|
-
request: Request;
|
|
68
|
-
};
|
|
69
|
-
useBuildComponentCallback: () => UseBuildComponentCallback<Request, Props>;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export { type ComponentMiddleware, createChainOfResponsibility };
|
|
1
|
+
export { a as ComponentMiddleware, c as createChainOfResponsibility } from './index-Cx9W9BxD.js';
|
|
2
|
+
import 'react';
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { IRenderFunction } from '@fluentui/react';
|
|
2
2
|
import { Key } from 'react';
|
|
3
|
-
import { createChainOfResponsibility } from './
|
|
3
|
+
import { C as CreateChainOfResponsibilityOptions, c as createChainOfResponsibility } from './index-Cx9W9BxD.mjs';
|
|
4
4
|
|
|
5
5
|
type UseBuildRenderFunctionOptions<Props> = {
|
|
6
6
|
getKey?: (props: Props | undefined) => Key;
|
|
7
7
|
};
|
|
8
8
|
type UseBuildRenderFunction<Props> = (options?: UseBuildRenderFunctionOptions<Props>) => IRenderFunction<Props>;
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated Fluent UI v9 no longer use `IRenderFunction` for custom render. We no longer validate the correctness of this function.
|
|
11
|
+
*/
|
|
12
|
+
declare function createChainOfResponsibilityForFluentUI<Props extends object, Init = undefined>(options?: CreateChainOfResponsibilityOptions): ReturnType<typeof createChainOfResponsibility<Props | undefined, Props, Init>> & {
|
|
10
13
|
useBuildRenderFunction: UseBuildRenderFunction<Props>;
|
|
11
14
|
};
|
|
12
15
|
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { IRenderFunction } from '@fluentui/react';
|
|
2
2
|
import { Key } from 'react';
|
|
3
|
-
import { createChainOfResponsibility } from './
|
|
3
|
+
import { C as CreateChainOfResponsibilityOptions, c as createChainOfResponsibility } from './index-Cx9W9BxD.js';
|
|
4
4
|
|
|
5
5
|
type UseBuildRenderFunctionOptions<Props> = {
|
|
6
6
|
getKey?: (props: Props | undefined) => Key;
|
|
7
7
|
};
|
|
8
8
|
type UseBuildRenderFunction<Props> = (options?: UseBuildRenderFunctionOptions<Props>) => IRenderFunction<Props>;
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated Fluent UI v9 no longer use `IRenderFunction` for custom render. We no longer validate the correctness of this function.
|
|
11
|
+
*/
|
|
12
|
+
declare function createChainOfResponsibilityForFluentUI<Props extends object, Init = undefined>(options?: CreateChainOfResponsibilityOptions): ReturnType<typeof createChainOfResponsibility<Props | undefined, Props, Init>> & {
|
|
10
13
|
useBuildRenderFunction: UseBuildRenderFunction<Props>;
|
|
11
14
|
};
|
|
12
15
|
|
|
@@ -42,15 +42,13 @@ var import_react = __toESM(require("react"));
|
|
|
42
42
|
|
|
43
43
|
// src/isReactComponent.ts
|
|
44
44
|
function isConsumer(component) {
|
|
45
|
-
|
|
46
|
-
return ((_a = component == null ? void 0 : component.$$typeof) == null ? void 0 : _a.toString()) === "Symbol(react.context)";
|
|
45
|
+
return component?.$$typeof?.toString() === "Symbol(react.context)";
|
|
47
46
|
}
|
|
48
47
|
function isProvider(component) {
|
|
49
|
-
|
|
50
|
-
return ((_a = component == null ? void 0 : component.$$typeof) == null ? void 0 : _a.toString()) === "Symbol(react.provider)";
|
|
48
|
+
return component?.$$typeof?.toString() === "Symbol(react.provider)";
|
|
51
49
|
}
|
|
52
50
|
function isFragment(component) {
|
|
53
|
-
return
|
|
51
|
+
return component?.toString() === "Symbol(react.fragment)";
|
|
54
52
|
}
|
|
55
53
|
function isFunctionComponent(component) {
|
|
56
54
|
if (typeof component === "function") {
|
|
@@ -59,11 +57,10 @@ function isFunctionComponent(component) {
|
|
|
59
57
|
return isPureFunctionComponent(component);
|
|
60
58
|
}
|
|
61
59
|
function isPureFunctionComponent(component) {
|
|
62
|
-
|
|
63
|
-
return ((_a = component == null ? void 0 : component.$$typeof) == null ? void 0 : _a.toString()) === "Symbol(react.memo)" && isFunctionComponent(component.type);
|
|
60
|
+
return component?.$$typeof?.toString() === "Symbol(react.memo)" && isFunctionComponent(component.type);
|
|
64
61
|
}
|
|
65
62
|
function isComponentClass(component) {
|
|
66
|
-
return typeof component === "object" && typeof
|
|
63
|
+
return typeof component === "object" && typeof component?.["render"] === "function";
|
|
67
64
|
}
|
|
68
65
|
function isReactComponent(component) {
|
|
69
66
|
return isFunctionComponent(component) || isComponentClass(component) || isFragment(component) || isConsumer(component) || isProvider(component);
|
|
@@ -88,11 +85,11 @@ function createChainOfResponsibility(options = {}) {
|
|
|
88
85
|
get enhancer() {
|
|
89
86
|
return void 0;
|
|
90
87
|
},
|
|
91
|
-
useBuildComponentCallback(
|
|
92
|
-
if (options2
|
|
93
|
-
|
|
88
|
+
useBuildComponentCallback(_request, options2) {
|
|
89
|
+
if (!options2?.fallbackComponent) {
|
|
90
|
+
throw new Error("This component/hook cannot be used outside of its corresponding <Provider>");
|
|
94
91
|
}
|
|
95
|
-
|
|
92
|
+
return options2.fallbackComponent;
|
|
96
93
|
}
|
|
97
94
|
};
|
|
98
95
|
const context = (0, import_react.createContext)(defaultUseBuildComponentCallback);
|
|
@@ -149,10 +146,10 @@ function createChainOfResponsibility(options = {}) {
|
|
|
149
146
|
return /* @__PURE__ */ import_react.default.createElement(context.Provider, { value: contextValue }, children);
|
|
150
147
|
}
|
|
151
148
|
const useBuildComponentCallback = () => (0, import_react.useContext)(context).useBuildComponentCallback;
|
|
152
|
-
function Proxy2({
|
|
149
|
+
function Proxy2({ fallbackComponent, request, ...props }) {
|
|
153
150
|
const enhancer = useBuildComponentCallback();
|
|
154
151
|
const Component = enhancer(request, { fallbackComponent });
|
|
155
|
-
return Component ? /* @__PURE__ */ import_react.default.createElement(Component, { ...props }
|
|
152
|
+
return Component ? /* @__PURE__ */ import_react.default.createElement(Component, { ...props }) : null;
|
|
156
153
|
}
|
|
157
154
|
const asMiddleware = (MiddlewareComponent) => (init) => (next) => (request) => {
|
|
158
155
|
const RawNextComponent = next(request);
|
|
@@ -172,29 +169,35 @@ function createChainOfResponsibility(options = {}) {
|
|
|
172
169
|
MiddlewareOf.displayName = `MiddlewareOf<${MiddlewareComponent.displayName || ""}>`;
|
|
173
170
|
return (0, import_react.memo)(MiddlewareOf);
|
|
174
171
|
};
|
|
175
|
-
return {
|
|
172
|
+
return Object.freeze({
|
|
176
173
|
asMiddleware,
|
|
177
174
|
Provider: (0, import_react.memo)(ChainOfResponsibilityProvider),
|
|
178
175
|
Proxy: (0, import_react.memo)(Proxy2),
|
|
179
|
-
types: {
|
|
176
|
+
types: Object.freeze({
|
|
180
177
|
middlewareComponentProps: void 0,
|
|
181
178
|
init: void 0,
|
|
182
179
|
middleware: void 0,
|
|
183
180
|
props: void 0,
|
|
181
|
+
proxyProps: void 0,
|
|
184
182
|
request: void 0
|
|
185
|
-
},
|
|
183
|
+
}),
|
|
186
184
|
useBuildComponentCallback
|
|
187
|
-
};
|
|
185
|
+
});
|
|
188
186
|
}
|
|
187
|
+
var createChainOfResponsibility_default = createChainOfResponsibility;
|
|
189
188
|
|
|
190
189
|
// src/createChainOfResponsibilityForFluentUI.tsx
|
|
191
190
|
function createChainOfResponsibilityForFluentUI(options) {
|
|
192
|
-
const returnValue =
|
|
191
|
+
const returnValue = createChainOfResponsibility_default(options);
|
|
193
192
|
const { Proxy: Proxy2 } = returnValue;
|
|
194
193
|
const useBuildRenderFunction = (options2 = {}) => {
|
|
195
194
|
const { getKey } = options2;
|
|
196
195
|
return (0, import_react2.useCallback)(
|
|
197
|
-
(props, defaultRender) =>
|
|
196
|
+
(props, defaultRender) => (
|
|
197
|
+
// We no longer validate the correctness of this function.
|
|
198
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
199
|
+
/* @__PURE__ */ import_react2.default.createElement(Proxy2, { ...props, fallbackComponent: defaultRender, key: getKey?.(props), request: props })
|
|
200
|
+
),
|
|
198
201
|
[getKey]
|
|
199
202
|
);
|
|
200
203
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.fluentUI.ts","../src/createChainOfResponsibilityForFluentUI.tsx","../src/createChainOfResponsibility.tsx","../src/isReactComponent.ts","../src/private/compose.ts","../src/private/applyMiddleware.ts"],"sourcesContent":["import createChainOfResponsibilityForFluentUI from './createChainOfResponsibilityForFluentUI.tsx';\n\nexport { createChainOfResponsibilityForFluentUI };\n","import { type IRenderFunction } from '@fluentui/react';\nimport React, { useCallback, type Key } from 'react';\n\nimport createChainOfResponsibility from './createChainOfResponsibility.tsx';\n\ntype UseBuildRenderFunctionOptions<Props> = { getKey?: (props: Props | undefined) => Key };\n\ntype UseBuildRenderFunction<Props> = (options?: UseBuildRenderFunctionOptions<Props>) => IRenderFunction<Props>;\n\n// We are using the props as both \"Request\" and \"Props\".\n// This should eases migration from `onRender` to chain of responsibility.\n// Downside is, web developers could accidentally pass request as props and not honoring props modified by upstreamers.\nexport default function createChainOfResponsibilityForFluentUI<Props extends object, Init = undefined>(\n options?: Parameters<typeof createChainOfResponsibility>[0]\n): ReturnType<typeof createChainOfResponsibility<Props | undefined, Props, Init>> & {\n useBuildRenderFunction: UseBuildRenderFunction<Props>;\n} {\n const returnValue = createChainOfResponsibility<Props | undefined, Props, Init>(options);\n\n const { Proxy } = returnValue;\n\n const useBuildRenderFunction: UseBuildRenderFunction<Props> = (options = {}) => {\n const { getKey } = options;\n\n return useCallback<IRenderFunction<Props>>(\n (props, defaultRender) => (\n <Proxy {...(props as Props)} fallbackComponent={defaultRender} key={getKey?.(props)} request={props} />\n ),\n [getKey]\n );\n };\n\n return { ...returnValue, useBuildRenderFunction };\n}\n","import React, {\n createContext,\n isValidElement,\n memo,\n useCallback,\n useContext,\n useMemo,\n type ComponentType,\n type PropsWithChildren\n} from 'react';\n\nimport isReactComponent from './isReactComponent.ts';\nimport applyMiddleware, { type Enhancer } from './private/applyMiddleware.ts';\nimport { type ComponentMiddleware } from './types.ts';\n\n// TODO: Simplify to ComponentType<Props> | undefined.\ntype ResultComponent<Props> = ComponentType<Props> | false | null | undefined;\n\ntype UseBuildComponentCallbackOptions<Props> = { fallbackComponent?: ResultComponent<Props> };\n\ntype UseBuildComponentCallback<Request, Props> = (\n request: Request,\n options?: UseBuildComponentCallbackOptions<Props>\n) => ComponentType<Props> | undefined;\n\ntype ProviderContext<Request, Props> = {\n get enhancer(): Enhancer<[Request], ResultComponent<Props>> | undefined;\n useBuildComponentCallback: UseBuildComponentCallback<Request, Props>;\n};\n\ntype ProviderProps<Request, Props, Init> = PropsWithChildren<{\n middleware: readonly ComponentMiddleware<Request, Props, Init>[];\n}> &\n (Init extends never | void ? { init?: undefined } : Init extends undefined ? { init?: Init } : { init: Init });\n\ntype ProxyProps<Request, Props extends object> = PropsWithChildren<\n Request extends never | void\n ? Props & { fallbackComponent?: ComponentType<Props>; request?: undefined }\n : Request extends undefined\n ? Props & { fallbackComponent?: ComponentType<Props>; request?: Request }\n : Props & { fallbackComponent?: ComponentType<Props>; request: Request }\n>;\n\ntype Options = {\n /**\n * Allows a middleware to pass another request object when calling its next middleware. Default is false.\n *\n * However, middleware could modify the request object before calling its next middleware. It is recommended\n * to use Object.freeze() to prevent middleware from modifying the request object.\n */\n passModifiedRequest?: boolean;\n};\n\ntype MiddlewareProps<Request, Props, Init> = Readonly<{\n init: Init;\n Next: ComponentType<Partial<Props>>;\n request: Request;\n}>;\n\ntype MiddlewareComponentProps<Request, Props, Init> = Props &\n Readonly<{ middleware: MiddlewareProps<Request, Props, Init> }>;\n\nexport default function createChainOfResponsibility<\n Request = undefined,\n Props extends object = Readonly<{ children?: never }>,\n Init = undefined\n>(\n options: Options = {}\n): {\n asMiddleware: (\n middlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init>;\n Provider: ComponentType<ProviderProps<Request, Props, Init>>;\n Proxy: ComponentType<ProxyProps<Request, Props>>;\n types: {\n init: Init;\n middleware: ComponentMiddleware<Request, Props, Init>;\n middlewareComponentProps: MiddlewareComponentProps<Request, Props, Init>;\n props: Props;\n request: Request;\n };\n useBuildComponentCallback: () => UseBuildComponentCallback<Request, Props>;\n} {\n const defaultUseBuildComponentCallback: ProviderContext<Request, Props> = {\n get enhancer() {\n return undefined;\n },\n useBuildComponentCallback(_, options) {\n if (options?.fallbackComponent) {\n return options.fallbackComponent;\n }\n\n throw new Error('This component/hook cannot be used outside of its corresponding <Provider>');\n }\n };\n\n const context = createContext<ProviderContext<Request, Props>>(defaultUseBuildComponentCallback);\n\n function ChainOfResponsibilityProvider({ children, init, middleware }: ProviderProps<Request, Props, Init>) {\n // TODO: Related to https://github.com/microsoft/TypeScript/issues/17002.\n // typescript@5.2.2 has a bug, Array.isArray() is a type predicate but only works with mutable array, not readonly array.\n // After removing \"as unknown\", `middleware` on the next line become `any[]`.\n if (!Array.isArray(middleware as unknown) || middleware.some(middleware => typeof middleware !== 'function')) {\n throw new Error('middleware prop must be an array of functions');\n }\n\n const patchedMiddleware: readonly ComponentMiddleware<Request, Props, Init>[] = Object.freeze(\n middleware\n ? middleware.map(fn => (init: Init) => {\n const enhancer = fn(init);\n\n return (next: (request: Request) => ComponentType<Props> | false | null | undefined) =>\n (originalRequest: Request) => {\n // False positive: although we did not re-assign the variable from true, it was initialized as undefined.\n // eslint-disable-next-line prefer-const\n let hasReturned: boolean;\n\n const returnValue = enhancer(nextRequest => {\n if (hasReturned) {\n throw new Error('next() cannot be called after the function had returned synchronously');\n }\n\n !options.passModifiedRequest &&\n nextRequest !== originalRequest &&\n console.warn(\n 'react-chain-of-responsibility: \"options.passModifiedRequest\" must be set to true to pass a different request object to next().'\n );\n\n return next(options.passModifiedRequest ? nextRequest : originalRequest);\n })(originalRequest);\n\n hasReturned = true;\n\n if (isValidElement(returnValue)) {\n throw new Error('middleware must not return React element directly');\n } else if (\n returnValue !== false &&\n returnValue !== null &&\n typeof returnValue !== 'undefined' &&\n !isReactComponent(returnValue)\n ) {\n throw new Error(\n 'middleware must return false, null, undefined, function component, or class component'\n );\n }\n\n return returnValue;\n };\n })\n : []\n );\n\n const { enhancer: parentEnhancer } = useContext(context);\n\n const enhancer = useMemo(\n () =>\n // We are reversing because it is easier to read:\n // - With reverse, [a, b, c] will become a(b(c(fn)))\n // - Without reverse, [a, b, c] will become c(b(a(fn)))\n applyMiddleware<[Request], ResultComponent<Props>, [Init]>(\n ...[...patchedMiddleware, ...(parentEnhancer ? [() => parentEnhancer] : [])].reverse()\n )(init as Init),\n [init, middleware, parentEnhancer]\n );\n\n const useBuildComponentCallback = useCallback<UseBuildComponentCallback<Request, Props>>(\n (request, options = {}) => enhancer(() => options.fallbackComponent)(request) || undefined,\n [enhancer]\n );\n\n const contextValue = useMemo<ProviderContext<Request, Props>>(\n () => ({ enhancer, useBuildComponentCallback }),\n [enhancer, useBuildComponentCallback]\n );\n\n return <context.Provider value={contextValue}>{children}</context.Provider>;\n }\n\n const useBuildComponentCallback = () => useContext(context).useBuildComponentCallback;\n\n function Proxy({ children, fallbackComponent, request, ...props }: ProxyProps<Request, Props>) {\n const enhancer = useBuildComponentCallback();\n const Component = enhancer(request as Request, { fallbackComponent });\n\n return Component ? <Component {...(props as Props)}>{children}</Component> : null;\n }\n\n const asMiddleware: (\n middlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init> =\n (\n MiddlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>\n ): ComponentMiddleware<Request, Props, Init> =>\n init =>\n next =>\n request => {\n const RawNextComponent = next(request);\n\n // TODO: Can we pre-build this component during init?\n const MiddlewareOf = (props: Props) => {\n const middleware = useMemo(\n () =>\n Object.freeze({\n init,\n Next: memo<Partial<Props>>(\n RawNextComponent\n ? (overridingProps: Partial<Props>) => <RawNextComponent {...props} {...overridingProps} />\n : () => null\n ),\n request\n }),\n []\n );\n\n return <MiddlewareComponent {...props} middleware={middleware} />;\n };\n\n MiddlewareOf.displayName = `MiddlewareOf<${MiddlewareComponent.displayName || ''}>`;\n\n return memo<Props>(MiddlewareOf);\n };\n\n return {\n asMiddleware,\n Provider: memo<ProviderProps<Request, Props, Init>>(ChainOfResponsibilityProvider),\n Proxy: memo<ProxyProps<Request, Props>>(Proxy),\n types: {\n middlewareComponentProps: undefined as unknown as MiddlewareComponentProps<Request, Props, Init>,\n init: undefined as unknown as Init,\n middleware: undefined as unknown as ComponentMiddleware<Request, Props, Init>,\n props: undefined as unknown as Props,\n request: undefined as unknown as Request\n },\n useBuildComponentCallback\n };\n}\n","import {\n type ComponentClass,\n type ComponentType,\n type Consumer,\n type Fragment,\n type FunctionComponent,\n type Provider\n} from 'react';\n\nfunction isConsumer(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Consumer<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.context)';\n}\n\nfunction isProvider(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Provider<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.provider)';\n}\n\nfunction isFragment(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is typeof Fragment {\n return component?.toString() === 'Symbol(react.fragment)';\n}\n\nfunction isFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n if (typeof component === 'function') {\n return true;\n }\n\n return isPureFunctionComponent(component);\n}\n\nfunction isPureFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n return component?.$$typeof?.toString() === 'Symbol(react.memo)' && isFunctionComponent(component.type);\n}\n\nfunction isComponentClass(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentClass {\n return typeof component === 'object' && typeof component?.['render'] === 'function';\n}\n\n// There are no definitive ways to check if an object is a React component or not.\n// We are checking if the object has a render function (classic component).\n// Note: \"forwardRef()\" returns plain object, not class instance.\nexport default function isReactComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentType {\n return (\n isFunctionComponent(component) ||\n isComponentClass(component) ||\n isFragment(component) ||\n isConsumer(component) ||\n isProvider(component)\n );\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function compose<P extends any[], R>(...fns: Enhancer<P, R>[]): Enhancer<P, R> {\n return (fn: Fn<P, R>): Fn<P, R> => fns.reduce((chain, fn) => fn(chain), fn);\n}\n","import compose from './compose.ts';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Middleware<P extends any[], R, S extends any[]> = (...init: S) => Enhancer<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function applyMiddleware<P extends any[], R, S extends any[]>(\n ...arrayOfMiddleware: Middleware<P, R, S>[]\n) {\n return (...init: S) => {\n const chain = arrayOfMiddleware.map(middleware => middleware(...init));\n\n return compose(...chain);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,IAAAA,gBAA6C;;;ACD7C,mBASO;;;ACAP,SAAS,WAEP,WACgC;AAZlC;AAaE,WAAO,4CAAW,aAAX,mBAAqB,gBAAe;AAC7C;AAEA,SAAS,WAEP,WACgC;AAnBlC;AAoBE,WAAO,4CAAW,aAAX,mBAAqB,gBAAe;AAC7C;AAEA,SAAS,WAEP,WAC8B;AAC9B,UAAO,uCAAW,gBAAe;AACnC;AAEA,SAAS,oBAEP,WACgC;AAChC,MAAI,OAAO,cAAc,YAAY;AACnC,WAAO;AAAA,EACT;AAEA,SAAO,wBAAwB,SAAS;AAC1C;AAEA,SAAS,wBAEP,WACgC;AA5ClC;AA6CE,WAAO,4CAAW,aAAX,mBAAqB,gBAAe,wBAAwB,oBAAoB,UAAU,IAAI;AACvG;AAEA,SAAS,iBAEP,WAC6B;AAC7B,SAAO,OAAO,cAAc,YAAY,QAAO,uCAAY,eAAc;AAC3E;AAKe,SAAR,iBAEL,WAC4B;AAC5B,SACE,o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S,IAAe;AAAA,EAC/E;AAEA,QAAM,eAGJ,CACE,wBAEF,UACA,UACA,aAAW;AACT,UAAM,mBAAmB,KAAK,OAAO;AAGrC,UAAM,eAAe,CAAC,UAAiB;AACrC,YAAM,iBAAa;AAAA,QACjB,MACE,OAAO,OAAO;AAAA,UACZ;AAAA,UACA,UAAM;AAAA,YACJ,mBACI,CAAC,oBAAoC,6BAAAA,QAAA,cAAC,oBAAkB,GAAG,OAAQ,GAAG,iBAAiB,IACvF,MAAM;AAAA,UACZ;AAAA,UACA;AAAA,QACF,CAAC;AAAA,QACH,CAAC;AAAA,MACH;AAEA,aAAO,6BAAAA,QAAA,cAAC,uBAAqB,GAAG,OAAO,YAAwB;AAAA,IACjE;AAEA,iBAAa,cAAc,gBAAgB,oBAAoB,eAAe,EAAE;AAEhF,eAAO,mBAAY,YAAY;AAAA,EACjC;AAEF,SAAO;AAAA,IACL;AAAA,IACA,cAAU,mBAA0C,6BAA6B;AAAA,IACjF,WAAO,mBAAiCC,MAAK;AAAA,IAC7C,OAAO;AAAA,MACL,0BAA0B;AAAA,MAC1B,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,OAAO;AAAA,MACP,SAAS;AAAA,IACX;AAAA,IACA;AAAA,EACF;AACF;;;AD/Ne,SAAR,uCACL,SAGA;AACA,QAAM,cAAc,4BAA4D,OAAO;AAEvF,QAAM,EAAE,OAAAC,OAAM,IAAI;AAElB,QAAM,yBAAwD,CAACC,WAAU,CAAC,MAAM;AAC9E,UAAM,EAAE,OAAO,IAAIA;AAEnB,eAAO;AAAA,MACL,CAAC,OAAO,kBACN,8BAAAC,QAAA,cAACF,QAAA,EAAO,GAAI,OAAiB,mBAAmB,eAAe,KAAK,iCAAS,QAAQ,SAAS,OAAO;AAAA,MAEvG,CAAC,MAAM;AAAA,IACT;AAAA,EACF;AAEA,SAAO,EAAE,GAAG,aAAa,uBAAuB;AAClD;","names":["import_react","fn","options","middleware","init","enhancer","useBuildComponentCallback","React","Proxy","Proxy","options","React"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.fluentUI.ts","../src/createChainOfResponsibilityForFluentUI.tsx","../src/createChainOfResponsibility.tsx","../src/isReactComponent.ts","../src/private/compose.ts","../src/private/applyMiddleware.ts"],"sourcesContent":["// eslint-disable-next-line import/no-deprecated\nimport createChainOfResponsibilityForFluentUI from './createChainOfResponsibilityForFluentUI.tsx';\n\n// eslint-disable-next-line import/no-deprecated\nexport { createChainOfResponsibilityForFluentUI };\n","import { type IRenderFunction } from '@fluentui/react';\nimport React, { useCallback, type Key } from 'react';\n\nimport createChainOfResponsibility, {\n type CreateChainOfResponsibilityOptions\n} from './createChainOfResponsibility.tsx';\n\ntype UseBuildRenderFunctionOptions<Props> = { getKey?: (props: Props | undefined) => Key };\n\ntype UseBuildRenderFunction<Props> = (options?: UseBuildRenderFunctionOptions<Props>) => IRenderFunction<Props>;\n\n// We are using the props as both \"Request\" and \"Props\".\n// This should eases migration from `onRender` to chain of responsibility.\n// Downside is, web developers could accidentally pass request as props and not honoring props modified by upstreamers.\n\n/**\n * @deprecated Fluent UI v9 no longer use `IRenderFunction` for custom render. We no longer validate the correctness of this function.\n */\nexport default function createChainOfResponsibilityForFluentUI<Props extends object, Init = undefined>(\n options?: CreateChainOfResponsibilityOptions\n): ReturnType<typeof createChainOfResponsibility<Props | undefined, Props, Init>> & {\n useBuildRenderFunction: UseBuildRenderFunction<Props>;\n} {\n // We no longer validate the correctness of this function.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const returnValue = createChainOfResponsibility<Props | undefined, Props, Init>(options as any);\n\n const { Proxy } = returnValue;\n\n const useBuildRenderFunction: UseBuildRenderFunction<Props> = (options = {}) => {\n const { getKey } = options;\n\n return useCallback<IRenderFunction<Props>>(\n (props, defaultRender) => (\n // We no longer validate the correctness of this function.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n <Proxy {...(props as Props)} fallbackComponent={defaultRender as any} key={getKey?.(props)} request={props} />\n ),\n [getKey]\n );\n };\n\n // We no longer validate the correctness of this function.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return { ...returnValue, useBuildRenderFunction } as any;\n}\n","import React, {\n createContext,\n isValidElement,\n memo,\n useCallback,\n useContext,\n useMemo,\n type ComponentType,\n type PropsWithChildren\n} from 'react';\n\nimport isReactComponent from './isReactComponent.ts';\nimport applyMiddleware, { type Enhancer } from './private/applyMiddleware.ts';\nimport { type ComponentMiddleware } from './types.ts';\n\n// TODO: Simplify to ComponentType<Props> | undefined.\ntype ResultComponent<Props> = ComponentType<Props> | false | null | undefined;\n\ntype UseBuildComponentCallbackOptions<Props> = {\n fallbackComponent?: ResultComponent<Props> | undefined;\n};\n\ninterface UseBuildComponentCallback<Request, Props> {\n (request: Request, options?: undefined | UseBuildComponentCallbackOptions<Props>): ComponentType<Props> | undefined;\n}\n\ntype ProviderContext<Request, Props> = {\n get enhancer(): Enhancer<[Request], ResultComponent<Props>> | undefined;\n useBuildComponentCallback: UseBuildComponentCallback<Request, Props>;\n};\n\ntype ProviderProps<Request, Props, Init> = PropsWithChildren<{\n middleware: readonly ComponentMiddleware<Request, Props, Init>[];\n}> &\n (Init extends never | void\n ? { readonly init?: undefined }\n : Init extends undefined | void\n ? { readonly init?: Init }\n : { readonly init: Init });\n\ntype ProxyProps<Request, Props extends object> = Props & {\n readonly fallbackComponent?: ComponentType<Props> | undefined;\n readonly request: Request;\n};\n\ntype CreateChainOfResponsibilityOptions = {\n /**\n * Allows a middleware to pass another request object when calling its next middleware. Default is false.\n *\n * However, middleware could modify the request object before calling its next middleware. It is recommended\n * to use Object.freeze() to prevent middleware from modifying the request object.\n */\n readonly passModifiedRequest?: boolean | undefined;\n};\n\ntype AsMiddlewareProps<Request, Props, Init> = {\n readonly init: Init;\n readonly Next: ComponentType<Partial<Props>>;\n readonly request: Request;\n};\n\ntype AsMiddlewareComponentProps<Request, Props, Init> = Props & {\n readonly middleware: AsMiddlewareProps<Request, Props, Init>;\n};\n\ntype ChainOfResponsibility<Request, Props extends object, Init> = {\n readonly asMiddleware: (\n middlewareComponent: ComponentType<AsMiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init>;\n readonly Provider: ComponentType<ProviderProps<Request, Props, Init>>;\n readonly Proxy: ComponentType<ProxyProps<Request, Props>>;\n readonly types: {\n readonly init: Init;\n readonly middleware: ComponentMiddleware<Request, Props, Init>;\n readonly middlewareComponentProps: AsMiddlewareComponentProps<Request, Props, Init>;\n readonly props: Props;\n readonly proxyProps: ProxyProps<Request, Props>;\n readonly request: Request;\n };\n readonly useBuildComponentCallback: () => UseBuildComponentCallback<Request, Props>;\n};\n\nfunction createChainOfResponsibility<Request = void, Props extends object = { readonly children?: never }, Init = void>(\n options: CreateChainOfResponsibilityOptions = {}\n): ChainOfResponsibility<Request, Props, Init> {\n const defaultUseBuildComponentCallback: ProviderContext<Request, Props> = {\n get enhancer() {\n return undefined;\n },\n useBuildComponentCallback(_request, options): ComponentType<Props> {\n if (!options?.fallbackComponent) {\n throw new Error('This component/hook cannot be used outside of its corresponding <Provider>');\n }\n\n return options.fallbackComponent;\n }\n };\n\n const context = createContext<ProviderContext<Request, Props>>(defaultUseBuildComponentCallback);\n\n function ChainOfResponsibilityProvider({ children, init, middleware }: ProviderProps<Request, Props, Init>) {\n // TODO: Related to https://github.com/microsoft/TypeScript/issues/17002.\n // typescript@5.2.2 has a bug, Array.isArray() is a type predicate but only works with mutable array, not readonly array.\n // After removing \"as unknown\", `middleware` on the next line become `any[]`.\n if (!Array.isArray(middleware as unknown) || middleware.some(middleware => typeof middleware !== 'function')) {\n throw new Error('middleware prop must be an array of functions');\n }\n\n const patchedMiddleware: readonly ComponentMiddleware<Request, Props, Init>[] = Object.freeze(\n middleware\n ? middleware.map(fn => (init: Init) => {\n const enhancer = fn(init);\n\n return (next: (request: Request) => ComponentType<Props> | false | null | undefined) =>\n (originalRequest: Request) => {\n // False positive: although we did not re-assign the variable from true, it was initialized as undefined.\n // eslint-disable-next-line prefer-const\n let hasReturned: boolean;\n\n const returnValue = enhancer(nextRequest => {\n if (hasReturned) {\n throw new Error('next() cannot be called after the function had returned synchronously');\n }\n\n !options.passModifiedRequest &&\n nextRequest !== originalRequest &&\n console.warn(\n 'react-chain-of-responsibility: \"options.passModifiedRequest\" must be set to true to pass a different request object to next().'\n );\n\n return next(options.passModifiedRequest ? nextRequest : originalRequest);\n })(originalRequest);\n\n hasReturned = true;\n\n if (isValidElement(returnValue)) {\n throw new Error('middleware must not return React element directly');\n } else if (\n returnValue !== false &&\n returnValue !== null &&\n typeof returnValue !== 'undefined' &&\n !isReactComponent(returnValue)\n ) {\n throw new Error(\n 'middleware must return false, null, undefined, function component, or class component'\n );\n }\n\n return returnValue;\n };\n })\n : []\n );\n\n const { enhancer: parentEnhancer } = useContext(context);\n\n const enhancer = useMemo(\n () =>\n // We are reversing because it is easier to read:\n // - With reverse, [a, b, c] will become a(b(c(fn)))\n // - Without reverse, [a, b, c] will become c(b(a(fn)))\n applyMiddleware<[Request], ResultComponent<Props>, [Init]>(\n ...[...patchedMiddleware, ...(parentEnhancer ? [() => parentEnhancer] : [])].reverse()\n )(init as Init),\n [init, middleware, parentEnhancer]\n );\n\n const useBuildComponentCallback = useCallback<UseBuildComponentCallback<Request, Props>>(\n (request, options = {}) => enhancer(() => options.fallbackComponent)(request) || undefined,\n [enhancer]\n );\n\n const contextValue = useMemo<ProviderContext<Request, Props>>(\n () => ({ enhancer, useBuildComponentCallback }),\n [enhancer, useBuildComponentCallback]\n );\n\n return <context.Provider value={contextValue}>{children}</context.Provider>;\n }\n\n const useBuildComponentCallback = () => useContext(context).useBuildComponentCallback;\n\n function Proxy({ fallbackComponent, request, ...props }: ProxyProps<Request, Props>) {\n const enhancer = useBuildComponentCallback();\n\n const Component = enhancer(request as Request, { fallbackComponent });\n\n return Component ? <Component {...(props as Props)} /> : null;\n }\n\n const asMiddleware: (\n middlewareComponent: ComponentType<AsMiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init> =\n (\n MiddlewareComponent: ComponentType<AsMiddlewareComponentProps<Request, Props, Init>>\n ): ComponentMiddleware<Request, Props, Init> =>\n init =>\n next =>\n request => {\n const RawNextComponent = next(request);\n\n // TODO: Can we pre-build this component during init?\n const MiddlewareOf = (props: Props) => {\n const middleware = useMemo(\n () =>\n Object.freeze({\n init,\n Next: memo<Partial<Props>>(\n RawNextComponent\n ? (overridingProps: Partial<Props>) => <RawNextComponent {...props} {...overridingProps} />\n : () => null\n ),\n request\n }),\n []\n );\n\n return <MiddlewareComponent {...props} middleware={middleware} />;\n };\n\n MiddlewareOf.displayName = `MiddlewareOf<${MiddlewareComponent.displayName || ''}>`;\n\n return memo<Props>(MiddlewareOf);\n };\n\n return Object.freeze({\n asMiddleware,\n Provider: memo<ProviderProps<Request, Props, Init>>(ChainOfResponsibilityProvider),\n Proxy: memo<ProxyProps<Request, Props>>(Proxy),\n types: Object.freeze({\n middlewareComponentProps: undefined as unknown as AsMiddlewareComponentProps<Request, Props, Init>,\n init: undefined as unknown as Init,\n middleware: undefined as unknown as ComponentMiddleware<Request, Props, Init>,\n props: undefined as unknown as Props,\n proxyProps: undefined as unknown as ProxyProps<Request, Props>,\n request: undefined as unknown as Request\n }),\n useBuildComponentCallback\n });\n}\n\nexport default createChainOfResponsibility;\nexport {\n type ChainOfResponsibility,\n type CreateChainOfResponsibilityOptions,\n type ProxyProps,\n type UseBuildComponentCallback\n};\n","import {\n type ComponentClass,\n type ComponentType,\n type Consumer,\n type Fragment,\n type FunctionComponent,\n type Provider\n} from 'react';\n\nfunction isConsumer(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Consumer<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.context)';\n}\n\nfunction isProvider(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Provider<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.provider)';\n}\n\nfunction isFragment(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is typeof Fragment {\n return component?.toString() === 'Symbol(react.fragment)';\n}\n\nfunction isFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n if (typeof component === 'function') {\n return true;\n }\n\n return isPureFunctionComponent(component);\n}\n\nfunction isPureFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n return component?.$$typeof?.toString() === 'Symbol(react.memo)' && isFunctionComponent(component.type);\n}\n\nfunction isComponentClass(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentClass {\n return typeof component === 'object' && typeof component?.['render'] === 'function';\n}\n\n// There are no definitive ways to check if an object is a React component or not.\n// We are checking if the object has a render function (classic component).\n// Note: \"forwardRef()\" returns plain object, not class instance.\nexport default function isReactComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentType {\n return (\n isFunctionComponent(component) ||\n isComponentClass(component) ||\n isFragment(component) ||\n isConsumer(component) ||\n isProvider(component)\n );\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function compose<P extends any[], R>(...fns: Enhancer<P, R>[]): Enhancer<P, R> {\n return (fn: Fn<P, R>): Fn<P, R> => fns.reduce((chain, fn) => fn(chain), fn);\n}\n","import compose from './compose.ts';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Middleware<P extends any[], R, S extends any[]> = (...init: S) => Enhancer<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function applyMiddleware<P extends any[], R, S extends any[]>(\n ...arrayOfMiddleware: Middleware<P, R, S>[]\n) {\n return (...init: S) => {\n const chain = arrayOfMiddleware.map(middleware => middleware(...init));\n\n return compose(...chain);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,IAAAA,gBAA6C;;;ACD7C,mBASO;;;ACAP,SAAS,WAEP,WACgC;AAChC,SAAO,WAAW,UAAU,SAAS,MAAM;AAC7C;AAEA,SAAS,WAEP,WACgC;AAChC,SAAO,WAAW,UAAU,SAAS,MAAM;AAC7C;AAEA,SAAS,WAEP,WAC8B;AAC9B,SAAO,WAAW,SAAS,MAAM;AACnC;AAEA,SAAS,oBAEP,WACgC;AAChC,MAAI,OAAO,cAAc,YAAY;AACnC,WAAO;AAAA,EACT;AAEA,SAAO,wBAAwB,SAAS;AAC1C;AAEA,SAAS,wBAEP,WACgC;AAChC,SAAO,WAAW,UAAU,SAAS,MAAM,wBAAwB,oBAAoB,UAAU,IAAI;AACvG;AAEA,SAAS,iBAEP,WAC6B;AAC7B,SAAO,OAAO,cAAc,YAAY,OAAO,YAAY,QAAQ,MAAM;AAC3E;AAKe,SAAR,iBAEL,WAC4B;AAC5B,SACE,oBAAoB,SAAS,KAC7B,iBAAiB,SAAS,KAC1B,WAAW,SAAS,KACpB,WAAW,SAAS,KACpB,WAAW,SAAS;AAExB;;;AC9De,SAAR,WAAgD,KAAuC;AAC5F,SAAO,CAAC,OAA2B,IAAI,OAAO,CAAC,OAAOC,QAAOA,IAAG,KAAK,GAAG,EAAE;AAC5E;;;ACGe,SAAR,mBACF,mBACH;AACA,SAAO,IAAI,SAAY;AACrB,UAAM,QAAQ,kBAAkB,IAAI,gBAAc,WAAW,GAAG,IAAI,CAAC;AAErE,WAAO,QAAQ,GAAG,KAAK;AAAA,EACzB;AACF;;;AH8DA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,uBAAqB,GAAG,OAAO,YAAwB;AAAA,IACjE;AAEA,iBAAa,cAAc,gBAAgB,oBAAoB,eAAe,EAAE;AAEhF,eAAO,mBAAY,YAAY;AAAA,EACjC;AAEF,SAAO,OAAO,OAAO;AAAA,IACnB;AAAA,IACA,cAAU,mBAA0C,6BAA6B;AAAA,IACjF,WAAO,mBAAiCC,MAAK;AAAA,IAC7C,OAAO,OAAO,OAAO;AAAA,MACnB,0BAA0B;AAAA,MAC1B,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,OAAO;AAAA,MACP,YAAY;AAAA,MACZ,SAAS;AAAA,IACX,CAAC;AAAA,IACD;AAAA,EACF,CAAC;AACH;AAEA,IAAO,sCAAQ;;;AD/NA,SAAR,uCACL,SAGA;AAGA,QAAM,cAAc,oCAA4D,OAAc;AAE9F,QAAM,EAAE,OAAAC,OAAM,IAAI;AAElB,QAAM,yBAAwD,CAACC,WAAU,CAAC,MAAM;AAC9E,UAAM,EAAE,OAAO,IAAIA;AAEnB,eAAO;AAAA,MACL,CAAC,OAAO;AAAA;AAAA;AAAA,QAGN,8BAAAC,QAAA,cAACF,QAAA,EAAO,GAAI,OAAiB,mBAAmB,eAAsB,KAAK,SAAS,KAAK,GAAG,SAAS,OAAO;AAAA;AAAA,MAE9G,CAAC,MAAM;AAAA,IACT;AAAA,EACF;AAIA,SAAO,EAAE,GAAG,aAAa,uBAAuB;AAClD;","names":["import_react","fn","options","middleware","init","enhancer","useBuildComponentCallback","React","Proxy","Proxy","options","React"]}
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "./chunk-
|
|
2
|
+
createChainOfResponsibility_default
|
|
3
|
+
} from "./chunk-CLWNNXXB.mjs";
|
|
4
4
|
|
|
5
5
|
// src/createChainOfResponsibilityForFluentUI.tsx
|
|
6
6
|
import React, { useCallback } from "react";
|
|
7
7
|
function createChainOfResponsibilityForFluentUI(options) {
|
|
8
|
-
const returnValue =
|
|
8
|
+
const returnValue = createChainOfResponsibility_default(options);
|
|
9
9
|
const { Proxy } = returnValue;
|
|
10
10
|
const useBuildRenderFunction = (options2 = {}) => {
|
|
11
11
|
const { getKey } = options2;
|
|
12
12
|
return useCallback(
|
|
13
|
-
(props, defaultRender) =>
|
|
13
|
+
(props, defaultRender) => (
|
|
14
|
+
// We no longer validate the correctness of this function.
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
/* @__PURE__ */ React.createElement(Proxy, { ...props, fallbackComponent: defaultRender, key: getKey?.(props), request: props })
|
|
17
|
+
),
|
|
14
18
|
[getKey]
|
|
15
19
|
);
|
|
16
20
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/createChainOfResponsibilityForFluentUI.tsx"],"sourcesContent":["import { type IRenderFunction } from '@fluentui/react';\nimport React, { useCallback, type Key } from 'react';\n\nimport createChainOfResponsibility from './createChainOfResponsibility.tsx';\n\ntype UseBuildRenderFunctionOptions<Props> = { getKey?: (props: Props | undefined) => Key };\n\ntype UseBuildRenderFunction<Props> = (options?: UseBuildRenderFunctionOptions<Props>) => IRenderFunction<Props>;\n\n// We are using the props as both \"Request\" and \"Props\".\n// This should eases migration from `onRender` to chain of responsibility.\n// Downside is, web developers could accidentally pass request as props and not honoring props modified by upstreamers.\nexport default function createChainOfResponsibilityForFluentUI<Props extends object, Init = undefined>(\n options?:
|
|
1
|
+
{"version":3,"sources":["../src/createChainOfResponsibilityForFluentUI.tsx"],"sourcesContent":["import { type IRenderFunction } from '@fluentui/react';\nimport React, { useCallback, type Key } from 'react';\n\nimport createChainOfResponsibility, {\n type CreateChainOfResponsibilityOptions\n} from './createChainOfResponsibility.tsx';\n\ntype UseBuildRenderFunctionOptions<Props> = { getKey?: (props: Props | undefined) => Key };\n\ntype UseBuildRenderFunction<Props> = (options?: UseBuildRenderFunctionOptions<Props>) => IRenderFunction<Props>;\n\n// We are using the props as both \"Request\" and \"Props\".\n// This should eases migration from `onRender` to chain of responsibility.\n// Downside is, web developers could accidentally pass request as props and not honoring props modified by upstreamers.\n\n/**\n * @deprecated Fluent UI v9 no longer use `IRenderFunction` for custom render. We no longer validate the correctness of this function.\n */\nexport default function createChainOfResponsibilityForFluentUI<Props extends object, Init = undefined>(\n options?: CreateChainOfResponsibilityOptions\n): ReturnType<typeof createChainOfResponsibility<Props | undefined, Props, Init>> & {\n useBuildRenderFunction: UseBuildRenderFunction<Props>;\n} {\n // We no longer validate the correctness of this function.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const returnValue = createChainOfResponsibility<Props | undefined, Props, Init>(options as any);\n\n const { Proxy } = returnValue;\n\n const useBuildRenderFunction: UseBuildRenderFunction<Props> = (options = {}) => {\n const { getKey } = options;\n\n return useCallback<IRenderFunction<Props>>(\n (props, defaultRender) => (\n // We no longer validate the correctness of this function.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n <Proxy {...(props as Props)} fallbackComponent={defaultRender as any} key={getKey?.(props)} request={props} />\n ),\n [getKey]\n );\n };\n\n // We no longer validate the correctness of this function.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return { ...returnValue, useBuildRenderFunction } as any;\n}\n"],"mappings":";;;;;AACA,OAAO,SAAS,mBAA6B;AAiB9B,SAAR,uCACL,SAGA;AAGA,QAAM,cAAc,oCAA4D,OAAc;AAE9F,QAAM,EAAE,MAAM,IAAI;AAElB,QAAM,yBAAwD,CAACA,WAAU,CAAC,MAAM;AAC9E,UAAM,EAAE,OAAO,IAAIA;AAEnB,WAAO;AAAA,MACL,CAAC,OAAO;AAAA;AAAA;AAAA,QAGN,oCAAC,SAAO,GAAI,OAAiB,mBAAmB,eAAsB,KAAK,SAAS,KAAK,GAAG,SAAS,OAAO;AAAA;AAAA,MAE9G,CAAC,MAAM;AAAA,IACT;AAAA,EACF;AAIA,SAAO,EAAE,GAAG,aAAa,uBAAuB;AAClD;","names":["options"]}
|
|
@@ -30,7 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
|
-
createChainOfResponsibility: () =>
|
|
33
|
+
createChainOfResponsibility: () => createChainOfResponsibility_default
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(src_exports);
|
|
36
36
|
|
|
@@ -39,15 +39,13 @@ var import_react = __toESM(require("react"));
|
|
|
39
39
|
|
|
40
40
|
// src/isReactComponent.ts
|
|
41
41
|
function isConsumer(component) {
|
|
42
|
-
|
|
43
|
-
return ((_a = component == null ? void 0 : component.$$typeof) == null ? void 0 : _a.toString()) === "Symbol(react.context)";
|
|
42
|
+
return component?.$$typeof?.toString() === "Symbol(react.context)";
|
|
44
43
|
}
|
|
45
44
|
function isProvider(component) {
|
|
46
|
-
|
|
47
|
-
return ((_a = component == null ? void 0 : component.$$typeof) == null ? void 0 : _a.toString()) === "Symbol(react.provider)";
|
|
45
|
+
return component?.$$typeof?.toString() === "Symbol(react.provider)";
|
|
48
46
|
}
|
|
49
47
|
function isFragment(component) {
|
|
50
|
-
return
|
|
48
|
+
return component?.toString() === "Symbol(react.fragment)";
|
|
51
49
|
}
|
|
52
50
|
function isFunctionComponent(component) {
|
|
53
51
|
if (typeof component === "function") {
|
|
@@ -56,11 +54,10 @@ function isFunctionComponent(component) {
|
|
|
56
54
|
return isPureFunctionComponent(component);
|
|
57
55
|
}
|
|
58
56
|
function isPureFunctionComponent(component) {
|
|
59
|
-
|
|
60
|
-
return ((_a = component == null ? void 0 : component.$$typeof) == null ? void 0 : _a.toString()) === "Symbol(react.memo)" && isFunctionComponent(component.type);
|
|
57
|
+
return component?.$$typeof?.toString() === "Symbol(react.memo)" && isFunctionComponent(component.type);
|
|
61
58
|
}
|
|
62
59
|
function isComponentClass(component) {
|
|
63
|
-
return typeof component === "object" && typeof
|
|
60
|
+
return typeof component === "object" && typeof component?.["render"] === "function";
|
|
64
61
|
}
|
|
65
62
|
function isReactComponent(component) {
|
|
66
63
|
return isFunctionComponent(component) || isComponentClass(component) || isFragment(component) || isConsumer(component) || isProvider(component);
|
|
@@ -85,11 +82,11 @@ function createChainOfResponsibility(options = {}) {
|
|
|
85
82
|
get enhancer() {
|
|
86
83
|
return void 0;
|
|
87
84
|
},
|
|
88
|
-
useBuildComponentCallback(
|
|
89
|
-
if (options2
|
|
90
|
-
|
|
85
|
+
useBuildComponentCallback(_request, options2) {
|
|
86
|
+
if (!options2?.fallbackComponent) {
|
|
87
|
+
throw new Error("This component/hook cannot be used outside of its corresponding <Provider>");
|
|
91
88
|
}
|
|
92
|
-
|
|
89
|
+
return options2.fallbackComponent;
|
|
93
90
|
}
|
|
94
91
|
};
|
|
95
92
|
const context = (0, import_react.createContext)(defaultUseBuildComponentCallback);
|
|
@@ -146,10 +143,10 @@ function createChainOfResponsibility(options = {}) {
|
|
|
146
143
|
return /* @__PURE__ */ import_react.default.createElement(context.Provider, { value: contextValue }, children);
|
|
147
144
|
}
|
|
148
145
|
const useBuildComponentCallback = () => (0, import_react.useContext)(context).useBuildComponentCallback;
|
|
149
|
-
function Proxy2({
|
|
146
|
+
function Proxy2({ fallbackComponent, request, ...props }) {
|
|
150
147
|
const enhancer = useBuildComponentCallback();
|
|
151
148
|
const Component = enhancer(request, { fallbackComponent });
|
|
152
|
-
return Component ? /* @__PURE__ */ import_react.default.createElement(Component, { ...props }
|
|
149
|
+
return Component ? /* @__PURE__ */ import_react.default.createElement(Component, { ...props }) : null;
|
|
153
150
|
}
|
|
154
151
|
const asMiddleware = (MiddlewareComponent) => (init) => (next) => (request) => {
|
|
155
152
|
const RawNextComponent = next(request);
|
|
@@ -169,20 +166,22 @@ function createChainOfResponsibility(options = {}) {
|
|
|
169
166
|
MiddlewareOf.displayName = `MiddlewareOf<${MiddlewareComponent.displayName || ""}>`;
|
|
170
167
|
return (0, import_react.memo)(MiddlewareOf);
|
|
171
168
|
};
|
|
172
|
-
return {
|
|
169
|
+
return Object.freeze({
|
|
173
170
|
asMiddleware,
|
|
174
171
|
Provider: (0, import_react.memo)(ChainOfResponsibilityProvider),
|
|
175
172
|
Proxy: (0, import_react.memo)(Proxy2),
|
|
176
|
-
types: {
|
|
173
|
+
types: Object.freeze({
|
|
177
174
|
middlewareComponentProps: void 0,
|
|
178
175
|
init: void 0,
|
|
179
176
|
middleware: void 0,
|
|
180
177
|
props: void 0,
|
|
178
|
+
proxyProps: void 0,
|
|
181
179
|
request: void 0
|
|
182
|
-
},
|
|
180
|
+
}),
|
|
183
181
|
useBuildComponentCallback
|
|
184
|
-
};
|
|
182
|
+
});
|
|
185
183
|
}
|
|
184
|
+
var createChainOfResponsibility_default = createChainOfResponsibility;
|
|
186
185
|
// Annotate the CommonJS export names for ESM import in node:
|
|
187
186
|
0 && (module.exports = {
|
|
188
187
|
createChainOfResponsibility
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/createChainOfResponsibility.tsx","../src/isReactComponent.ts","../src/private/compose.ts","../src/private/applyMiddleware.ts"],"sourcesContent":["export { default as createChainOfResponsibility } from './createChainOfResponsibility.tsx';\nexport { type ComponentMiddleware } from './types.ts';\n","import React, {\n createContext,\n isValidElement,\n memo,\n useCallback,\n useContext,\n useMemo,\n type ComponentType,\n type PropsWithChildren\n} from 'react';\n\nimport isReactComponent from './isReactComponent.ts';\nimport applyMiddleware, { type Enhancer } from './private/applyMiddleware.ts';\nimport { type ComponentMiddleware } from './types.ts';\n\n// TODO: Simplify to ComponentType<Props> | undefined.\ntype ResultComponent<Props> = ComponentType<Props> | false | null | undefined;\n\ntype UseBuildComponentCallbackOptions<Props> = { fallbackComponent?: ResultComponent<Props> };\n\ntype UseBuildComponentCallback<Request, Props> = (\n request: Request,\n options?: UseBuildComponentCallbackOptions<Props>\n) => ComponentType<Props> | undefined;\n\ntype ProviderContext<Request, Props> = {\n get enhancer(): Enhancer<[Request], ResultComponent<Props>> | undefined;\n useBuildComponentCallback: UseBuildComponentCallback<Request, Props>;\n};\n\ntype ProviderProps<Request, Props, Init> = PropsWithChildren<{\n middleware: readonly ComponentMiddleware<Request, Props, Init>[];\n}> &\n (Init extends never | void ? { init?: undefined } : Init extends undefined ? { init?: Init } : { init: Init });\n\ntype ProxyProps<Request, Props extends object> = PropsWithChildren<\n Request extends never | void\n ? Props & { fallbackComponent?: ComponentType<Props>; request?: undefined }\n : Request extends undefined\n ? Props & { fallbackComponent?: ComponentType<Props>; request?: Request }\n : Props & { fallbackComponent?: ComponentType<Props>; request: Request }\n>;\n\ntype Options = {\n /**\n * Allows a middleware to pass another request object when calling its next middleware. Default is false.\n *\n * However, middleware could modify the request object before calling its next middleware. It is recommended\n * to use Object.freeze() to prevent middleware from modifying the request object.\n */\n passModifiedRequest?: boolean;\n};\n\ntype MiddlewareProps<Request, Props, Init> = Readonly<{\n init: Init;\n Next: ComponentType<Partial<Props>>;\n request: Request;\n}>;\n\ntype MiddlewareComponentProps<Request, Props, Init> = Props &\n Readonly<{ middleware: MiddlewareProps<Request, Props, Init> }>;\n\nexport default function createChainOfResponsibility<\n Request = undefined,\n Props extends object = Readonly<{ children?: never }>,\n Init = undefined\n>(\n options: Options = {}\n): {\n asMiddleware: (\n middlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init>;\n Provider: ComponentType<ProviderProps<Request, Props, Init>>;\n Proxy: ComponentType<ProxyProps<Request, Props>>;\n types: {\n init: Init;\n middleware: ComponentMiddleware<Request, Props, Init>;\n middlewareComponentProps: MiddlewareComponentProps<Request, Props, Init>;\n props: Props;\n request: Request;\n };\n useBuildComponentCallback: () => UseBuildComponentCallback<Request, Props>;\n} {\n const defaultUseBuildComponentCallback: ProviderContext<Request, Props> = {\n get enhancer() {\n return undefined;\n },\n useBuildComponentCallback(_, options) {\n if (options?.fallbackComponent) {\n return options.fallbackComponent;\n }\n\n throw new Error('This component/hook cannot be used outside of its corresponding <Provider>');\n }\n };\n\n const context = createContext<ProviderContext<Request, Props>>(defaultUseBuildComponentCallback);\n\n function ChainOfResponsibilityProvider({ children, init, middleware }: ProviderProps<Request, Props, Init>) {\n // TODO: Related to https://github.com/microsoft/TypeScript/issues/17002.\n // typescript@5.2.2 has a bug, Array.isArray() is a type predicate but only works with mutable array, not readonly array.\n // After removing \"as unknown\", `middleware` on the next line become `any[]`.\n if (!Array.isArray(middleware as unknown) || middleware.some(middleware => typeof middleware !== 'function')) {\n throw new Error('middleware prop must be an array of functions');\n }\n\n const patchedMiddleware: readonly ComponentMiddleware<Request, Props, Init>[] = Object.freeze(\n middleware\n ? middleware.map(fn => (init: Init) => {\n const enhancer = fn(init);\n\n return (next: (request: Request) => ComponentType<Props> | false | null | undefined) =>\n (originalRequest: Request) => {\n // False positive: although we did not re-assign the variable from true, it was initialized as undefined.\n // eslint-disable-next-line prefer-const\n let hasReturned: boolean;\n\n const returnValue = enhancer(nextRequest => {\n if (hasReturned) {\n throw new Error('next() cannot be called after the function had returned synchronously');\n }\n\n !options.passModifiedRequest &&\n nextRequest !== originalRequest &&\n console.warn(\n 'react-chain-of-responsibility: \"options.passModifiedRequest\" must be set to true to pass a different request object to next().'\n );\n\n return next(options.passModifiedRequest ? nextRequest : originalRequest);\n })(originalRequest);\n\n hasReturned = true;\n\n if (isValidElement(returnValue)) {\n throw new Error('middleware must not return React element directly');\n } else if (\n returnValue !== false &&\n returnValue !== null &&\n typeof returnValue !== 'undefined' &&\n !isReactComponent(returnValue)\n ) {\n throw new Error(\n 'middleware must return false, null, undefined, function component, or class component'\n );\n }\n\n return returnValue;\n };\n })\n : []\n );\n\n const { enhancer: parentEnhancer } = useContext(context);\n\n const enhancer = useMemo(\n () =>\n // We are reversing because it is easier to read:\n // - With reverse, [a, b, c] will become a(b(c(fn)))\n // - Without reverse, [a, b, c] will become c(b(a(fn)))\n applyMiddleware<[Request], ResultComponent<Props>, [Init]>(\n ...[...patchedMiddleware, ...(parentEnhancer ? [() => parentEnhancer] : [])].reverse()\n )(init as Init),\n [init, middleware, parentEnhancer]\n );\n\n const useBuildComponentCallback = useCallback<UseBuildComponentCallback<Request, Props>>(\n (request, options = {}) => enhancer(() => options.fallbackComponent)(request) || undefined,\n [enhancer]\n );\n\n const contextValue = useMemo<ProviderContext<Request, Props>>(\n () => ({ enhancer, useBuildComponentCallback }),\n [enhancer, useBuildComponentCallback]\n );\n\n return <context.Provider value={contextValue}>{children}</context.Provider>;\n }\n\n const useBuildComponentCallback = () => useContext(context).useBuildComponentCallback;\n\n function Proxy({ children, fallbackComponent, request, ...props }: ProxyProps<Request, Props>) {\n const enhancer = useBuildComponentCallback();\n const Component = enhancer(request as Request, { fallbackComponent });\n\n return Component ? <Component {...(props as Props)}>{children}</Component> : null;\n }\n\n const asMiddleware: (\n middlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init> =\n (\n MiddlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>\n ): ComponentMiddleware<Request, Props, Init> =>\n init =>\n next =>\n request => {\n const RawNextComponent = next(request);\n\n // TODO: Can we pre-build this component during init?\n const MiddlewareOf = (props: Props) => {\n const middleware = useMemo(\n () =>\n Object.freeze({\n init,\n Next: memo<Partial<Props>>(\n RawNextComponent\n ? (overridingProps: Partial<Props>) => <RawNextComponent {...props} {...overridingProps} />\n : () => null\n ),\n request\n }),\n []\n );\n\n return <MiddlewareComponent {...props} middleware={middleware} />;\n };\n\n MiddlewareOf.displayName = `MiddlewareOf<${MiddlewareComponent.displayName || ''}>`;\n\n return memo<Props>(MiddlewareOf);\n };\n\n return {\n asMiddleware,\n Provider: memo<ProviderProps<Request, Props, Init>>(ChainOfResponsibilityProvider),\n Proxy: memo<ProxyProps<Request, Props>>(Proxy),\n types: {\n middlewareComponentProps: undefined as unknown as MiddlewareComponentProps<Request, Props, Init>,\n init: undefined as unknown as Init,\n middleware: undefined as unknown as ComponentMiddleware<Request, Props, Init>,\n props: undefined as unknown as Props,\n request: undefined as unknown as Request\n },\n useBuildComponentCallback\n };\n}\n","import {\n type ComponentClass,\n type ComponentType,\n type Consumer,\n type Fragment,\n type FunctionComponent,\n type Provider\n} from 'react';\n\nfunction isConsumer(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Consumer<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.context)';\n}\n\nfunction isProvider(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Provider<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.provider)';\n}\n\nfunction isFragment(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is typeof Fragment {\n return component?.toString() === 'Symbol(react.fragment)';\n}\n\nfunction isFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n if (typeof component === 'function') {\n return true;\n }\n\n return isPureFunctionComponent(component);\n}\n\nfunction isPureFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n return component?.$$typeof?.toString() === 'Symbol(react.memo)' && isFunctionComponent(component.type);\n}\n\nfunction isComponentClass(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentClass {\n return typeof component === 'object' && typeof component?.['render'] === 'function';\n}\n\n// There are no definitive ways to check if an object is a React component or not.\n// We are checking if the object has a render function (classic component).\n// Note: \"forwardRef()\" returns plain object, not class instance.\nexport default function isReactComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentType {\n return (\n isFunctionComponent(component) ||\n isComponentClass(component) ||\n isFragment(component) ||\n isConsumer(component) ||\n isProvider(component)\n );\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function compose<P extends any[], R>(...fns: Enhancer<P, R>[]): Enhancer<P, R> {\n return (fn: Fn<P, R>): Fn<P, R> => fns.reduce((chain, fn) => fn(chain), fn);\n}\n","import compose from './compose.ts';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Middleware<P extends any[], R, S extends any[]> = (...init: S) => Enhancer<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function applyMiddleware<P extends any[], R, S extends any[]>(\n ...arrayOfMiddleware: Middleware<P, R, S>[]\n) {\n return (...init: S) => {\n const chain = arrayOfMiddleware.map(middleware => middleware(...init));\n\n return compose(...chain);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBASO;;;ACAP,SAAS,WAEP,WACgC;AAZlC;AAaE,WAAO,4CAAW,aAAX,mBAAqB,gBAAe;AAC7C;AAEA,SAAS,WAEP,WACgC;AAnBlC;AAoBE,WAAO,4CAAW,aAAX,mBAAqB,gBAAe;AAC7C;AAEA,SAAS,WAEP,WAC8B;AAC9B,UAAO,uCAAW,gBAAe;AACnC;AAEA,SAAS,oBAEP,WACgC;AAChC,MAAI,OAAO,cAAc,YAAY;AACnC,WAAO;AAAA,EACT;AAEA,SAAO,wBAAwB,SAAS;AAC1C;AAEA,SAAS,wBAEP,WACgC;AA5ClC;AA6CE,WAAO,4CAAW,aAAX,mBAAqB,gBAAe,wBAAwB,oBAAoB,UAAU,IAAI;AACvG;AAEA,SAAS,iBAEP,WAC6B;AAC7B,SAAO,OAAO,cAAc,YAAY,QAAO,uCAAY,eAAc;AAC3E;AAKe,SAAR,iBAEL,WAC4B;AAC5B,SACE,o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S,IAAe;AAAA,EAC/E;AAEA,QAAM,eAGJ,CACE,wBAEF,UACA,UACA,aAAW;AACT,UAAM,mBAAmB,KAAK,OAAO;AAGrC,UAAM,eAAe,CAAC,UAAiB;AACrC,YAAM,iBAAa;AAAA,QACjB,MACE,OAAO,OAAO;AAAA,UACZ;AAAA,UACA,UAAM;AAAA,YACJ,mBACI,CAAC,oBAAoC,6BAAAA,QAAA,cAAC,oBAAkB,GAAG,OAAQ,GAAG,iBAAiB,IACvF,MAAM;AAAA,UACZ;AAAA,UACA;AAAA,QACF,CAAC;AAAA,QACH,CAAC;AAAA,MACH;AAEA,aAAO,6BAAAA,QAAA,cAAC,uBAAqB,GAAG,OAAO,YAAwB;AAAA,IACjE;AAEA,iBAAa,cAAc,gBAAgB,oBAAoB,eAAe,EAAE;AAEhF,eAAO,mBAAY,YAAY;AAAA,EACjC;AAEF,SAAO;AAAA,IACL;AAAA,IACA,cAAU,mBAA0C,6BAA6B;AAAA,IACjF,WAAO,mBAAiCC,MAAK;AAAA,IAC7C,OAAO;AAAA,MACL,0BAA0B;AAAA,MAC1B,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,OAAO;AAAA,MACP,SAAS;AAAA,IACX;AAAA,IACA;AAAA,EACF;AACF;","names":["fn","options","middleware","init","enhancer","useBuildComponentCallback","React","Proxy"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/createChainOfResponsibility.tsx","../src/isReactComponent.ts","../src/private/compose.ts","../src/private/applyMiddleware.ts"],"sourcesContent":["export { default as createChainOfResponsibility } from './createChainOfResponsibility.tsx';\nexport { type ComponentMiddleware } from './types.ts';\n","import React, {\n createContext,\n isValidElement,\n memo,\n useCallback,\n useContext,\n useMemo,\n type ComponentType,\n type PropsWithChildren\n} from 'react';\n\nimport isReactComponent from './isReactComponent.ts';\nimport applyMiddleware, { type Enhancer } from './private/applyMiddleware.ts';\nimport { type ComponentMiddleware } from './types.ts';\n\n// TODO: Simplify to ComponentType<Props> | undefined.\ntype ResultComponent<Props> = ComponentType<Props> | false | null | undefined;\n\ntype UseBuildComponentCallbackOptions<Props> = {\n fallbackComponent?: ResultComponent<Props> | undefined;\n};\n\ninterface UseBuildComponentCallback<Request, Props> {\n (request: Request, options?: undefined | UseBuildComponentCallbackOptions<Props>): ComponentType<Props> | undefined;\n}\n\ntype ProviderContext<Request, Props> = {\n get enhancer(): Enhancer<[Request], ResultComponent<Props>> | undefined;\n useBuildComponentCallback: UseBuildComponentCallback<Request, Props>;\n};\n\ntype ProviderProps<Request, Props, Init> = PropsWithChildren<{\n middleware: readonly ComponentMiddleware<Request, Props, Init>[];\n}> &\n (Init extends never | void\n ? { readonly init?: undefined }\n : Init extends undefined | void\n ? { readonly init?: Init }\n : { readonly init: Init });\n\ntype ProxyProps<Request, Props extends object> = Props & {\n readonly fallbackComponent?: ComponentType<Props> | undefined;\n readonly request: Request;\n};\n\ntype CreateChainOfResponsibilityOptions = {\n /**\n * Allows a middleware to pass another request object when calling its next middleware. Default is false.\n *\n * However, middleware could modify the request object before calling its next middleware. It is recommended\n * to use Object.freeze() to prevent middleware from modifying the request object.\n */\n readonly passModifiedRequest?: boolean | undefined;\n};\n\ntype AsMiddlewareProps<Request, Props, Init> = {\n readonly init: Init;\n readonly Next: ComponentType<Partial<Props>>;\n readonly request: Request;\n};\n\ntype AsMiddlewareComponentProps<Request, Props, Init> = Props & {\n readonly middleware: AsMiddlewareProps<Request, Props, Init>;\n};\n\ntype ChainOfResponsibility<Request, Props extends object, Init> = {\n readonly asMiddleware: (\n middlewareComponent: ComponentType<AsMiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init>;\n readonly Provider: ComponentType<ProviderProps<Request, Props, Init>>;\n readonly Proxy: ComponentType<ProxyProps<Request, Props>>;\n readonly types: {\n readonly init: Init;\n readonly middleware: ComponentMiddleware<Request, Props, Init>;\n readonly middlewareComponentProps: AsMiddlewareComponentProps<Request, Props, Init>;\n readonly props: Props;\n readonly proxyProps: ProxyProps<Request, Props>;\n readonly request: Request;\n };\n readonly useBuildComponentCallback: () => UseBuildComponentCallback<Request, Props>;\n};\n\nfunction createChainOfResponsibility<Request = void, Props extends object = { readonly children?: never }, Init = void>(\n options: CreateChainOfResponsibilityOptions = {}\n): ChainOfResponsibility<Request, Props, Init> {\n const defaultUseBuildComponentCallback: ProviderContext<Request, Props> = {\n get enhancer() {\n return undefined;\n },\n useBuildComponentCallback(_request, options): ComponentType<Props> {\n if (!options?.fallbackComponent) {\n throw new Error('This component/hook cannot be used outside of its corresponding <Provider>');\n }\n\n return options.fallbackComponent;\n }\n };\n\n const context = createContext<ProviderContext<Request, Props>>(defaultUseBuildComponentCallback);\n\n function ChainOfResponsibilityProvider({ children, init, middleware }: ProviderProps<Request, Props, Init>) {\n // TODO: Related to https://github.com/microsoft/TypeScript/issues/17002.\n // typescript@5.2.2 has a bug, Array.isArray() is a type predicate but only works with mutable array, not readonly array.\n // After removing \"as unknown\", `middleware` on the next line become `any[]`.\n if (!Array.isArray(middleware as unknown) || middleware.some(middleware => typeof middleware !== 'function')) {\n throw new Error('middleware prop must be an array of functions');\n }\n\n const patchedMiddleware: readonly ComponentMiddleware<Request, Props, Init>[] = Object.freeze(\n middleware\n ? middleware.map(fn => (init: Init) => {\n const enhancer = fn(init);\n\n return (next: (request: Request) => ComponentType<Props> | false | null | undefined) =>\n (originalRequest: Request) => {\n // False positive: although we did not re-assign the variable from true, it was initialized as undefined.\n // eslint-disable-next-line prefer-const\n let hasReturned: boolean;\n\n const returnValue = enhancer(nextRequest => {\n if (hasReturned) {\n throw new Error('next() cannot be called after the function had returned synchronously');\n }\n\n !options.passModifiedRequest &&\n nextRequest !== originalRequest &&\n console.warn(\n 'react-chain-of-responsibility: \"options.passModifiedRequest\" must be set to true to pass a different request object to next().'\n );\n\n return next(options.passModifiedRequest ? nextRequest : originalRequest);\n })(originalRequest);\n\n hasReturned = true;\n\n if (isValidElement(returnValue)) {\n throw new Error('middleware must not return React element directly');\n } else if (\n returnValue !== false &&\n returnValue !== null &&\n typeof returnValue !== 'undefined' &&\n !isReactComponent(returnValue)\n ) {\n throw new Error(\n 'middleware must return false, null, undefined, function component, or class component'\n );\n }\n\n return returnValue;\n };\n })\n : []\n );\n\n const { enhancer: parentEnhancer } = useContext(context);\n\n const enhancer = useMemo(\n () =>\n // We are reversing because it is easier to read:\n // - With reverse, [a, b, c] will become a(b(c(fn)))\n // - Without reverse, [a, b, c] will become c(b(a(fn)))\n applyMiddleware<[Request], ResultComponent<Props>, [Init]>(\n ...[...patchedMiddleware, ...(parentEnhancer ? [() => parentEnhancer] : [])].reverse()\n )(init as Init),\n [init, middleware, parentEnhancer]\n );\n\n const useBuildComponentCallback = useCallback<UseBuildComponentCallback<Request, Props>>(\n (request, options = {}) => enhancer(() => options.fallbackComponent)(request) || undefined,\n [enhancer]\n );\n\n const contextValue = useMemo<ProviderContext<Request, Props>>(\n () => ({ enhancer, useBuildComponentCallback }),\n [enhancer, useBuildComponentCallback]\n );\n\n return <context.Provider value={contextValue}>{children}</context.Provider>;\n }\n\n const useBuildComponentCallback = () => useContext(context).useBuildComponentCallback;\n\n function Proxy({ fallbackComponent, request, ...props }: ProxyProps<Request, Props>) {\n const enhancer = useBuildComponentCallback();\n\n const Component = enhancer(request as Request, { fallbackComponent });\n\n return Component ? <Component {...(props as Props)} /> : null;\n }\n\n const asMiddleware: (\n middlewareComponent: ComponentType<AsMiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init> =\n (\n MiddlewareComponent: ComponentType<AsMiddlewareComponentProps<Request, Props, Init>>\n ): ComponentMiddleware<Request, Props, Init> =>\n init =>\n next =>\n request => {\n const RawNextComponent = next(request);\n\n // TODO: Can we pre-build this component during init?\n const MiddlewareOf = (props: Props) => {\n const middleware = useMemo(\n () =>\n Object.freeze({\n init,\n Next: memo<Partial<Props>>(\n RawNextComponent\n ? (overridingProps: Partial<Props>) => <RawNextComponent {...props} {...overridingProps} />\n : () => null\n ),\n request\n }),\n []\n );\n\n return <MiddlewareComponent {...props} middleware={middleware} />;\n };\n\n MiddlewareOf.displayName = `MiddlewareOf<${MiddlewareComponent.displayName || ''}>`;\n\n return memo<Props>(MiddlewareOf);\n };\n\n return Object.freeze({\n asMiddleware,\n Provider: memo<ProviderProps<Request, Props, Init>>(ChainOfResponsibilityProvider),\n Proxy: memo<ProxyProps<Request, Props>>(Proxy),\n types: Object.freeze({\n middlewareComponentProps: undefined as unknown as AsMiddlewareComponentProps<Request, Props, Init>,\n init: undefined as unknown as Init,\n middleware: undefined as unknown as ComponentMiddleware<Request, Props, Init>,\n props: undefined as unknown as Props,\n proxyProps: undefined as unknown as ProxyProps<Request, Props>,\n request: undefined as unknown as Request\n }),\n useBuildComponentCallback\n });\n}\n\nexport default createChainOfResponsibility;\nexport {\n type ChainOfResponsibility,\n type CreateChainOfResponsibilityOptions,\n type ProxyProps,\n type UseBuildComponentCallback\n};\n","import {\n type ComponentClass,\n type ComponentType,\n type Consumer,\n type Fragment,\n type FunctionComponent,\n type Provider\n} from 'react';\n\nfunction isConsumer(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Consumer<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.context)';\n}\n\nfunction isProvider(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Provider<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.provider)';\n}\n\nfunction isFragment(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is typeof Fragment {\n return component?.toString() === 'Symbol(react.fragment)';\n}\n\nfunction isFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n if (typeof component === 'function') {\n return true;\n }\n\n return isPureFunctionComponent(component);\n}\n\nfunction isPureFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n return component?.$$typeof?.toString() === 'Symbol(react.memo)' && isFunctionComponent(component.type);\n}\n\nfunction isComponentClass(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentClass {\n return typeof component === 'object' && typeof component?.['render'] === 'function';\n}\n\n// There are no definitive ways to check if an object is a React component or not.\n// We are checking if the object has a render function (classic component).\n// Note: \"forwardRef()\" returns plain object, not class instance.\nexport default function isReactComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentType {\n return (\n isFunctionComponent(component) ||\n isComponentClass(component) ||\n isFragment(component) ||\n isConsumer(component) ||\n isProvider(component)\n );\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function compose<P extends any[], R>(...fns: Enhancer<P, R>[]): Enhancer<P, R> {\n return (fn: Fn<P, R>): Fn<P, R> => fns.reduce((chain, fn) => fn(chain), fn);\n}\n","import compose from './compose.ts';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Middleware<P extends any[], R, S extends any[]> = (...init: S) => Enhancer<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function applyMiddleware<P extends any[], R, S extends any[]>(\n ...arrayOfMiddleware: Middleware<P, R, S>[]\n) {\n return (...init: S) => {\n const chain = arrayOfMiddleware.map(middleware => middleware(...init));\n\n return compose(...chain);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBASO;;;ACAP,SAAS,WAEP,WACgC;AAChC,SA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MACP,YAAY;AAAA,MACZ,SAAS;AAAA,IACX,CAAC;AAAA,IACD;AAAA,EACF,CAAC;AACH;AAEA,IAAO,sCAAQ;","names":["fn","options","middleware","init","enhancer","useBuildComponentCallback","React","Proxy"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "./chunk-
|
|
2
|
+
createChainOfResponsibility_default
|
|
3
|
+
} from "./chunk-CLWNNXXB.mjs";
|
|
4
4
|
export {
|
|
5
|
-
createChainOfResponsibility
|
|
5
|
+
createChainOfResponsibility_default as createChainOfResponsibility
|
|
6
6
|
};
|
|
7
7
|
//# sourceMappingURL=react-chain-of-responsibility.mjs.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-chain-of-responsibility",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1-main.932fc50",
|
|
4
4
|
"description": "Using chain of responsibility design pattern for compositing and customizing React component.",
|
|
5
5
|
"files": [
|
|
6
6
|
"./dist/"
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"repository": {
|
|
47
47
|
"type": "git",
|
|
48
|
-
"url": "
|
|
48
|
+
"url": "https://github.com/compulim/react-chain-of-responsibility.git"
|
|
49
49
|
},
|
|
50
50
|
"keywords": [
|
|
51
51
|
"react",
|
|
@@ -106,7 +106,7 @@
|
|
|
106
106
|
"@babel/preset-react": "^7.27.1",
|
|
107
107
|
"@babel/preset-typescript": "^7.27.1",
|
|
108
108
|
"@fluentui/react": "^8.123.0",
|
|
109
|
-
"@testduet/given-when-then": "^0.1.0-main.
|
|
109
|
+
"@testduet/given-when-then": "^0.1.0-main.334801c",
|
|
110
110
|
"@testing-library/dom": "^10.4.0",
|
|
111
111
|
"@testing-library/react": "^16.3.0",
|
|
112
112
|
"@tsconfig/recommended": "^1.0.10",
|
|
@@ -115,7 +115,7 @@
|
|
|
115
115
|
"@types/node": "^24.0.3",
|
|
116
116
|
"@types/react": "^18.3.23",
|
|
117
117
|
"esbuild": "^0.25.5",
|
|
118
|
-
"escape-string-regexp": "^
|
|
118
|
+
"escape-string-regexp": "^4.0.0",
|
|
119
119
|
"jest": "^30.0.2",
|
|
120
120
|
"jest-environment-jsdom": "^30.0.2",
|
|
121
121
|
"prettier": "^3.5.3",
|
|
@@ -125,12 +125,13 @@
|
|
|
125
125
|
"react-test-renderer": "^18.3.1",
|
|
126
126
|
"react-wrap-with": "^0.1.0",
|
|
127
127
|
"tsup": "^8.5.0",
|
|
128
|
+
"type-fest": "^4.41.0",
|
|
128
129
|
"typescript": "^5.8.3"
|
|
129
130
|
},
|
|
130
131
|
"peerDependencies": {
|
|
131
132
|
"react": ">=16.8.0"
|
|
132
133
|
},
|
|
133
134
|
"dependencies": {
|
|
134
|
-
"react-chain-of-responsibility": "^0.3.
|
|
135
|
+
"react-chain-of-responsibility": "^0.3.1-main.932fc50"
|
|
135
136
|
}
|
|
136
137
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/createChainOfResponsibility.tsx","../src/isReactComponent.ts","../src/private/compose.ts","../src/private/applyMiddleware.ts"],"sourcesContent":["import React, {\n createContext,\n isValidElement,\n memo,\n useCallback,\n useContext,\n useMemo,\n type ComponentType,\n type PropsWithChildren\n} from 'react';\n\nimport isReactComponent from './isReactComponent.ts';\nimport applyMiddleware, { type Enhancer } from './private/applyMiddleware.ts';\nimport { type ComponentMiddleware } from './types.ts';\n\n// TODO: Simplify to ComponentType<Props> | undefined.\ntype ResultComponent<Props> = ComponentType<Props> | false | null | undefined;\n\ntype UseBuildComponentCallbackOptions<Props> = { fallbackComponent?: ResultComponent<Props> };\n\ntype UseBuildComponentCallback<Request, Props> = (\n request: Request,\n options?: UseBuildComponentCallbackOptions<Props>\n) => ComponentType<Props> | undefined;\n\ntype ProviderContext<Request, Props> = {\n get enhancer(): Enhancer<[Request], ResultComponent<Props>> | undefined;\n useBuildComponentCallback: UseBuildComponentCallback<Request, Props>;\n};\n\ntype ProviderProps<Request, Props, Init> = PropsWithChildren<{\n middleware: readonly ComponentMiddleware<Request, Props, Init>[];\n}> &\n (Init extends never | void ? { init?: undefined } : Init extends undefined ? { init?: Init } : { init: Init });\n\ntype ProxyProps<Request, Props extends object> = PropsWithChildren<\n Request extends never | void\n ? Props & { fallbackComponent?: ComponentType<Props>; request?: undefined }\n : Request extends undefined\n ? Props & { fallbackComponent?: ComponentType<Props>; request?: Request }\n : Props & { fallbackComponent?: ComponentType<Props>; request: Request }\n>;\n\ntype Options = {\n /**\n * Allows a middleware to pass another request object when calling its next middleware. Default is false.\n *\n * However, middleware could modify the request object before calling its next middleware. It is recommended\n * to use Object.freeze() to prevent middleware from modifying the request object.\n */\n passModifiedRequest?: boolean;\n};\n\ntype MiddlewareProps<Request, Props, Init> = Readonly<{\n init: Init;\n Next: ComponentType<Partial<Props>>;\n request: Request;\n}>;\n\ntype MiddlewareComponentProps<Request, Props, Init> = Props &\n Readonly<{ middleware: MiddlewareProps<Request, Props, Init> }>;\n\nexport default function createChainOfResponsibility<\n Request = undefined,\n Props extends object = Readonly<{ children?: never }>,\n Init = undefined\n>(\n options: Options = {}\n): {\n asMiddleware: (\n middlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init>;\n Provider: ComponentType<ProviderProps<Request, Props, Init>>;\n Proxy: ComponentType<ProxyProps<Request, Props>>;\n types: {\n init: Init;\n middleware: ComponentMiddleware<Request, Props, Init>;\n middlewareComponentProps: MiddlewareComponentProps<Request, Props, Init>;\n props: Props;\n request: Request;\n };\n useBuildComponentCallback: () => UseBuildComponentCallback<Request, Props>;\n} {\n const defaultUseBuildComponentCallback: ProviderContext<Request, Props> = {\n get enhancer() {\n return undefined;\n },\n useBuildComponentCallback(_, options) {\n if (options?.fallbackComponent) {\n return options.fallbackComponent;\n }\n\n throw new Error('This component/hook cannot be used outside of its corresponding <Provider>');\n }\n };\n\n const context = createContext<ProviderContext<Request, Props>>(defaultUseBuildComponentCallback);\n\n function ChainOfResponsibilityProvider({ children, init, middleware }: ProviderProps<Request, Props, Init>) {\n // TODO: Related to https://github.com/microsoft/TypeScript/issues/17002.\n // typescript@5.2.2 has a bug, Array.isArray() is a type predicate but only works with mutable array, not readonly array.\n // After removing \"as unknown\", `middleware` on the next line become `any[]`.\n if (!Array.isArray(middleware as unknown) || middleware.some(middleware => typeof middleware !== 'function')) {\n throw new Error('middleware prop must be an array of functions');\n }\n\n const patchedMiddleware: readonly ComponentMiddleware<Request, Props, Init>[] = Object.freeze(\n middleware\n ? middleware.map(fn => (init: Init) => {\n const enhancer = fn(init);\n\n return (next: (request: Request) => ComponentType<Props> | false | null | undefined) =>\n (originalRequest: Request) => {\n // False positive: although we did not re-assign the variable from true, it was initialized as undefined.\n // eslint-disable-next-line prefer-const\n let hasReturned: boolean;\n\n const returnValue = enhancer(nextRequest => {\n if (hasReturned) {\n throw new Error('next() cannot be called after the function had returned synchronously');\n }\n\n !options.passModifiedRequest &&\n nextRequest !== originalRequest &&\n console.warn(\n 'react-chain-of-responsibility: \"options.passModifiedRequest\" must be set to true to pass a different request object to next().'\n );\n\n return next(options.passModifiedRequest ? nextRequest : originalRequest);\n })(originalRequest);\n\n hasReturned = true;\n\n if (isValidElement(returnValue)) {\n throw new Error('middleware must not return React element directly');\n } else if (\n returnValue !== false &&\n returnValue !== null &&\n typeof returnValue !== 'undefined' &&\n !isReactComponent(returnValue)\n ) {\n throw new Error(\n 'middleware must return false, null, undefined, function component, or class component'\n );\n }\n\n return returnValue;\n };\n })\n : []\n );\n\n const { enhancer: parentEnhancer } = useContext(context);\n\n const enhancer = useMemo(\n () =>\n // We are reversing because it is easier to read:\n // - With reverse, [a, b, c] will become a(b(c(fn)))\n // - Without reverse, [a, b, c] will become c(b(a(fn)))\n applyMiddleware<[Request], ResultComponent<Props>, [Init]>(\n ...[...patchedMiddleware, ...(parentEnhancer ? [() => parentEnhancer] : [])].reverse()\n )(init as Init),\n [init, middleware, parentEnhancer]\n );\n\n const useBuildComponentCallback = useCallback<UseBuildComponentCallback<Request, Props>>(\n (request, options = {}) => enhancer(() => options.fallbackComponent)(request) || undefined,\n [enhancer]\n );\n\n const contextValue = useMemo<ProviderContext<Request, Props>>(\n () => ({ enhancer, useBuildComponentCallback }),\n [enhancer, useBuildComponentCallback]\n );\n\n return <context.Provider value={contextValue}>{children}</context.Provider>;\n }\n\n const useBuildComponentCallback = () => useContext(context).useBuildComponentCallback;\n\n function Proxy({ children, fallbackComponent, request, ...props }: ProxyProps<Request, Props>) {\n const enhancer = useBuildComponentCallback();\n const Component = enhancer(request as Request, { fallbackComponent });\n\n return Component ? <Component {...(props as Props)}>{children}</Component> : null;\n }\n\n const asMiddleware: (\n middlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>\n ) => ComponentMiddleware<Request, Props, Init> =\n (\n MiddlewareComponent: ComponentType<MiddlewareComponentProps<Request, Props, Init>>\n ): ComponentMiddleware<Request, Props, Init> =>\n init =>\n next =>\n request => {\n const RawNextComponent = next(request);\n\n // TODO: Can we pre-build this component during init?\n const MiddlewareOf = (props: Props) => {\n const middleware = useMemo(\n () =>\n Object.freeze({\n init,\n Next: memo<Partial<Props>>(\n RawNextComponent\n ? (overridingProps: Partial<Props>) => <RawNextComponent {...props} {...overridingProps} />\n : () => null\n ),\n request\n }),\n []\n );\n\n return <MiddlewareComponent {...props} middleware={middleware} />;\n };\n\n MiddlewareOf.displayName = `MiddlewareOf<${MiddlewareComponent.displayName || ''}>`;\n\n return memo<Props>(MiddlewareOf);\n };\n\n return {\n asMiddleware,\n Provider: memo<ProviderProps<Request, Props, Init>>(ChainOfResponsibilityProvider),\n Proxy: memo<ProxyProps<Request, Props>>(Proxy),\n types: {\n middlewareComponentProps: undefined as unknown as MiddlewareComponentProps<Request, Props, Init>,\n init: undefined as unknown as Init,\n middleware: undefined as unknown as ComponentMiddleware<Request, Props, Init>,\n props: undefined as unknown as Props,\n request: undefined as unknown as Request\n },\n useBuildComponentCallback\n };\n}\n","import {\n type ComponentClass,\n type ComponentType,\n type Consumer,\n type Fragment,\n type FunctionComponent,\n type Provider\n} from 'react';\n\nfunction isConsumer(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Consumer<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.context)';\n}\n\nfunction isProvider(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is Provider<unknown> {\n return component?.$$typeof?.toString() === 'Symbol(react.provider)';\n}\n\nfunction isFragment(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is typeof Fragment {\n return component?.toString() === 'Symbol(react.fragment)';\n}\n\nfunction isFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n if (typeof component === 'function') {\n return true;\n }\n\n return isPureFunctionComponent(component);\n}\n\nfunction isPureFunctionComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is FunctionComponent {\n return component?.$$typeof?.toString() === 'Symbol(react.memo)' && isFunctionComponent(component.type);\n}\n\nfunction isComponentClass(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentClass {\n return typeof component === 'object' && typeof component?.['render'] === 'function';\n}\n\n// There are no definitive ways to check if an object is a React component or not.\n// We are checking if the object has a render function (classic component).\n// Note: \"forwardRef()\" returns plain object, not class instance.\nexport default function isReactComponent(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n component: any\n): component is ComponentType {\n return (\n isFunctionComponent(component) ||\n isComponentClass(component) ||\n isFragment(component) ||\n isConsumer(component) ||\n isProvider(component)\n );\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function compose<P extends any[], R>(...fns: Enhancer<P, R>[]): Enhancer<P, R> {\n return (fn: Fn<P, R>): Fn<P, R> => fns.reduce((chain, fn) => fn(chain), fn);\n}\n","import compose from './compose.ts';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype Fn<P extends any[], R> = (...args: P) => R;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Enhancer<P extends any[], R> = (next: Fn<P, R>) => Fn<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Middleware<P extends any[], R, S extends any[]> = (...init: S) => Enhancer<P, R>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function applyMiddleware<P extends any[], R, S extends any[]>(\n ...arrayOfMiddleware: Middleware<P, R, S>[]\n) {\n return (...init: S) => {\n const chain = arrayOfMiddleware.map(middleware => middleware(...init));\n\n return compose(...chain);\n };\n}\n"],"mappings":";AAAA,OAAO;AAAA,EACL;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAGK;;;ACAP,SAAS,WAEP,WACgC;AAZlC;AAaE,WAAO,4CAAW,aAAX,mBAAqB,gBAAe;AAC7C;AAEA,SAAS,WAEP,WACgC;AAnBlC;AAoBE,WAAO,4CAAW,aAAX,mBAAqB,gBAAe;AAC7C;AAEA,SAAS,WAEP,WAC8B;AAC9B,UAAO,uCAAW,gBAAe;AACnC;AAEA,SAAS,oBAEP,WACgC;AAChC,MAAI,OAAO,cAAc,YAAY;AACnC,WAAO;AAAA,EACT;AAEA,SAAO,wBAAwB,SAAS;AAC1C;AAEA,SAAS,wBAEP,WACgC;AA5ClC;AA6CE,WAAO,4CAAW,aAAX,mBAAqB,gBAAe,wBAAwB,oBAAoB,UAAU,IAAI;AACvG;AAEA,SAAS,iBAEP,WAC6B;AAC7B,SAAO,OAAO,cAAc,YAAY,QAAO,uCAAY,eAAc;AAC3E;AAKe,SAAR,i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