UNPKG

refun

Version:

A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:

1 lines 1.84 kB
{"version":3,"sources":["map-handlers.ts"],"names":["useRef","getObjectKeys","EMPTY_OBJECT","mapHandlers","handlers","props","actualHandlers","propsRef","current","reduce","result","key"],"mappings":"AAAA,SAASA,MAAT,QAAuB,OAAvB;AACA,SAASC,aAAT,EAAwBC,YAAxB,QAA4C,MAA5C;AAWA,OAAO,IAAMC,WAAW,GAAG,SAAdA,WAAc,CAAwCC,QAAxC;AAAA,SACzB,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;AAC3CI,MAAAA,cAAc,CAACE,OAAf,GAAyBP,aAAa,CAACG,QAAD,CAAb,CAAwBK,MAAxB,CAA+B,UAACC,MAAD,EAASC,GAAT,EAAiB;AACvED,QAAAA,MAAM,CAACC,GAAD,CAAN,GAAc;AAAA,iBAAaP,QAAQ,CAACO,GAAD,CAAR,CAAcJ,QAAQ,CAACC,OAAvB,0BAAb;AAAA,SAAd;;AAEA,eAAOE,MAAP;AACD,OAJwB,EAItB,EAJsB,CAAzB;AAKD;;AAED,6BACKL,KADL,EAEKC,cAAc,CAACE,OAFpB;AAID,GAnBwB;AAAA,CAApB","sourcesContent":["import { useRef } from 'react'\nimport { getObjectKeys, EMPTY_OBJECT } from 'tsfn'\nimport type { TExtend } from 'tsfn'\n\nexport type TActualHandlers <T> = {\n [key in keyof T]: (...args: any[]) => void\n}\n\nexport type THandlers <P> = {\n [key: string]: (props: P) => (...args: any[]) => void,\n}\n\nexport const mapHandlers = <P extends {}, R extends THandlers<P>> (handlers: 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 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.js"}