{"version":3,"file":"use-dispatch-with-map.mjs","names":[],"sources":["../../../src/components/use-dispatch/use-dispatch-with-map.ts"],"sourcesContent":["import { useMemo, useRef } from '@wordpress/element';\nimport { useIsomorphicLayoutEffect } from '@wordpress/compose';\nimport useRegistry from '../registry-provider/use-registry';\nimport type { DataRegistry } from '../../types';\n\ntype DispatchMap = (\n\tdispatch: DataRegistry[ 'dispatch' ],\n\tregistry: DataRegistry\n) => Record< string, ( ...args: never[] ) => unknown >;\n\n/**\n * Custom react hook for returning aggregate dispatch actions using the provided\n * dispatchMap.\n *\n * Currently this is an internal api only and is implemented by `withDispatch`\n *\n * @param dispatchMap Receives the `registry.dispatch` function as\n *                    the first argument and the `registry` object\n *                    as the second argument.  Should return an\n *                    object mapping props to functions.\n * @param deps        An array of dependencies for the hook.\n * @return An object mapping props to functions created by the passed\n *         in dispatchMap.\n */\nconst useDispatchWithMap = (\n\tdispatchMap: DispatchMap,\n\tdeps: unknown[]\n): Record< string, ( ...args: unknown[] ) => unknown > => {\n\tconst registry = useRegistry();\n\tconst currentDispatchMapRef = useRef( dispatchMap );\n\n\tuseIsomorphicLayoutEffect( () => {\n\t\tcurrentDispatchMapRef.current = dispatchMap;\n\t} );\n\n\treturn useMemo( () => {\n\t\tconst currentDispatchProps = currentDispatchMapRef.current(\n\t\t\tregistry.dispatch,\n\t\t\tregistry\n\t\t);\n\t\treturn Object.fromEntries(\n\t\t\tObject.entries( currentDispatchProps ).map(\n\t\t\t\t( [ propName, dispatcher ] ) => {\n\t\t\t\t\tif ( typeof dispatcher !== 'function' ) {\n\t\t\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t`Property ${ propName } returned from dispatchMap in useDispatchWithMap must be a function.`\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn [\n\t\t\t\t\t\tpropName,\n\t\t\t\t\t\t( ...args: unknown[] ) =>\n\t\t\t\t\t\t\t// The arguments are passed through to the dispatcher.\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\tcurrentDispatchMapRef.current(\n\t\t\t\t\t\t\t\t\tregistry.dispatch,\n\t\t\t\t\t\t\t\t\tregistry\n\t\t\t\t\t\t\t\t)[ propName ] as (\n\t\t\t\t\t\t\t\t\t...args: unknown[]\n\t\t\t\t\t\t\t\t) => unknown\n\t\t\t\t\t\t\t )( ...args ),\n\t\t\t\t\t];\n\t\t\t\t}\n\t\t\t)\n\t\t);\n\t}, [ registry, ...deps ] );\n};\n\nexport default useDispatchWithMap;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAwBA,MAAM,sBACL,aACA,SACyD;CACzD,MAAM,WAAW,YAAY;CAC7B,MAAM,wBAAwB,OAAQ,WAAY;CAElD,gCAAiC;EAChC,sBAAsB,UAAU;CACjC,CAAE;CAEF,OAAO,cAAe;EACrB,MAAM,uBAAuB,sBAAsB,QAClD,SAAS,UACT,QACD;EACA,OAAO,OAAO,YACb,OAAO,QAAS,oBAAqB,CAAC,CAAC,KACpC,CAAE,UAAU,gBAAkB;GAC/B,IAAK,OAAO,eAAe,YAE1B,QAAQ,KACP,YAAa,SAAU,qEACxB;GAED,OAAO,CACN,WACE,GAAG,SAGH,sBAAsB,QACrB,SAAS,UACT,QACD,CAAC,CAAE,SAAU,CAGV,GAAG,IAAK,CACd;EACD,CACD,CACD;CACD,GAAG,CAAE,UAAU,GAAG,IAAK,CAAE;AAC1B"}