refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
1 lines • 1.88 kB
Source Map (JSON)
{"version":3,"sources":["map-handlers-factory.ts"],"names":["useRef","getObjectKeys","EMPTY_OBJECT","mapHandlersFactory","getHandlers","props","actualHandlers","propsRef","current","handlers","reduce","result","key"],"mappings":"AAAA,SAASA,MAAT,QAAuB,OAAvB;AACA,SAASC,aAAT,EAAwBC,YAAxB,QAA4C,MAA5C;AAIA,OAAO,IAAMC,kBAAkB,GAAG,SAArBA,kBAAqB,CAAwCC,WAAxC;AAAA,SAChC,UAACC,KAAD,EAA8C;AAC5C,QAAMC,cAAc,GAAGN,MAAM,CAAqBE,YAArB,CAA7B;AACA,QAAMK,QAAQ,GAAGP,MAAM,CAAIK,KAAJ,CAAvB;AAEAE,IAAAA,QAAQ,CAACC,OAAT,GAAmBH,KAAnB;;AAEA,QAAIC,cAAc,CAACE,OAAf,KAA2BN,YAA/B,EAA6C;AAC3C,UAAMO,QAAQ,GAAGL,WAAW,CAACC,KAAD,CAA5B;AAEAC,MAAAA,cAAc,CAACE,OAAf,GAAyBP,aAAa,CAACQ,QAAD,CAAb,CAAwBC,MAAxB,CAA+B,UAACC,MAAD,EAASC,GAAT,EAAiB;AACvED,QAAAA,MAAM,CAACC,GAAD,CAAN,GAAc;AAAA,iBAAaH,QAAQ,CAACG,GAAD,CAAR,CAAcL,QAAQ,CAACC,OAAvB,0BAAb;AAAA,SAAd;;AAEA,eAAOG,MAAP;AACD,OAJwB,EAItB,EAJsB,CAAzB;AAKD;;AAED,6BACKN,KADL,EAEKC,cAAc,CAACE,OAFpB;AAID,GArB+B;AAAA,CAA3B","sourcesContent":["import { useRef } from 'react'\nimport { getObjectKeys, EMPTY_OBJECT } from 'tsfn'\nimport type { TExtend } from 'tsfn'\nimport type { THandlers, TActualHandlers } from './map-handlers'\n\nexport const mapHandlersFactory = <P extends {}, R extends THandlers<P>> (getHandlers: (props: P) => R) =>\n (props: P): TExtend<P, TActualHandlers<R>> => {\n const actualHandlers = useRef<TActualHandlers<R>>(EMPTY_OBJECT)\n const propsRef = useRef<P>(props)\n\n propsRef.current = props\n\n if (actualHandlers.current === EMPTY_OBJECT) {\n const handlers = getHandlers(props)\n\n actualHandlers.current = getObjectKeys(handlers).reduce((result, key) => {\n result[key] = (...args) => handlers[key](propsRef.current)(...args)\n\n return result\n }, {} as TActualHandlers<R>)\n }\n\n return {\n ...props,\n ...actualHandlers.current,\n }\n }\n"],"file":"map-handlers-factory.js"}