refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
1 lines • 1.45 kB
Source Map (JSON)
{"version":3,"sources":["map-with-props-memo.ts"],"names":["useRef","EMPTY_OBJECT","shallowEqualByKeys","mapWithPropsMemo","getFn","watchKeys","props","prevProps","prevResult","current"],"mappings":"AAAA,SAASA,MAAT,QAAuB,OAAvB;AACA,SAASC,YAAT,QAA6B,MAA7B;AAEA,SAASC,kBAAT,QAAmC,SAAnC;AAEA,OAAO,IAAMC,gBAAgB,GAAG,SAAnBA,gBAAmB,CAA6BC,KAA7B,EAAqDC,SAArD;AAAA,SAAgF,UAACC,KAAD,EAA6B;AAC3I,QAAMC,SAAS,GAAGP,MAAM,CAAIC,YAAJ,CAAxB;AACA,QAAMO,UAAU,GAAGR,MAAM,CAAIC,YAAJ,CAAzB;;AAEA,QAAIO,UAAU,CAACC,OAAX,KAAuBR,YAAvB,IAAuC,CAACC,kBAAkB,CAACK,SAAS,CAACE,OAAX,EAAoBH,KAApB,EAA2BD,SAA3B,CAA9D,EAAqG;AACnGE,MAAAA,SAAS,CAACE,OAAV,GAAoBH,KAApB;AACAE,MAAAA,UAAU,CAACC,OAAX,GAAqBL,KAAK,CAACE,KAAD,CAA1B;AACD;;AAED,6BACKA,KADL,EAEKE,UAAU,CAACC,OAFhB;AAID,GAb+B;AAAA,CAAzB","sourcesContent":["import { useRef } from 'react'\nimport { EMPTY_OBJECT } from 'tsfn'\nimport type { TExtend } from 'tsfn'\nimport { shallowEqualByKeys } from './utils'\n\nexport const mapWithPropsMemo = <P extends {}, R extends {}>(getFn: (props: P) => R, watchKeys: (keyof P)[]) => (props: P): TExtend<P, R> => {\n const prevProps = useRef<P>(EMPTY_OBJECT)\n const prevResult = useRef<R>(EMPTY_OBJECT)\n\n if (prevResult.current === EMPTY_OBJECT || !shallowEqualByKeys(prevProps.current, props, watchKeys)) {\n prevProps.current = props\n prevResult.current = getFn(props)\n }\n\n return {\n ...props,\n ...prevResult.current,\n }\n}\n"],"file":"map-with-props-memo.js"}