@wordpress/data
Version:
Data module for WordPress.
8 lines (7 loc) • 2.85 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/use-dispatch/use-dispatch-with-map.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useMemo, useRef } from '@wordpress/element';\nimport { useIsomorphicLayoutEffect } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport useRegistry from '../registry-provider/use-registry';\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 {Function} 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 {Array} deps An array of dependencies for the hook.\n * @return {Object} An object mapping props to functions created by the passed\n * in dispatchMap.\n */\nconst useDispatchWithMap = ( dispatchMap, deps ) => {\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 ) =>\n\t\t\t\t\t\t\tcurrentDispatchMapRef\n\t\t\t\t\t\t\t\t.current( registry.dispatch, registry )\n\t\t\t\t\t\t\t\t[ propName ]( ...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": ";AAGA,SAAS,SAAS,cAAc;AAChC,SAAS,iCAAiC;AAK1C,OAAO,iBAAiB;AAgBxB,IAAM,qBAAqB,CAAE,aAAa,SAAU;AACnD,QAAM,WAAW,YAAY;AAC7B,QAAM,wBAAwB,OAAQ,WAAY;AAElD,4BAA2B,MAAM;AAChC,0BAAsB,UAAU;AAAA,EACjC,CAAE;AAEF,SAAO,QAAS,MAAM;AACrB,UAAM,uBAAuB,sBAAsB;AAAA,MAClD,SAAS;AAAA,MACT;AAAA,IACD;AACA,WAAO,OAAO;AAAA,MACb,OAAO,QAAS,oBAAqB,EAAE;AAAA,QACtC,CAAE,CAAE,UAAU,UAAW,MAAO;AAC/B,cAAK,OAAO,eAAe,YAAa;AAEvC,oBAAQ;AAAA,cACP,YAAa,QAAS;AAAA,YACvB;AAAA,UACD;AACA,iBAAO;AAAA,YACN;AAAA,YACA,IAAK,SACJ,sBACE,QAAS,SAAS,UAAU,QAAS,EACpC,QAAS,EAAG,GAAG,IAAK;AAAA,UACzB;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD,GAAG,CAAE,UAAU,GAAG,IAAK,CAAE;AAC1B;AAEA,IAAO,gCAAQ;",
"names": []
}