{"version":3,"file":"keyed-reducer.mjs","names":[],"sources":["../../src/redux-store/keyed-reducer.ts"],"sourcesContent":["import type { AnyAction, Reducer } from 'redux';\n\n/**\n * Higher-order reducer creator which creates a combined reducer object, keyed\n * by a property on the action object.\n *\n * @param actionProperty Action property by which to key object.\n * @return Higher-order reducer.\n *\n * @example\n * ```js\n * import { keyedReducer } from '@wordpress/data';\n *\n * const itemsByContext = keyedReducer( 'context' )( ( state = [], action ) => {\n *   switch ( action.type ) {\n *     case 'ADD_ITEM':\n *       return [ ...state, action.item ];\n *   }\n *   return state;\n * } );\n * ```\n */\nexport const keyedReducer =\n\t< TState extends unknown, TAction extends AnyAction >(\n\t\tactionProperty: string\n\t) =>\n\t(\n\t\treducer: Reducer< TState, TAction >\n\t): Reducer< Record< string, TState >, TAction > =>\n\t( state: Record< string, TState > = {}, action ) => {\n\t\t// Retrieve subkey from action. Do not track if undefined; useful for cases\n\t\t// where reducer is scoped by action shape.\n\t\tconst key = action[ actionProperty ];\n\t\tif ( key === undefined ) {\n\t\t\treturn state;\n\t\t}\n\n\t\t// Avoid updating state if unchanged. Note that this also accounts for a\n\t\t// reducer which returns undefined on a key which is not yet tracked.\n\t\tconst nextKeyState = reducer( state[ key ], action );\n\t\tif ( nextKeyState === state[ key ] ) {\n\t\t\treturn state;\n\t\t}\n\n\t\treturn {\n\t\t\t...state,\n\t\t\t[ key ]: nextKeyState,\n\t\t};\n\t};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAsBA,MAAa,gBAEX,oBAGA,aAEC,QAAkC,CAAC,GAAG,WAAY;CAGnD,MAAM,MAAM,OAAQ;CACpB,IAAK,QAAQ,KAAA,GACZ,OAAO;CAKR,MAAM,eAAe,QAAS,MAAO,MAAO,MAAO;CACnD,IAAK,iBAAiB,MAAO,MAC5B,OAAO;CAGR,OAAO;EACN,GAAG;GACD,MAAO;CACV;AACD"}