react-chain-of-responsibility 0.4.0-main.f9aeacf → 0.4.0-main.feea005
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.
|
@@ -34,12 +34,12 @@ __export(index_preview_exports, {
|
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(index_preview_exports);
|
|
36
36
|
|
|
37
|
-
// src/createChainOfResponsibilityAsRenderCallback.tsx
|
|
37
|
+
// src/preview/createChainOfResponsibilityAsRenderCallback.tsx
|
|
38
38
|
var import_handler_chain = require("handler-chain");
|
|
39
39
|
var import_react2 = __toESM(require("react"));
|
|
40
40
|
var import_valibot = require("valibot");
|
|
41
41
|
|
|
42
|
-
// src/private/arePropsEqual.ts
|
|
42
|
+
// src/preview/private/arePropsEqual.ts
|
|
43
43
|
function arePropsEqual(x, y) {
|
|
44
44
|
if (Object.is(x, y)) {
|
|
45
45
|
return true;
|
|
@@ -58,7 +58,7 @@ function arePropsEqual(x, y) {
|
|
|
58
58
|
return true;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
// src/private/useMemoValueWithEquality.ts
|
|
61
|
+
// src/preview/private/useMemoValueWithEquality.ts
|
|
62
62
|
var import_react = require("react");
|
|
63
63
|
var NOT_INITIALIZED = Symbol();
|
|
64
64
|
function useMemoValueWithEquality(factory, equalityFn) {
|
|
@@ -71,7 +71,7 @@ function useMemoValueWithEquality(factory, equalityFn) {
|
|
|
71
71
|
return current;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
// src/createChainOfResponsibilityAsRenderCallback.tsx
|
|
74
|
+
// src/preview/createChainOfResponsibilityAsRenderCallback.tsx
|
|
75
75
|
var DO_NOT_CREATE_THIS_OBJECT_YOURSELF = Symbol();
|
|
76
76
|
var componentHandlerResultSchema = (0, import_valibot.custom)(
|
|
77
77
|
(value) => (0, import_valibot.safeParse)((0, import_valibot.object)({ render: (0, import_valibot.function_)() }), value).success && !!value && typeof value === "object" && DO_NOT_CREATE_THIS_OBJECT_YOURSELF in value,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.preview.ts","../src/createChainOfResponsibilityAsRenderCallback.tsx","../src/private/arePropsEqual.ts","../src/private/useMemoValueWithEquality.ts"],"sourcesContent":["export {\n default as createChainOfResponsibility,\n type ChainOfResponsibility,\n type ComponentEnhancer,\n type ComponentHandler,\n type ComponentHandlerResult,\n type ComponentMiddleware,\n type ComponentRenderer,\n type CreateChainOfResponsibilityOptions,\n type InferenceHelper,\n type InferInit,\n type InferMiddleware,\n type InferProps,\n type InferProviderProps,\n type InferProxyProps,\n type InferRequest,\n type ProviderProps,\n type ProxyProps,\n type ReactComponentHandlerResult,\n type UseBuildRenderCallback,\n type UseBuildRenderCallbackOptions\n} from './createChainOfResponsibilityAsRenderCallback.tsx';\n","import { applyMiddleware } from 'handler-chain';\nimport React, {\n createContext,\n Fragment,\n memo,\n useCallback,\n useContext,\n useMemo,\n type ComponentType,\n type PropsWithChildren,\n type ReactNode\n} from 'react';\nimport { custom, function_, object, parse, safeParse } from 'valibot';\n\nimport arePropsEqual from './private/arePropsEqual.ts';\nimport useMemoValueWithEquality from './private/useMemoValueWithEquality.ts';\n\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.\ndeclare global {\n interface ArrayConstructor {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n isArray(arg: any): arg is readonly any[];\n }\n}\n\ntype BaseProps = object;\n\ntype CreateChainOfResponsibilityOptions = {\n /**\n * Allows one component to pass different set of props to its downstream component. Default is false.\n *\n * It is recommended to keep this settings as default to prevent newly added component from unexpectedly changing behavior of downstream components.\n */\n readonly allowOverrideProps?: boolean | undefined;\n\n /**\n * Allows a middleware to pass another request object when calling its next middleware. Default is false.\n *\n * It is recommended to keep this settings as default ot prevent newly added middleware from unexpectedly changing behavior of downstream middleware.\n *\n * To prevent upstream middleware from modifying the request, the request object should be set to be immutable through `Object.freeze`.\n */\n readonly passModifiedRequest?: boolean | undefined;\n};\n\ntype ChainOfResponsibility<Request, Props extends BaseProps, Init> = {\n readonly Provider: ComponentType<ProviderProps<Request, Props, Init>> & InferenceHelper<Request, Props, Init>;\n readonly Proxy: ComponentType<ProxyProps<Request, Props>>;\n readonly reactComponent: ReactComponentHandlerResult<Props>;\n readonly useBuildRenderCallback: () => UseBuildRenderCallback<Request, Props>;\n};\n\n// TODO: Maybe this one should be local.\n// Verify that reactComponent() from an instance of CoR should throw error when used in another instance of CoR.\nconst DO_NOT_CREATE_THIS_OBJECT_YOURSELF = Symbol();\n\ntype ComponentRenderer<Props> = (props: Props) => ReactNode;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst componentHandlerResultSchema = custom<ComponentHandlerResult<any>>(\n value =>\n safeParse(object({ render: function_() }), value).success &&\n !!value &&\n typeof value === 'object' &&\n DO_NOT_CREATE_THIS_OBJECT_YOURSELF in value,\n 'react-chain-of-responsibility: middleware must return value constructed by reactComponent()'\n);\n\ninterface ComponentHandlerResult<Props extends BaseProps> {\n readonly [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined;\n readonly render: (overridingProps?: Partial<Props> | undefined) => ReactNode;\n}\n\ntype ComponentHandler<Request, Props extends BaseProps> = (\n request: Request\n) => ComponentHandlerResult<Props> | undefined;\n\ntype ComponentEnhancer<Request, Props extends BaseProps> = (\n next: ComponentHandler<Request, Props>\n) => ComponentHandler<Request, Props>;\n\ntype ComponentMiddleware<Request, Props extends BaseProps, Init = undefined> = (\n init: Init\n) => ComponentEnhancer<Request, Props>;\n\ntype ReactComponentHandlerResult<Props extends object> = <P extends Props>(\n component: ComponentType<P>,\n bindProps?:\n | (Partial<Props> & Omit<P, keyof Props>)\n | ((props: Props) => Partial<Props> & Omit<P, keyof Props>)\n | undefined\n) => ComponentHandlerResult<Props>;\n\ntype UseBuildRenderCallbackOptions<Props> = {\n readonly fallbackComponent?: ComponentType<Props> | undefined;\n};\n\ninterface UseBuildRenderCallback<Request, Props extends BaseProps> {\n (request: Request, options?: undefined | UseBuildRenderCallbackOptions<Props>): ComponentRenderer<Props> | undefined;\n}\n\ntype BuildContextType<Request, Props extends BaseProps> = {\n readonly enhancer: ComponentEnhancer<Request, Props>;\n};\n\ntype RenderContextType<Props> = {\n readonly renderCallbackProps: Props;\n};\n\ntype ProviderProps<Request, Props extends BaseProps, Init> = PropsWithChildren<{\n readonly 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 BaseProps> = Props & {\n readonly fallbackComponent?: ComponentType<Props> | undefined;\n readonly request: Request;\n};\n\ntype InferenceHelper<Request, Props extends BaseProps, Init> = {\n readonly '~types': {\n readonly init: Init;\n readonly middleware: ComponentMiddleware<Request, Props, Init>;\n readonly props: Props;\n readonly proxyProps: ProxyProps<Request, Props>;\n readonly providerProps: ProviderProps<Request, Props, Init>;\n readonly request: Request;\n };\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferInit<T extends InferenceHelper<any, any, any>> = T['~types']['init'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferMiddleware<T extends InferenceHelper<any, any, any>> = T['~types']['middleware'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProps<T extends InferenceHelper<any, any, any>> = T['~types']['props'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProxyProps<T extends InferenceHelper<any, any, any>> = T['~types']['proxyProps'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProviderProps<T extends InferenceHelper<any, any, any>> = T['~types']['providerProps'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferRequest<T extends InferenceHelper<any, any, any>> = T['~types']['request'];\n\nfunction createChainOfResponsibility<\n Request = void,\n Props extends BaseProps = { readonly children?: never },\n Init = void\n>(options: CreateChainOfResponsibilityOptions = {}): ChainOfResponsibility<Request, Props, Init> {\n // Freeze options to prevent accidental change.\n options = Object.freeze({ ...options });\n\n const BuildContext = createContext<BuildContextType<Request, Props>>(\n Object.freeze({ enhancer: next => request => next(request) })\n );\n\n const RenderContext = createContext<RenderContextType<Props>>(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new Proxy({} as any, {\n get() {\n // The following is assertion, there is no way to hit this line.\n /* istanbul ignore next */\n throw new Error(\n 'react-chain-of-responsibility: this hook cannot be used outside of <Proxy> and useBuildRenderCallback()'\n );\n }\n })\n );\n\n function reactComponent<P extends Props>(\n component: ComponentType<P>,\n bindProps?:\n | (Partial<Props> & Omit<P, keyof Props>)\n | ((props: Props) => Partial<Props> & Omit<P, keyof Props>)\n | undefined\n ): ComponentHandlerResult<Props> {\n return Object.freeze({\n [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined,\n render: (overridingProps?: Partial<Props> | undefined) => (\n <ComponentWithProps\n bindProps={bindProps}\n component={component as ComponentType<Props>}\n overridingProps={overridingProps}\n />\n )\n });\n }\n\n const ComponentWithProps = memo(function ComponentWithProps({\n bindProps,\n component: Component,\n overridingProps\n }: {\n readonly bindProps?: Partial<Props> | ((props: Props) => Partial<Props>) | undefined;\n readonly component: ComponentType<Props>;\n readonly overridingProps?: Partial<Props> | undefined;\n }) {\n const { allowOverrideProps } = options;\n const { renderCallbackProps } = useContext(RenderContext);\n\n if (overridingProps && !arePropsEqual(overridingProps, renderCallbackProps) && !allowOverrideProps) {\n console.warn('react-chain-of-responsibility: \"allowOverrideProps\" must be set to true to override props');\n }\n\n const props = Object.freeze(\n allowOverrideProps ? { ...renderCallbackProps, ...overridingProps } : { ...renderCallbackProps }\n );\n\n return <Component {...props} {...(typeof bindProps === 'function' ? bindProps(props) : bindProps)} />;\n });\n\n const useBuildRenderCallback: () => UseBuildRenderCallback<Request, Props> = () => {\n const { enhancer } = useContext(BuildContext);\n\n return useCallback(\n (request, buildOptions = {}) => {\n const result =\n // Put the \"fallbackComponent\" as the last one in the chain.\n enhancer(() => {\n const { fallbackComponent } = buildOptions;\n\n if (!fallbackComponent) {\n console.warn(\n 'react-chain-of-responsibility: the request has fall through all middleware, set \"fallbackComponent\" as a catchall',\n request\n );\n\n // For clarity, we are returning `undefined` instead of `() => undefined`.\n return;\n }\n\n return Object.freeze({\n // Convert fallback component as renderer.\n [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined,\n render: () => (\n // Currently, there are no ways to set `bindProps` to `fallbackComponent`.\n // `fallbackComponent` do not need `overridingProps` because it is the last one in the chain, it would not have the next() function.\n <ComponentWithProps component={fallbackComponent} />\n )\n });\n })(request);\n\n return (\n result &&\n ((props: Props) => {\n const renderCallbackProps = useMemoValueWithEquality<Props>(() => props, arePropsEqual);\n\n const context = useMemo<RenderContextType<Props>>(\n () => Object.freeze({ renderCallbackProps }),\n [renderCallbackProps]\n );\n\n return <RenderContext.Provider value={context}>{result.render()}</RenderContext.Provider>;\n })\n );\n },\n [enhancer]\n );\n };\n\n function ChainOfResponsibilityProvider({ children, init, middleware }: ProviderProps<Request, Props, Init>) {\n if (!Array.isArray(middleware) || middleware.some(middleware => typeof middleware !== 'function')) {\n throw new Error('react-chain-of-responsibility: \"middleware\" prop must be an array of functions');\n }\n\n // Remap the middleware, so all inputs/outputs are validated.\n const fortifiedMiddleware = useMemo(\n () =>\n Object.freeze(\n middleware.map<ComponentMiddleware<Request, Props, Init>>(fn => (init: Init) => {\n const enhancer = fn(init);\n\n return next => originalRequest => {\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(\n 'react-chain-of-responsibility: next() cannot be called after the function had returned synchronously'\n );\n }\n\n // We do not allow passing void/undefined to next() because it would be confusing whether to keep the original request or pass an undefined.\n !options.passModifiedRequest &&\n !Object.is(nextRequest, originalRequest) &&\n console.warn(\n 'react-chain-of-responsibility: next() must be called with the original request, otherwise, set \"options.passModifiedRequest\" to true to pass a different request object downstream'\n );\n\n return next(options.passModifiedRequest ? nextRequest : originalRequest);\n })(originalRequest);\n\n hasReturned = true;\n\n // Make sure the return value is built using our helper function for forward-compatibility reason.\n return returnValue && parse(componentHandlerResultSchema, returnValue);\n };\n })\n ),\n [middleware]\n );\n\n const { enhancer: parentEnhancer } = useContext(BuildContext);\n\n const enhancer = useMemo<ComponentEnhancer<Request, Props>>(\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<ComponentHandlerResult<Props> | undefined, Request, Init>(\n ...[...fortifiedMiddleware, ...[() => parentEnhancer]]\n )(init as Init),\n [init, fortifiedMiddleware, parentEnhancer]\n );\n\n const contextValue = useMemo<BuildContextType<Request, Props>>(() => Object.freeze({ enhancer }), [enhancer]);\n\n return <BuildContext.Provider value={contextValue}>{children}</BuildContext.Provider>;\n }\n\n function ChainOfResponsibilityProxy({ fallbackComponent, request, ...props }: ProxyProps<Request, Props>) {\n const result = useBuildRenderCallback()(request, { fallbackComponent })?.(props as Props);\n\n return result ? <Fragment>{result}</Fragment> : null;\n }\n\n const MemoizedChainOfResponsibilityProvider =\n memo<ProviderProps<Request, Props, Init>>(ChainOfResponsibilityProvider);\n\n return Object.freeze({\n Provider: MemoizedChainOfResponsibilityProvider as typeof MemoizedChainOfResponsibilityProvider &\n InferenceHelper<Request, Props, Init>,\n Proxy: memo<ProxyProps<Request, Props>>(ChainOfResponsibilityProxy),\n reactComponent,\n useBuildRenderCallback\n\n // TODO: Consider adding back `asMiddleware`.\n });\n}\n\nexport default createChainOfResponsibility;\nexport {\n type ChainOfResponsibility,\n type ComponentEnhancer,\n type ComponentHandler,\n type ComponentHandlerResult,\n type ComponentMiddleware,\n type ComponentRenderer,\n type CreateChainOfResponsibilityOptions,\n type InferenceHelper,\n type InferInit,\n type InferMiddleware,\n type InferProps,\n type InferProviderProps,\n type InferProxyProps,\n type InferRequest,\n type ProviderProps,\n type ProxyProps,\n type ReactComponentHandlerResult,\n type UseBuildRenderCallback,\n type UseBuildRenderCallbackOptions\n};\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function arePropsEqual<T extends Record<string, any>>(x: T, y: T): boolean {\n if (Object.is(x, y)) {\n return true;\n }\n\n const mapOfX = new Map(Object.entries(x));\n const mapOfY = new Map(Object.entries(y));\n\n if (mapOfX.size !== mapOfY.size) {\n return false;\n }\n\n const keys = new Set([...mapOfX.keys(), ...mapOfY.keys()]);\n\n for (const key of keys) {\n if (!Object.is(mapOfX.get(key), mapOfY.get(key))) {\n return false;\n }\n }\n\n return true;\n}\n","import { useEffect, useRef } from 'react';\n\nconst NOT_INITIALIZED = Symbol();\n\nexport default function useMemoValueWithEquality<T>(factory: () => T, equalityFn: (x: T, y: T) => boolean): T {\n const prevRef = useRef<T | typeof NOT_INITIALIZED>(NOT_INITIALIZED);\n const next: T = factory();\n const current: T = prevRef.current !== NOT_INITIALIZED && equalityFn(prevRef.current, next) ? prevRef.current : next;\n\n useEffect(() => {\n prevRef.current = current;\n });\n\n return current;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,2BAAgC;AAChC,IAAAA,gBAUO;AACP,qBAA4D;;;ACX7C,SAAR,cAA8D,GAAM,GAAe;AACxF,MAAI,OAAO,GAAG,GAAG,CAAC,GAAG;AACnB,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,IAAI,IAAI,OAAO,QAAQ,CAAC,CAAC;AACxC,QAAM,SAAS,IAAI,IAAI,OAAO,QAAQ,CAAC,CAAC;AAExC,MAAI,OAAO,SAAS,OAAO,MAAM;AAC/B,WAAO;AAAA,EACT;AAEA,QAAM,OAAO,oBAAI,IAAI,CAAC,GAAG,OAAO,KAAK,GAAG,GAA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,MAAM,OAAO,OAAO,EAAE,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC;AAE5G,WAAO,8BAAAJ,QAAA,cAAC,aAAa,UAAb,EAAsB,OAAO,gBAAe,QAAS;AAAA,EAC/D;AAEA,WAAS,2BAA2B,EAAE,mBAAmB,SAAS,GAAG,MAAM,GAA+B;AACxG,UAAM,SAAS,uBAAuB,EAAE,SAAS,EAAE,kBAAkB,CAAC,IAAI,KAAc;AAExF,WAAO,SAAS,8BAAAA,QAAA,cAAC,8BAAU,MAAO,IAAc;AAAA,EAClD;AAEA,QAAM,4CACJ,oBAA0C,6BAA6B;AAEzE,SAAO,OAAO,OAAO;AAAA,IACnB,UAAU;AAAA,IAEV,WAAO,oBAAiC,0BAA0B;AAAA,IAClE;AAAA,IACA;AAAA;AAAA,EAGF,CAAC;AACH;AAEA,IAAO,sDAAQ;","names":["import_react","React","ComponentWithProps","middleware","init","enhancer"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.preview.ts","../src/preview/createChainOfResponsibilityAsRenderCallback.tsx","../src/preview/private/arePropsEqual.ts","../src/preview/private/useMemoValueWithEquality.ts"],"sourcesContent":["export {\n default as createChainOfResponsibility,\n type ChainOfResponsibility,\n type ComponentEnhancer,\n type ComponentHandler,\n type ComponentHandlerResult,\n type ComponentMiddleware,\n type ComponentRenderer,\n type CreateChainOfResponsibilityOptions,\n type InferenceHelper,\n type InferInit,\n type InferMiddleware,\n type InferProps,\n type InferProviderProps,\n type InferProxyProps,\n type InferRequest,\n type ProviderProps,\n type ProxyProps,\n type ReactComponentHandlerResult,\n type UseBuildRenderCallback,\n type UseBuildRenderCallbackOptions\n} from './preview/createChainOfResponsibilityAsRenderCallback.tsx';\n","import { applyMiddleware } from 'handler-chain';\nimport React, {\n createContext,\n Fragment,\n memo,\n useCallback,\n useContext,\n useMemo,\n type ComponentType,\n type PropsWithChildren,\n type ReactNode\n} from 'react';\nimport { custom, function_, object, parse, safeParse } from 'valibot';\n\nimport arePropsEqual from './private/arePropsEqual.ts';\nimport useMemoValueWithEquality from './private/useMemoValueWithEquality.ts';\n\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.\ndeclare global {\n interface ArrayConstructor {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n isArray(arg: any): arg is readonly any[];\n }\n}\n\ntype BaseProps = object;\n\ntype CreateChainOfResponsibilityOptions = {\n /**\n * Allows one component to pass different set of props to its downstream component. Default is false.\n *\n * It is recommended to keep this settings as default to prevent newly added component from unexpectedly changing behavior of downstream components.\n */\n readonly allowOverrideProps?: boolean | undefined;\n\n /**\n * Allows a middleware to pass another request object when calling its next middleware. Default is false.\n *\n * It is recommended to keep this settings as default ot prevent newly added middleware from unexpectedly changing behavior of downstream middleware.\n *\n * To prevent upstream middleware from modifying the request, the request object should be set to be immutable through `Object.freeze`.\n */\n readonly passModifiedRequest?: boolean | undefined;\n};\n\ntype ChainOfResponsibility<Request, Props extends BaseProps, Init> = {\n readonly Provider: ComponentType<ProviderProps<Request, Props, Init>> & InferenceHelper<Request, Props, Init>;\n readonly Proxy: ComponentType<ProxyProps<Request, Props>>;\n readonly reactComponent: ReactComponentHandlerResult<Props>;\n readonly useBuildRenderCallback: () => UseBuildRenderCallback<Request, Props>;\n};\n\n// TODO: Maybe this one should be local.\n// Verify that reactComponent() from an instance of CoR should throw error when used in another instance of CoR.\nconst DO_NOT_CREATE_THIS_OBJECT_YOURSELF = Symbol();\n\ntype ComponentRenderer<Props> = (props: Props) => ReactNode;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst componentHandlerResultSchema = custom<ComponentHandlerResult<any>>(\n value =>\n safeParse(object({ render: function_() }), value).success &&\n !!value &&\n typeof value === 'object' &&\n DO_NOT_CREATE_THIS_OBJECT_YOURSELF in value,\n 'react-chain-of-responsibility: middleware must return value constructed by reactComponent()'\n);\n\ninterface ComponentHandlerResult<Props extends BaseProps> {\n readonly [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined;\n readonly render: (overridingProps?: Partial<Props> | undefined) => ReactNode;\n}\n\ntype ComponentHandler<Request, Props extends BaseProps> = (\n request: Request\n) => ComponentHandlerResult<Props> | undefined;\n\ntype ComponentEnhancer<Request, Props extends BaseProps> = (\n next: ComponentHandler<Request, Props>\n) => ComponentHandler<Request, Props>;\n\ntype ComponentMiddleware<Request, Props extends BaseProps, Init = undefined> = (\n init: Init\n) => ComponentEnhancer<Request, Props>;\n\ntype ReactComponentHandlerResult<Props extends object> = <P extends Props>(\n component: ComponentType<P>,\n bindProps?:\n | (Partial<Props> & Omit<P, keyof Props>)\n | ((props: Props) => Partial<Props> & Omit<P, keyof Props>)\n | undefined\n) => ComponentHandlerResult<Props>;\n\ntype UseBuildRenderCallbackOptions<Props> = {\n readonly fallbackComponent?: ComponentType<Props> | undefined;\n};\n\ninterface UseBuildRenderCallback<Request, Props extends BaseProps> {\n (request: Request, options?: undefined | UseBuildRenderCallbackOptions<Props>): ComponentRenderer<Props> | undefined;\n}\n\ntype BuildContextType<Request, Props extends BaseProps> = {\n readonly enhancer: ComponentEnhancer<Request, Props>;\n};\n\ntype RenderContextType<Props> = {\n readonly renderCallbackProps: Props;\n};\n\ntype ProviderProps<Request, Props extends BaseProps, Init> = PropsWithChildren<{\n readonly 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 BaseProps> = Props & {\n readonly fallbackComponent?: ComponentType<Props> | undefined;\n readonly request: Request;\n};\n\ntype InferenceHelper<Request, Props extends BaseProps, Init> = {\n readonly '~types': {\n readonly init: Init;\n readonly middleware: ComponentMiddleware<Request, Props, Init>;\n readonly props: Props;\n readonly proxyProps: ProxyProps<Request, Props>;\n readonly providerProps: ProviderProps<Request, Props, Init>;\n readonly request: Request;\n };\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferInit<T extends InferenceHelper<any, any, any>> = T['~types']['init'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferMiddleware<T extends InferenceHelper<any, any, any>> = T['~types']['middleware'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProps<T extends InferenceHelper<any, any, any>> = T['~types']['props'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProxyProps<T extends InferenceHelper<any, any, any>> = T['~types']['proxyProps'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProviderProps<T extends InferenceHelper<any, any, any>> = T['~types']['providerProps'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferRequest<T extends InferenceHelper<any, any, any>> = T['~types']['request'];\n\nfunction createChainOfResponsibility<\n Request = void,\n Props extends BaseProps = { readonly children?: never },\n Init = void\n>(options: CreateChainOfResponsibilityOptions = {}): ChainOfResponsibility<Request, Props, Init> {\n // Freeze options to prevent accidental change.\n options = Object.freeze({ ...options });\n\n const BuildContext = createContext<BuildContextType<Request, Props>>(\n Object.freeze({ enhancer: next => request => next(request) })\n );\n\n const RenderContext = createContext<RenderContextType<Props>>(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new Proxy({} as any, {\n get() {\n // The following is assertion, there is no way to hit this line.\n /* istanbul ignore next */\n throw new Error(\n 'react-chain-of-responsibility: this hook cannot be used outside of <Proxy> and useBuildRenderCallback()'\n );\n }\n })\n );\n\n function reactComponent<P extends Props>(\n component: ComponentType<P>,\n bindProps?:\n | (Partial<Props> & Omit<P, keyof Props>)\n | ((props: Props) => Partial<Props> & Omit<P, keyof Props>)\n | undefined\n ): ComponentHandlerResult<Props> {\n return Object.freeze({\n [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined,\n render: (overridingProps?: Partial<Props> | undefined) => (\n <ComponentWithProps\n bindProps={bindProps}\n component={component as ComponentType<Props>}\n overridingProps={overridingProps}\n />\n )\n });\n }\n\n const ComponentWithProps = memo(function ComponentWithProps({\n bindProps,\n component: Component,\n overridingProps\n }: {\n readonly bindProps?: Partial<Props> | ((props: Props) => Partial<Props>) | undefined;\n readonly component: ComponentType<Props>;\n readonly overridingProps?: Partial<Props> | undefined;\n }) {\n const { allowOverrideProps } = options;\n const { renderCallbackProps } = useContext(RenderContext);\n\n if (overridingProps && !arePropsEqual(overridingProps, renderCallbackProps) && !allowOverrideProps) {\n console.warn('react-chain-of-responsibility: \"allowOverrideProps\" must be set to true to override props');\n }\n\n const props = Object.freeze(\n allowOverrideProps ? { ...renderCallbackProps, ...overridingProps } : { ...renderCallbackProps }\n );\n\n return <Component {...props} {...(typeof bindProps === 'function' ? bindProps(props) : bindProps)} />;\n });\n\n const useBuildRenderCallback: () => UseBuildRenderCallback<Request, Props> = () => {\n const { enhancer } = useContext(BuildContext);\n\n return useCallback(\n (request, buildOptions = {}) => {\n const result =\n // Put the \"fallbackComponent\" as the last one in the chain.\n enhancer(() => {\n const { fallbackComponent } = buildOptions;\n\n if (!fallbackComponent) {\n console.warn(\n 'react-chain-of-responsibility: the request has fall through all middleware, set \"fallbackComponent\" as a catchall',\n request\n );\n\n // For clarity, we are returning `undefined` instead of `() => undefined`.\n return;\n }\n\n return Object.freeze({\n // Convert fallback component as renderer.\n [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined,\n render: () => (\n // Currently, there are no ways to set `bindProps` to `fallbackComponent`.\n // `fallbackComponent` do not need `overridingProps` because it is the last one in the chain, it would not have the next() function.\n <ComponentWithProps component={fallbackComponent} />\n )\n });\n })(request);\n\n return (\n result &&\n ((props: Props) => {\n const renderCallbackProps = useMemoValueWithEquality<Props>(() => props, arePropsEqual);\n\n const context = useMemo<RenderContextType<Props>>(\n () => Object.freeze({ renderCallbackProps }),\n [renderCallbackProps]\n );\n\n return <RenderContext.Provider value={context}>{result.render()}</RenderContext.Provider>;\n })\n );\n },\n [enhancer]\n );\n };\n\n function ChainOfResponsibilityProvider({ children, init, middleware }: ProviderProps<Request, Props, Init>) {\n if (!Array.isArray(middleware) || middleware.some(middleware => typeof middleware !== 'function')) {\n throw new Error('react-chain-of-responsibility: \"middleware\" prop must be an array of functions');\n }\n\n // Remap the middleware, so all inputs/outputs are validated.\n const fortifiedMiddleware = useMemo(\n () =>\n Object.freeze(\n middleware.map<ComponentMiddleware<Request, Props, Init>>(fn => (init: Init) => {\n const enhancer = fn(init);\n\n return next => originalRequest => {\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(\n 'react-chain-of-responsibility: next() cannot be called after the function had returned synchronously'\n );\n }\n\n // We do not allow passing void/undefined to next() because it would be confusing whether to keep the original request or pass an undefined.\n !options.passModifiedRequest &&\n !Object.is(nextRequest, originalRequest) &&\n console.warn(\n 'react-chain-of-responsibility: next() must be called with the original request, otherwise, set \"options.passModifiedRequest\" to true to pass a different request object downstream'\n );\n\n return next(options.passModifiedRequest ? nextRequest : originalRequest);\n })(originalRequest);\n\n hasReturned = true;\n\n // Make sure the return value is built using our helper function for forward-compatibility reason.\n return returnValue && parse(componentHandlerResultSchema, returnValue);\n };\n })\n ),\n [middleware]\n );\n\n const { enhancer: parentEnhancer } = useContext(BuildContext);\n\n const enhancer = useMemo<ComponentEnhancer<Request, Props>>(\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<ComponentHandlerResult<Props> | undefined, Request, Init>(\n ...[...fortifiedMiddleware, ...[() => parentEnhancer]]\n )(init as Init),\n [init, fortifiedMiddleware, parentEnhancer]\n );\n\n const contextValue = useMemo<BuildContextType<Request, Props>>(() => Object.freeze({ enhancer }), [enhancer]);\n\n return <BuildContext.Provider value={contextValue}>{children}</BuildContext.Provider>;\n }\n\n function ChainOfResponsibilityProxy({ fallbackComponent, request, ...props }: ProxyProps<Request, Props>) {\n const result = useBuildRenderCallback()(request, { fallbackComponent })?.(props as Props);\n\n return result ? <Fragment>{result}</Fragment> : null;\n }\n\n const MemoizedChainOfResponsibilityProvider =\n memo<ProviderProps<Request, Props, Init>>(ChainOfResponsibilityProvider);\n\n return Object.freeze({\n Provider: MemoizedChainOfResponsibilityProvider as typeof MemoizedChainOfResponsibilityProvider &\n InferenceHelper<Request, Props, Init>,\n Proxy: memo<ProxyProps<Request, Props>>(ChainOfResponsibilityProxy),\n reactComponent,\n useBuildRenderCallback\n\n // TODO: Consider adding back `asMiddleware`.\n });\n}\n\nexport default createChainOfResponsibility;\nexport {\n type ChainOfResponsibility,\n type ComponentEnhancer,\n type ComponentHandler,\n type ComponentHandlerResult,\n type ComponentMiddleware,\n type ComponentRenderer,\n type CreateChainOfResponsibilityOptions,\n type InferenceHelper,\n type InferInit,\n type InferMiddleware,\n type InferProps,\n type InferProviderProps,\n type InferProxyProps,\n type InferRequest,\n type ProviderProps,\n type ProxyProps,\n type ReactComponentHandlerResult,\n type UseBuildRenderCallback,\n type UseBuildRenderCallbackOptions\n};\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function arePropsEqual<T extends Record<string, any>>(x: T, y: T): boolean {\n if (Object.is(x, y)) {\n return true;\n }\n\n const mapOfX = new Map(Object.entries(x));\n const mapOfY = new Map(Object.entries(y));\n\n if (mapOfX.size !== mapOfY.size) {\n return false;\n }\n\n const keys = new Set([...mapOfX.keys(), ...mapOfY.keys()]);\n\n for (const key of keys) {\n if (!Object.is(mapOfX.get(key), mapOfY.get(key))) {\n return false;\n }\n }\n\n return true;\n}\n","import { useEffect, useRef } from 'react';\n\nconst NOT_INITIALIZED = Symbol();\n\nexport default function useMemoValueWithEquality<T>(factory: () => T, equalityFn: (x: T, y: T) => boolean): T {\n const prevRef = useRef<T | typeof NOT_INITIALIZED>(NOT_INITIALIZED);\n const next: T = factory();\n const current: T = prevRef.current !== NOT_INITIALIZED && equalityFn(prevRef.current, next) ? prevRef.current : next;\n\n useEffect(() => {\n prevRef.current = current;\n });\n\n return current;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,2BAAgC;AAChC,IAAAA,gBAUO;AACP,qBAA4D;;;ACX7C,SAAR,cAA8D,GAAM,GAAe;AACxF,MAAI,OAAO,GAAG,GAAG,CAAC,GAAG;AACnB,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,IAAI,IAAI,OAAO,QAAQ,CAAC,CAAC;AACxC,QAAM,SAAS,IAAI,IAAI,OAAO,QAAQ,CAAC,CAAC;AAExC,MAAI,OAAO,SAAS,OAAO,MAAM;AAC/B,WAAO;AAAA,EACT;AAEA,QAAM,OAAO,oBAAI,IAAI,CAAC,GAAG,OAAO,KAAK,GAAG,GAAG,OAAO,KAAK,CAAC,CAAC;AAEzD,aAAW,OAAO,MAAM;AACtB,QAAI,CAAC,OAAO,GAAG,OAAO,IAAI,GAAG,GAAG,OAAO,IAAI,GAAG,CAAC,GAAG;AAChD,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;;;ACtBA,mBAAkC;AAElC,IAAM,kBAAkB,OAAO;AAEhB,SAAR,yBAA6C,SAAkB,YAAwC;AAC5G,QAAM,cAAU,qBAAmC,eAAe;AAClE,QAAM,OAAU,QAAQ;AACxB,QAAM,UAAa,QAAQ,YAAY,mBAAmB,WAAW,QAAQ,SAAS,IAAI,IAAI,QAAQ,UAAU;AAEhH,8BAAU,MAAM;AACd,YAAQ,UAAU;AAAA,EACpB,CAAC;AAED,SAAO;AACT;;;AFyCA,IAAM,qCAAqC,OAAO;AAKlD,IAAM,mCAA+B;AAAA,EACnC,eACE,8BAAU,uBAAO,EAAE,YAAQ,0BAAU,EAAE,CAAC,GAAG,KAAK,EAAE,WAClD,CAAC,CAAC,SACF,OAAO,UAAU,YACjB,sCAAsC;AAAA,EACxC;AACF;AAiFA,SAA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aAAa,eAAe,KACvC,QAAQ;AAAA,gBACN;AAAA,cACF;AAEF,qBAAO,KAAK,QAAQ,sBAAsB,cAAc,eAAe;AAAA,YACzE,CAAC,EAAE,eAAe;AAElB,0BAAc;AAGd,mBAAO,mBAAe,sBAAM,8BAA8B,WAAW;AAAA,UACvE;AAAA,QACF,CAAC;AAAA,MACH;AAAA,MACF,CAAC,UAAU;AAAA,IACb;AAEA,UAAM,EAAE,UAAU,eAAe,QAAI,0BAAW,YAAY;AAE5D,UAAM,eAAW;AAAA,MACf;AAAA;AAAA;AAAA;AAAA,YAIE;AAAA,UACE,GAAG,CAAC,GAAG,qBAAqB,GAAG,CAAC,MAAM,cAAc,CAAC;AAAA,QACvD,EAAE,IAAY;AAAA;AAAA,MAChB,CAAC,MAAM,qBAAqB,cAAc;AAAA,IAC5C;AAEA,UAAM,mBAAe,uBAA0C,MAAM,OAAO,OAAO,EAAE,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC;AAE5G,WAAO,8BAAAJ,QAAA,cAAC,aAAa,UAAb,EAAsB,OAAO,gBAAe,QAAS;AAAA,EAC/D;AAEA,WAAS,2BAA2B,EAAE,mBAAmB,SAAS,GAAG,MAAM,GAA+B;AACxG,UAAM,SAAS,uBAAuB,EAAE,SAAS,EAAE,kBAAkB,CAAC,IAAI,KAAc;AAExF,WAAO,SAAS,8BAAAA,QAAA,cAAC,8BAAU,MAAO,IAAc;AAAA,EAClD;AAEA,QAAM,4CACJ,oBAA0C,6BAA6B;AAEzE,SAAO,OAAO,OAAO;AAAA,IACnB,UAAU;AAAA,IAEV,WAAO,oBAAiC,0BAA0B;AAAA,IAClE;AAAA,IACA;AAAA;AAAA,EAGF,CAAC;AACH;AAEA,IAAO,sDAAQ;","names":["import_react","React","ComponentWithProps","middleware","init","enhancer"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// src/createChainOfResponsibilityAsRenderCallback.tsx
|
|
1
|
+
// src/preview/createChainOfResponsibilityAsRenderCallback.tsx
|
|
2
2
|
import { applyMiddleware } from "handler-chain";
|
|
3
3
|
import React, {
|
|
4
4
|
createContext,
|
|
@@ -10,7 +10,7 @@ import React, {
|
|
|
10
10
|
} from "react";
|
|
11
11
|
import { custom, function_, object, parse, safeParse } from "valibot";
|
|
12
12
|
|
|
13
|
-
// src/private/arePropsEqual.ts
|
|
13
|
+
// src/preview/private/arePropsEqual.ts
|
|
14
14
|
function arePropsEqual(x, y) {
|
|
15
15
|
if (Object.is(x, y)) {
|
|
16
16
|
return true;
|
|
@@ -29,7 +29,7 @@ function arePropsEqual(x, y) {
|
|
|
29
29
|
return true;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
// src/private/useMemoValueWithEquality.ts
|
|
32
|
+
// src/preview/private/useMemoValueWithEquality.ts
|
|
33
33
|
import { useEffect, useRef } from "react";
|
|
34
34
|
var NOT_INITIALIZED = Symbol();
|
|
35
35
|
function useMemoValueWithEquality(factory, equalityFn) {
|
|
@@ -42,7 +42,7 @@ function useMemoValueWithEquality(factory, equalityFn) {
|
|
|
42
42
|
return current;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
// src/createChainOfResponsibilityAsRenderCallback.tsx
|
|
45
|
+
// src/preview/createChainOfResponsibilityAsRenderCallback.tsx
|
|
46
46
|
var DO_NOT_CREATE_THIS_OBJECT_YOURSELF = Symbol();
|
|
47
47
|
var componentHandlerResultSchema = custom(
|
|
48
48
|
(value) => safeParse(object({ render: function_() }), value).success && !!value && typeof value === "object" && DO_NOT_CREATE_THIS_OBJECT_YOURSELF in value,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/createChainOfResponsibilityAsRenderCallback.tsx","../src/private/arePropsEqual.ts","../src/private/useMemoValueWithEquality.ts"],"sourcesContent":["import { applyMiddleware } from 'handler-chain';\nimport React, {\n createContext,\n Fragment,\n memo,\n useCallback,\n useContext,\n useMemo,\n type ComponentType,\n type PropsWithChildren,\n type ReactNode\n} from 'react';\nimport { custom, function_, object, parse, safeParse } from 'valibot';\n\nimport arePropsEqual from './private/arePropsEqual.ts';\nimport useMemoValueWithEquality from './private/useMemoValueWithEquality.ts';\n\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.\ndeclare global {\n interface ArrayConstructor {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n isArray(arg: any): arg is readonly any[];\n }\n}\n\ntype BaseProps = object;\n\ntype CreateChainOfResponsibilityOptions = {\n /**\n * Allows one component to pass different set of props to its downstream component. Default is false.\n *\n * It is recommended to keep this settings as default to prevent newly added component from unexpectedly changing behavior of downstream components.\n */\n readonly allowOverrideProps?: boolean | undefined;\n\n /**\n * Allows a middleware to pass another request object when calling its next middleware. Default is false.\n *\n * It is recommended to keep this settings as default ot prevent newly added middleware from unexpectedly changing behavior of downstream middleware.\n *\n * To prevent upstream middleware from modifying the request, the request object should be set to be immutable through `Object.freeze`.\n */\n readonly passModifiedRequest?: boolean | undefined;\n};\n\ntype ChainOfResponsibility<Request, Props extends BaseProps, Init> = {\n readonly Provider: ComponentType<ProviderProps<Request, Props, Init>> & InferenceHelper<Request, Props, Init>;\n readonly Proxy: ComponentType<ProxyProps<Request, Props>>;\n readonly reactComponent: ReactComponentHandlerResult<Props>;\n readonly useBuildRenderCallback: () => UseBuildRenderCallback<Request, Props>;\n};\n\n// TODO: Maybe this one should be local.\n// Verify that reactComponent() from an instance of CoR should throw error when used in another instance of CoR.\nconst DO_NOT_CREATE_THIS_OBJECT_YOURSELF = Symbol();\n\ntype ComponentRenderer<Props> = (props: Props) => ReactNode;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst componentHandlerResultSchema = custom<ComponentHandlerResult<any>>(\n value =>\n safeParse(object({ render: function_() }), value).success &&\n !!value &&\n typeof value === 'object' &&\n DO_NOT_CREATE_THIS_OBJECT_YOURSELF in value,\n 'react-chain-of-responsibility: middleware must return value constructed by reactComponent()'\n);\n\ninterface ComponentHandlerResult<Props extends BaseProps> {\n readonly [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined;\n readonly render: (overridingProps?: Partial<Props> | undefined) => ReactNode;\n}\n\ntype ComponentHandler<Request, Props extends BaseProps> = (\n request: Request\n) => ComponentHandlerResult<Props> | undefined;\n\ntype ComponentEnhancer<Request, Props extends BaseProps> = (\n next: ComponentHandler<Request, Props>\n) => ComponentHandler<Request, Props>;\n\ntype ComponentMiddleware<Request, Props extends BaseProps, Init = undefined> = (\n init: Init\n) => ComponentEnhancer<Request, Props>;\n\ntype ReactComponentHandlerResult<Props extends object> = <P extends Props>(\n component: ComponentType<P>,\n bindProps?:\n | (Partial<Props> & Omit<P, keyof Props>)\n | ((props: Props) => Partial<Props> & Omit<P, keyof Props>)\n | undefined\n) => ComponentHandlerResult<Props>;\n\ntype UseBuildRenderCallbackOptions<Props> = {\n readonly fallbackComponent?: ComponentType<Props> | undefined;\n};\n\ninterface UseBuildRenderCallback<Request, Props extends BaseProps> {\n (request: Request, options?: undefined | UseBuildRenderCallbackOptions<Props>): ComponentRenderer<Props> | undefined;\n}\n\ntype BuildContextType<Request, Props extends BaseProps> = {\n readonly enhancer: ComponentEnhancer<Request, Props>;\n};\n\ntype RenderContextType<Props> = {\n readonly renderCallbackProps: Props;\n};\n\ntype ProviderProps<Request, Props extends BaseProps, Init> = PropsWithChildren<{\n readonly 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 BaseProps> = Props & {\n readonly fallbackComponent?: ComponentType<Props> | undefined;\n readonly request: Request;\n};\n\ntype InferenceHelper<Request, Props extends BaseProps, Init> = {\n readonly '~types': {\n readonly init: Init;\n readonly middleware: ComponentMiddleware<Request, Props, Init>;\n readonly props: Props;\n readonly proxyProps: ProxyProps<Request, Props>;\n readonly providerProps: ProviderProps<Request, Props, Init>;\n readonly request: Request;\n };\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferInit<T extends InferenceHelper<any, any, any>> = T['~types']['init'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferMiddleware<T extends InferenceHelper<any, any, any>> = T['~types']['middleware'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProps<T extends InferenceHelper<any, any, any>> = T['~types']['props'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProxyProps<T extends InferenceHelper<any, any, any>> = T['~types']['proxyProps'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProviderProps<T extends InferenceHelper<any, any, any>> = T['~types']['providerProps'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferRequest<T extends InferenceHelper<any, any, any>> = T['~types']['request'];\n\nfunction createChainOfResponsibility<\n Request = void,\n Props extends BaseProps = { readonly children?: never },\n Init = void\n>(options: CreateChainOfResponsibilityOptions = {}): ChainOfResponsibility<Request, Props, Init> {\n // Freeze options to prevent accidental change.\n options = Object.freeze({ ...options });\n\n const BuildContext = createContext<BuildContextType<Request, Props>>(\n Object.freeze({ enhancer: next => request => next(request) })\n );\n\n const RenderContext = createContext<RenderContextType<Props>>(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new Proxy({} as any, {\n get() {\n // The following is assertion, there is no way to hit this line.\n /* istanbul ignore next */\n throw new Error(\n 'react-chain-of-responsibility: this hook cannot be used outside of <Proxy> and useBuildRenderCallback()'\n );\n }\n })\n );\n\n function reactComponent<P extends Props>(\n component: ComponentType<P>,\n bindProps?:\n | (Partial<Props> & Omit<P, keyof Props>)\n | ((props: Props) => Partial<Props> & Omit<P, keyof Props>)\n | undefined\n ): ComponentHandlerResult<Props> {\n return Object.freeze({\n [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined,\n render: (overridingProps?: Partial<Props> | undefined) => (\n <ComponentWithProps\n bindProps={bindProps}\n component={component as ComponentType<Props>}\n overridingProps={overridingProps}\n />\n )\n });\n }\n\n const ComponentWithProps = memo(function ComponentWithProps({\n bindProps,\n component: Component,\n overridingProps\n }: {\n readonly bindProps?: Partial<Props> | ((props: Props) => Partial<Props>) | undefined;\n readonly component: ComponentType<Props>;\n readonly overridingProps?: Partial<Props> | undefined;\n }) {\n const { allowOverrideProps } = options;\n const { renderCallbackProps } = useContext(RenderContext);\n\n if (overridingProps && !arePropsEqual(overridingProps, renderCallbackProps) && !allowOverrideProps) {\n console.warn('react-chain-of-responsibility: \"allowOverrideProps\" must be set to true to override props');\n }\n\n const props = Object.freeze(\n allowOverrideProps ? { ...renderCallbackProps, ...overridingProps } : { ...renderCallbackProps }\n );\n\n return <Component {...props} {...(typeof bindProps === 'function' ? bindProps(props) : bindProps)} />;\n });\n\n const useBuildRenderCallback: () => UseBuildRenderCallback<Request, Props> = () => {\n const { enhancer } = useContext(BuildContext);\n\n return useCallback(\n (request, buildOptions = {}) => {\n const result =\n // Put the \"fallbackComponent\" as the last one in the chain.\n enhancer(() => {\n const { fallbackComponent } = buildOptions;\n\n if (!fallbackComponent) {\n console.warn(\n 'react-chain-of-responsibility: the request has fall through all middleware, set \"fallbackComponent\" as a catchall',\n request\n );\n\n // For clarity, we are returning `undefined` instead of `() => undefined`.\n return;\n }\n\n return Object.freeze({\n // Convert fallback component as renderer.\n [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined,\n render: () => (\n // Currently, there are no ways to set `bindProps` to `fallbackComponent`.\n // `fallbackComponent` do not need `overridingProps` because it is the last one in the chain, it would not have the next() function.\n <ComponentWithProps component={fallbackComponent} />\n )\n });\n })(request);\n\n return (\n result &&\n ((props: Props) => {\n const renderCallbackProps = useMemoValueWithEquality<Props>(() => props, arePropsEqual);\n\n const context = useMemo<RenderContextType<Props>>(\n () => Object.freeze({ renderCallbackProps }),\n [renderCallbackProps]\n );\n\n return <RenderContext.Provider value={context}>{result.render()}</RenderContext.Provider>;\n })\n );\n },\n [enhancer]\n );\n };\n\n function ChainOfResponsibilityProvider({ children, init, middleware }: ProviderProps<Request, Props, Init>) {\n if (!Array.isArray(middleware) || middleware.some(middleware => typeof middleware !== 'function')) {\n throw new Error('react-chain-of-responsibility: \"middleware\" prop must be an array of functions');\n }\n\n // Remap the middleware, so all inputs/outputs are validated.\n const fortifiedMiddleware = useMemo(\n () =>\n Object.freeze(\n middleware.map<ComponentMiddleware<Request, Props, Init>>(fn => (init: Init) => {\n const enhancer = fn(init);\n\n return next => originalRequest => {\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(\n 'react-chain-of-responsibility: next() cannot be called after the function had returned synchronously'\n );\n }\n\n // We do not allow passing void/undefined to next() because it would be confusing whether to keep the original request or pass an undefined.\n !options.passModifiedRequest &&\n !Object.is(nextRequest, originalRequest) &&\n console.warn(\n 'react-chain-of-responsibility: next() must be called with the original request, otherwise, set \"options.passModifiedRequest\" to true to pass a different request object downstream'\n );\n\n return next(options.passModifiedRequest ? nextRequest : originalRequest);\n })(originalRequest);\n\n hasReturned = true;\n\n // Make sure the return value is built using our helper function for forward-compatibility reason.\n return returnValue && parse(componentHandlerResultSchema, returnValue);\n };\n })\n ),\n [middleware]\n );\n\n const { enhancer: parentEnhancer } = useContext(BuildContext);\n\n const enhancer = useMemo<ComponentEnhancer<Request, Props>>(\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<ComponentHandlerResult<Props> | undefined, Request, Init>(\n ...[...fortifiedMiddleware, ...[() => parentEnhancer]]\n )(init as Init),\n [init, fortifiedMiddleware, parentEnhancer]\n );\n\n const contextValue = useMemo<BuildContextType<Request, Props>>(() => Object.freeze({ enhancer }), [enhancer]);\n\n return <BuildContext.Provider value={contextValue}>{children}</BuildContext.Provider>;\n }\n\n function ChainOfResponsibilityProxy({ fallbackComponent, request, ...props }: ProxyProps<Request, Props>) {\n const result = useBuildRenderCallback()(request, { fallbackComponent })?.(props as Props);\n\n return result ? <Fragment>{result}</Fragment> : null;\n }\n\n const MemoizedChainOfResponsibilityProvider =\n memo<ProviderProps<Request, Props, Init>>(ChainOfResponsibilityProvider);\n\n return Object.freeze({\n Provider: MemoizedChainOfResponsibilityProvider as typeof MemoizedChainOfResponsibilityProvider &\n InferenceHelper<Request, Props, Init>,\n Proxy: memo<ProxyProps<Request, Props>>(ChainOfResponsibilityProxy),\n reactComponent,\n useBuildRenderCallback\n\n // TODO: Consider adding back `asMiddleware`.\n });\n}\n\nexport default createChainOfResponsibility;\nexport {\n type ChainOfResponsibility,\n type ComponentEnhancer,\n type ComponentHandler,\n type ComponentHandlerResult,\n type ComponentMiddleware,\n type ComponentRenderer,\n type CreateChainOfResponsibilityOptions,\n type InferenceHelper,\n type InferInit,\n type InferMiddleware,\n type InferProps,\n type InferProviderProps,\n type InferProxyProps,\n type InferRequest,\n type ProviderProps,\n type ProxyProps,\n type ReactComponentHandlerResult,\n type UseBuildRenderCallback,\n type UseBuildRenderCallbackOptions\n};\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function arePropsEqual<T extends Record<string, any>>(x: T, y: T): boolean {\n if (Object.is(x, y)) {\n return true;\n }\n\n const mapOfX = new Map(Object.entries(x));\n const mapOfY = new Map(Object.entries(y));\n\n if (mapOfX.size !== mapOfY.size) {\n return false;\n }\n\n const keys = new Set([...mapOfX.keys(), ...mapOfY.keys()]);\n\n for (const key of keys) {\n if (!Object.is(mapOfX.get(key), mapOfY.get(key))) {\n return false;\n }\n }\n\n return true;\n}\n","import { useEffect, useRef } from 'react';\n\nconst NOT_INITIALIZED = Symbol();\n\nexport default function useMemoValueWithEquality<T>(factory: () => T, equalityFn: (x: T, y: T) => boolean): T {\n const prevRef = useRef<T | typeof NOT_INITIALIZED>(NOT_INITIALIZED);\n const next: T = factory();\n const current: T = prevRef.current !== NOT_INITIALIZED && equalityFn(prevRef.current, next) ? prevRef.current : next;\n\n useEffect(() => {\n prevRef.current = current;\n });\n\n return current;\n}\n"],"mappings":";AAAA,SAAS,uBAAuB;AAChC,OAAO;AAAA,EACL;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AACP,SAAS,QAAQ,WAAW,QAAQ,OAAO,iBAAiB;;;ACX7C,SAAR,cAA8D,GAAM,GAAe;AACxF,MAAI,OAAO,GAAG,GAAG,CAAC,GAAG;AACnB,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,IAAI,IAAI,OAAO,QAAQ,CAAC,CAAC;AACxC,QAAM,SAAS,IAAI,IAAI,OAAO,QAAQ,CAAC,CAAC;AAExC,MAAI,OAAO,SAAS,OAAO,MAAM;AAC/B,WAAO;AAAA,EACT;AAEA,QAAM,OAAO,oBAAI,IAAI,CAAC,GAAG,OAAO,KAAK,GAAG,GAAG,OAAO,KAAK,CAAC,CAAC;AAEzD,aAAW,OAAO,MAAM;AACtB,QAAI,CAAC,OAAO,GAAG,OAAO,IAAI,GAAG,GAAG,OAAO,IAAI,GAAG,CAAC,GAAG;AAChD,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;;;ACtBA,SAAS,WAAW,cAAc;AAElC,IAAM,kBAAkB,OAAO;AAEhB,SAAR,yBAA6C,SAAkB,YAAwC;AAC5G,QAAM,UAAU,OAAmC,eAAe;AAClE,QAAM,OAAU,QAAQ;AACxB,QAAM,UAAa,QAAQ,YAAY,mBAAmB,WAAW,QAAQ,SAAS,IAAI,IAAI,QAAQ,UAAU;AAEhH,YAAU,MAAM;AACd,YAAQ,UAAU;AAAA,EACpB,CAAC;AAED,SAAO;AACT;;;AFyCA,IAAM,qCAAqC,OAAO;AAKlD,IAAM,+BAA+B;AAAA,EACnC,WACE,UAAU,OAAO,EAAE,QAAQ,UAAU,EAAE,CAAC,GAAG,KAAK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,uBACP,CAAC,OAAO,GAAG,aAAa,eAAe,KACvC,QAAQ;AAAA,gBACN;AAAA,cACF;AAEF,qBAAO,KAAK,QAAQ,sBAAsB,cAAc,eAAe;AAAA,YACzE,CAAC,EAAE,eAAe;AAElB,0BAAc;AAGd,mBAAO,eAAe,MAAM,8BAA8B,WAAW;AAAA,UACvE;AAAA,QACF,CAAC;AAAA,MACH;AAAA,MACF,CAAC,UAAU;AAAA,IACb;AAEA,UAAM,EAAE,UAAU,eAAe,IAAI,WAAW,YAAY;AAE5D,UAAM,WAAW;AAAA,MACf;AAAA;AAAA;AAAA;AAAA,QAIE;AAAA,UACE,GAAG,CAAC,GAAG,qBAAqB,GAAG,CAAC,MAAM,cAAc,CAAC;AAAA,QACvD,EAAE,IAAY;AAAA;AAAA,MAChB,CAAC,MAAM,qBAAqB,cAAc;AAAA,IAC5C;AAEA,UAAM,eAAe,QAA0C,MAAM,OAAO,OAAO,EAAE,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC;AAE5G,WAAO,oCAAC,aAAa,UAAb,EAAsB,OAAO,gBAAe,QAAS;AAAA,EAC/D;AAEA,WAAS,2BAA2B,EAAE,mBAAmB,SAAS,GAAG,MAAM,GAA+B;AACxG,UAAM,SAAS,uBAAuB,EAAE,SAAS,EAAE,kBAAkB,CAAC,IAAI,KAAc;AAExF,WAAO,SAAS,oCAAC,gBAAU,MAAO,IAAc;AAAA,EAClD;AAEA,QAAM,wCACJ,KAA0C,6BAA6B;AAEzE,SAAO,OAAO,OAAO;AAAA,IACnB,UAAU;AAAA,IAEV,OAAO,KAAiC,0BAA0B;AAAA,IAClE;AAAA,IACA;AAAA;AAAA,EAGF,CAAC;AACH;AAEA,IAAO,sDAAQ;","names":["ComponentWithProps","middleware","init","enhancer"]}
|
|
1
|
+
{"version":3,"sources":["../src/preview/createChainOfResponsibilityAsRenderCallback.tsx","../src/preview/private/arePropsEqual.ts","../src/preview/private/useMemoValueWithEquality.ts"],"sourcesContent":["import { applyMiddleware } from 'handler-chain';\nimport React, {\n createContext,\n Fragment,\n memo,\n useCallback,\n useContext,\n useMemo,\n type ComponentType,\n type PropsWithChildren,\n type ReactNode\n} from 'react';\nimport { custom, function_, object, parse, safeParse } from 'valibot';\n\nimport arePropsEqual from './private/arePropsEqual.ts';\nimport useMemoValueWithEquality from './private/useMemoValueWithEquality.ts';\n\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.\ndeclare global {\n interface ArrayConstructor {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n isArray(arg: any): arg is readonly any[];\n }\n}\n\ntype BaseProps = object;\n\ntype CreateChainOfResponsibilityOptions = {\n /**\n * Allows one component to pass different set of props to its downstream component. Default is false.\n *\n * It is recommended to keep this settings as default to prevent newly added component from unexpectedly changing behavior of downstream components.\n */\n readonly allowOverrideProps?: boolean | undefined;\n\n /**\n * Allows a middleware to pass another request object when calling its next middleware. Default is false.\n *\n * It is recommended to keep this settings as default ot prevent newly added middleware from unexpectedly changing behavior of downstream middleware.\n *\n * To prevent upstream middleware from modifying the request, the request object should be set to be immutable through `Object.freeze`.\n */\n readonly passModifiedRequest?: boolean | undefined;\n};\n\ntype ChainOfResponsibility<Request, Props extends BaseProps, Init> = {\n readonly Provider: ComponentType<ProviderProps<Request, Props, Init>> & InferenceHelper<Request, Props, Init>;\n readonly Proxy: ComponentType<ProxyProps<Request, Props>>;\n readonly reactComponent: ReactComponentHandlerResult<Props>;\n readonly useBuildRenderCallback: () => UseBuildRenderCallback<Request, Props>;\n};\n\n// TODO: Maybe this one should be local.\n// Verify that reactComponent() from an instance of CoR should throw error when used in another instance of CoR.\nconst DO_NOT_CREATE_THIS_OBJECT_YOURSELF = Symbol();\n\ntype ComponentRenderer<Props> = (props: Props) => ReactNode;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst componentHandlerResultSchema = custom<ComponentHandlerResult<any>>(\n value =>\n safeParse(object({ render: function_() }), value).success &&\n !!value &&\n typeof value === 'object' &&\n DO_NOT_CREATE_THIS_OBJECT_YOURSELF in value,\n 'react-chain-of-responsibility: middleware must return value constructed by reactComponent()'\n);\n\ninterface ComponentHandlerResult<Props extends BaseProps> {\n readonly [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined;\n readonly render: (overridingProps?: Partial<Props> | undefined) => ReactNode;\n}\n\ntype ComponentHandler<Request, Props extends BaseProps> = (\n request: Request\n) => ComponentHandlerResult<Props> | undefined;\n\ntype ComponentEnhancer<Request, Props extends BaseProps> = (\n next: ComponentHandler<Request, Props>\n) => ComponentHandler<Request, Props>;\n\ntype ComponentMiddleware<Request, Props extends BaseProps, Init = undefined> = (\n init: Init\n) => ComponentEnhancer<Request, Props>;\n\ntype ReactComponentHandlerResult<Props extends object> = <P extends Props>(\n component: ComponentType<P>,\n bindProps?:\n | (Partial<Props> & Omit<P, keyof Props>)\n | ((props: Props) => Partial<Props> & Omit<P, keyof Props>)\n | undefined\n) => ComponentHandlerResult<Props>;\n\ntype UseBuildRenderCallbackOptions<Props> = {\n readonly fallbackComponent?: ComponentType<Props> | undefined;\n};\n\ninterface UseBuildRenderCallback<Request, Props extends BaseProps> {\n (request: Request, options?: undefined | UseBuildRenderCallbackOptions<Props>): ComponentRenderer<Props> | undefined;\n}\n\ntype BuildContextType<Request, Props extends BaseProps> = {\n readonly enhancer: ComponentEnhancer<Request, Props>;\n};\n\ntype RenderContextType<Props> = {\n readonly renderCallbackProps: Props;\n};\n\ntype ProviderProps<Request, Props extends BaseProps, Init> = PropsWithChildren<{\n readonly 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 BaseProps> = Props & {\n readonly fallbackComponent?: ComponentType<Props> | undefined;\n readonly request: Request;\n};\n\ntype InferenceHelper<Request, Props extends BaseProps, Init> = {\n readonly '~types': {\n readonly init: Init;\n readonly middleware: ComponentMiddleware<Request, Props, Init>;\n readonly props: Props;\n readonly proxyProps: ProxyProps<Request, Props>;\n readonly providerProps: ProviderProps<Request, Props, Init>;\n readonly request: Request;\n };\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferInit<T extends InferenceHelper<any, any, any>> = T['~types']['init'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferMiddleware<T extends InferenceHelper<any, any, any>> = T['~types']['middleware'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProps<T extends InferenceHelper<any, any, any>> = T['~types']['props'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProxyProps<T extends InferenceHelper<any, any, any>> = T['~types']['proxyProps'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferProviderProps<T extends InferenceHelper<any, any, any>> = T['~types']['providerProps'];\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferRequest<T extends InferenceHelper<any, any, any>> = T['~types']['request'];\n\nfunction createChainOfResponsibility<\n Request = void,\n Props extends BaseProps = { readonly children?: never },\n Init = void\n>(options: CreateChainOfResponsibilityOptions = {}): ChainOfResponsibility<Request, Props, Init> {\n // Freeze options to prevent accidental change.\n options = Object.freeze({ ...options });\n\n const BuildContext = createContext<BuildContextType<Request, Props>>(\n Object.freeze({ enhancer: next => request => next(request) })\n );\n\n const RenderContext = createContext<RenderContextType<Props>>(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new Proxy({} as any, {\n get() {\n // The following is assertion, there is no way to hit this line.\n /* istanbul ignore next */\n throw new Error(\n 'react-chain-of-responsibility: this hook cannot be used outside of <Proxy> and useBuildRenderCallback()'\n );\n }\n })\n );\n\n function reactComponent<P extends Props>(\n component: ComponentType<P>,\n bindProps?:\n | (Partial<Props> & Omit<P, keyof Props>)\n | ((props: Props) => Partial<Props> & Omit<P, keyof Props>)\n | undefined\n ): ComponentHandlerResult<Props> {\n return Object.freeze({\n [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined,\n render: (overridingProps?: Partial<Props> | undefined) => (\n <ComponentWithProps\n bindProps={bindProps}\n component={component as ComponentType<Props>}\n overridingProps={overridingProps}\n />\n )\n });\n }\n\n const ComponentWithProps = memo(function ComponentWithProps({\n bindProps,\n component: Component,\n overridingProps\n }: {\n readonly bindProps?: Partial<Props> | ((props: Props) => Partial<Props>) | undefined;\n readonly component: ComponentType<Props>;\n readonly overridingProps?: Partial<Props> | undefined;\n }) {\n const { allowOverrideProps } = options;\n const { renderCallbackProps } = useContext(RenderContext);\n\n if (overridingProps && !arePropsEqual(overridingProps, renderCallbackProps) && !allowOverrideProps) {\n console.warn('react-chain-of-responsibility: \"allowOverrideProps\" must be set to true to override props');\n }\n\n const props = Object.freeze(\n allowOverrideProps ? { ...renderCallbackProps, ...overridingProps } : { ...renderCallbackProps }\n );\n\n return <Component {...props} {...(typeof bindProps === 'function' ? bindProps(props) : bindProps)} />;\n });\n\n const useBuildRenderCallback: () => UseBuildRenderCallback<Request, Props> = () => {\n const { enhancer } = useContext(BuildContext);\n\n return useCallback(\n (request, buildOptions = {}) => {\n const result =\n // Put the \"fallbackComponent\" as the last one in the chain.\n enhancer(() => {\n const { fallbackComponent } = buildOptions;\n\n if (!fallbackComponent) {\n console.warn(\n 'react-chain-of-responsibility: the request has fall through all middleware, set \"fallbackComponent\" as a catchall',\n request\n );\n\n // For clarity, we are returning `undefined` instead of `() => undefined`.\n return;\n }\n\n return Object.freeze({\n // Convert fallback component as renderer.\n [DO_NOT_CREATE_THIS_OBJECT_YOURSELF]: undefined,\n render: () => (\n // Currently, there are no ways to set `bindProps` to `fallbackComponent`.\n // `fallbackComponent` do not need `overridingProps` because it is the last one in the chain, it would not have the next() function.\n <ComponentWithProps component={fallbackComponent} />\n )\n });\n })(request);\n\n return (\n result &&\n ((props: Props) => {\n const renderCallbackProps = useMemoValueWithEquality<Props>(() => props, arePropsEqual);\n\n const context = useMemo<RenderContextType<Props>>(\n () => Object.freeze({ renderCallbackProps }),\n [renderCallbackProps]\n );\n\n return <RenderContext.Provider value={context}>{result.render()}</RenderContext.Provider>;\n })\n );\n },\n [enhancer]\n );\n };\n\n function ChainOfResponsibilityProvider({ children, init, middleware }: ProviderProps<Request, Props, Init>) {\n if (!Array.isArray(middleware) || middleware.some(middleware => typeof middleware !== 'function')) {\n throw new Error('react-chain-of-responsibility: \"middleware\" prop must be an array of functions');\n }\n\n // Remap the middleware, so all inputs/outputs are validated.\n const fortifiedMiddleware = useMemo(\n () =>\n Object.freeze(\n middleware.map<ComponentMiddleware<Request, Props, Init>>(fn => (init: Init) => {\n const enhancer = fn(init);\n\n return next => originalRequest => {\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(\n 'react-chain-of-responsibility: next() cannot be called after the function had returned synchronously'\n );\n }\n\n // We do not allow passing void/undefined to next() because it would be confusing whether to keep the original request or pass an undefined.\n !options.passModifiedRequest &&\n !Object.is(nextRequest, originalRequest) &&\n console.warn(\n 'react-chain-of-responsibility: next() must be called with the original request, otherwise, set \"options.passModifiedRequest\" to true to pass a different request object downstream'\n );\n\n return next(options.passModifiedRequest ? nextRequest : originalRequest);\n })(originalRequest);\n\n hasReturned = true;\n\n // Make sure the return value is built using our helper function for forward-compatibility reason.\n return returnValue && parse(componentHandlerResultSchema, returnValue);\n };\n })\n ),\n [middleware]\n );\n\n const { enhancer: parentEnhancer } = useContext(BuildContext);\n\n const enhancer = useMemo<ComponentEnhancer<Request, Props>>(\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<ComponentHandlerResult<Props> | undefined, Request, Init>(\n ...[...fortifiedMiddleware, ...[() => parentEnhancer]]\n )(init as Init),\n [init, fortifiedMiddleware, parentEnhancer]\n );\n\n const contextValue = useMemo<BuildContextType<Request, Props>>(() => Object.freeze({ enhancer }), [enhancer]);\n\n return <BuildContext.Provider value={contextValue}>{children}</BuildContext.Provider>;\n }\n\n function ChainOfResponsibilityProxy({ fallbackComponent, request, ...props }: ProxyProps<Request, Props>) {\n const result = useBuildRenderCallback()(request, { fallbackComponent })?.(props as Props);\n\n return result ? <Fragment>{result}</Fragment> : null;\n }\n\n const MemoizedChainOfResponsibilityProvider =\n memo<ProviderProps<Request, Props, Init>>(ChainOfResponsibilityProvider);\n\n return Object.freeze({\n Provider: MemoizedChainOfResponsibilityProvider as typeof MemoizedChainOfResponsibilityProvider &\n InferenceHelper<Request, Props, Init>,\n Proxy: memo<ProxyProps<Request, Props>>(ChainOfResponsibilityProxy),\n reactComponent,\n useBuildRenderCallback\n\n // TODO: Consider adding back `asMiddleware`.\n });\n}\n\nexport default createChainOfResponsibility;\nexport {\n type ChainOfResponsibility,\n type ComponentEnhancer,\n type ComponentHandler,\n type ComponentHandlerResult,\n type ComponentMiddleware,\n type ComponentRenderer,\n type CreateChainOfResponsibilityOptions,\n type InferenceHelper,\n type InferInit,\n type InferMiddleware,\n type InferProps,\n type InferProviderProps,\n type InferProxyProps,\n type InferRequest,\n type ProviderProps,\n type ProxyProps,\n type ReactComponentHandlerResult,\n type UseBuildRenderCallback,\n type UseBuildRenderCallbackOptions\n};\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport default function arePropsEqual<T extends Record<string, any>>(x: T, y: T): boolean {\n if (Object.is(x, y)) {\n return true;\n }\n\n const mapOfX = new Map(Object.entries(x));\n const mapOfY = new Map(Object.entries(y));\n\n if (mapOfX.size !== mapOfY.size) {\n return false;\n }\n\n const keys = new Set([...mapOfX.keys(), ...mapOfY.keys()]);\n\n for (const key of keys) {\n if (!Object.is(mapOfX.get(key), mapOfY.get(key))) {\n return false;\n }\n }\n\n return true;\n}\n","import { useEffect, useRef } from 'react';\n\nconst NOT_INITIALIZED = Symbol();\n\nexport default function useMemoValueWithEquality<T>(factory: () => T, equalityFn: (x: T, y: T) => boolean): T {\n const prevRef = useRef<T | typeof NOT_INITIALIZED>(NOT_INITIALIZED);\n const next: T = factory();\n const current: T = prevRef.current !== NOT_INITIALIZED && equalityFn(prevRef.current, next) ? prevRef.current : next;\n\n useEffect(() => {\n prevRef.current = current;\n });\n\n return current;\n}\n"],"mappings":";AAAA,SAAS,uBAAuB;AAChC,OAAO;AAAA,EACL;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AACP,SAAS,QAAQ,WAAW,QAAQ,OAAO,iBAAiB;;;ACX7C,SAAR,cAA8D,GAAM,GAAe;AACxF,MAAI,OAAO,GAAG,GAAG,CAAC,GAAG;AACnB,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,IAAI,IAAI,OAAO,QAAQ,CAAC,CAAC;AACxC,QAAM,SAAS,IAAI,IAAI,OAAO,QAAQ,CAAC,CAAC;AAExC,MAAI,OAAO,SAAS,OAAO,MAAM;AAC/B,WAAO;AAAA,EACT;AAEA,QAAM,OAAO,oBAAI,IAAI,CAAC,GAAG,OAAO,KAAK,GAAG,GAAG,OAAO,KAAK,CAAC,CAAC;AAEzD,aAAW,OAAO,MAAM;AACtB,QAAI,CAAC,OAAO,GAAG,OAAO,IAAI,GAAG,GAAG,OAAO,IAAI,GAAG,CAAC,GAAG;AAChD,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;;;ACtBA,SAAS,WAAW,cAAc;AAElC,IAAM,kBAAkB,OAAO;AAEhB,SAAR,yBAA6C,SAAkB,YAAwC;AAC5G,QAAM,UAAU,OAAmC,eAAe;AAClE,QAAM,OAAU,QAAQ;AACxB,QAAM,UAAa,QAAQ,YAAY,mBAAmB,WAAW,QAAQ,SAAS,IAAI,IAAI,QAAQ,UAAU;AAEhH,YAAU,MAAM;AACd,YAAQ,UAAU;AAAA,EACpB,CAAC;AAED,SAAO;AACT;;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GED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-chain-of-responsibility",
|
|
3
|
-
"version": "0.4.0-main.
|
|
3
|
+
"version": "0.4.0-main.feea005",
|
|
4
4
|
"description": "Using chain of responsibility design pattern for compositing and customizing React component.",
|
|
5
5
|
"files": [
|
|
6
6
|
"./dist/"
|
|
@@ -143,7 +143,7 @@
|
|
|
143
143
|
},
|
|
144
144
|
"dependencies": {
|
|
145
145
|
"handler-chain": "^0.1.0",
|
|
146
|
-
"react-chain-of-responsibility": "^0.4.0-main.
|
|
146
|
+
"react-chain-of-responsibility": "^0.4.0-main.feea005",
|
|
147
147
|
"valibot": "^1.1.0"
|
|
148
148
|
}
|
|
149
149
|
}
|