UNPKG

refun

Version:

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

1 lines • 1.75 kB
{"version":3,"sources":["map-handlers.ts"],"names":["mapHandlers","handlers","props","actualHandlers","EMPTY_OBJECT","propsRef","current","reduce","result","key"],"mappings":";;;;;;;;;AAAA;;AACA;;AAWO,IAAMA,WAAW,GAAG,SAAdA,WAAc,CAAwCC,QAAxC;AAAA,SACzB,UAACC,KAAD,EAA8C;AAC5C,QAAMC,cAAc,GAAG,mBAA2BC,kBAA3B,CAAvB;AACA,QAAMC,QAAQ,GAAG,mBAAUH,KAAV,CAAjB;AAEAG,IAAAA,QAAQ,CAACC,OAAT,GAAmBJ,KAAnB;;AAEA,QAAIC,cAAc,CAACG,OAAf,KAA2BF,kBAA/B,EAA6C;AAC3CD,MAAAA,cAAc,CAACG,OAAf,GAAyB,yBAAcL,QAAd,EAAwBM,MAAxB,CAA+B,UAACC,MAAD,EAASC,GAAT,EAAiB;AACvED,QAAAA,MAAM,CAACC,GAAD,CAAN,GAAc;AAAA,iBAAaR,QAAQ,CAACQ,GAAD,CAAR,CAAcJ,QAAQ,CAACC,OAAvB,0BAAb;AAAA,SAAd;;AAEA,eAAOE,MAAP;AACD,OAJwB,EAItB,EAJsB,CAAzB;AAKD;;AAED,sCACKN,KADL,EAEKC,cAAc,CAACG,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"}