UNPKG

refun

Version:

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

1 lines 3.14 kB
{"version":3,"sources":["redux-state-factory.ts"],"names":["useContext","useRef","useEffect","useState","EMPTY_OBJECT","EMPTY_ARRAY","NOOP","shallowEqualByKeys","ReduxStateFactory","context","mapStateToProps","stateKeysToWatch","props","rerender","prevStateRef","prevStatePropsRef","store","onMountRef","current","newState","getState","subscribe"],"mappings":";AAAA,SAASA,UAAT,EAAqBC,MAArB,EAA6BC,SAA7B,EAAwCC,QAAxC,QAAwD,OAAxD;AAGA,SAASC,YAAT,EAAuBC,WAAvB,EAAoCC,IAApC,QAAgD,MAAhD;AAEA,SAASC,kBAAT,QAAmC,SAAnC;AAEA,OAAO,IAAMC,iBAAiB,GAAG,SAApBA,iBAAoB,CAAIC,OAAJ;AAAA,SAAmC,UAA8BC,eAA9B,EAAiEC,gBAAjE;AAAA,WAClE,UAACC,KAAD,EAA8B;AAAA,sBACPT,QAAQ,CAACC,YAAD,CADD;AAAA;AAAA,UACnBS,QADmB;;AAE5B,UAAMC,YAAY,GAAGb,MAAM,CAAIG,YAAJ,CAA3B;AACA,UAAMW,iBAAiB,GAAGd,MAAM,CAAKG,YAAL,CAAhC;AACA,UAAMY,KAAK,GAAGhB,UAAU,CAACS,OAAD,CAAxB;AACA,UAAMQ,UAAU,GAAGhB,MAAM,CAAaK,IAAb,CAAzB;;AAEA,UAAIS,iBAAiB,CAACG,OAAlB,KAA8Bd,YAAlC,EAAgD;AAC9C,YAAMe,QAAQ,GAAGH,KAAK,CAACI,QAAN,EAAjB;AAEAN,QAAAA,YAAY,CAACI,OAAb,GAAuBC,QAAvB;AACAJ,QAAAA,iBAAiB,CAACG,OAAlB,GAA4BR,eAAe,CAACS,QAAD,CAA3C;AACD;;AAED,UAAIF,UAAU,CAACC,OAAX,KAAuBZ,IAA3B,EAAiC;AAC/BW,QAAAA,UAAU,CAACC,OAAX,GAAqB;AAAA,iBAAMF,KAAK,CAACK,SAAN,CAAgB,YAAM;AAC/C,gBAAMF,QAAQ,GAAGH,KAAK,CAACI,QAAN,EAAjB;;AAEA,gBAAI,CAACb,kBAAkB,CAACO,YAAY,CAACI,OAAd,EAAuBC,QAAvB,EAAiCR,gBAAjC,CAAvB,EAA2E;AACzEI,cAAAA,iBAAiB,CAACG,OAAlB,GAA4BR,eAAe,CAACS,QAAD,CAA3C;AAEAN,cAAAA,QAAQ,CAAC,EAAD,CAAR;AACD;;AAEDC,YAAAA,YAAY,CAACI,OAAb,GAAuBC,QAAvB;AACD,WAV0B,CAAN;AAAA,SAArB;AAWD;;AAEDjB,MAAAA,SAAS,CAACe,UAAU,CAACC,OAAZ,EAAqBb,WAArB,CAAT;AAEA,+BACKO,KADL,EAEKG,iBAAiB,CAACG,OAFvB;AAID,KAnCiE;AAAA,GAAnC;AAAA,CAA1B","sourcesContent":["import { useContext, useRef, useEffect, useState } from 'react'\nimport type { Context } from 'react'\nimport type { Store } from 'redux'\nimport { EMPTY_OBJECT, EMPTY_ARRAY, NOOP } from 'tsfn'\nimport type { TExtend } from 'tsfn'\nimport { shallowEqualByKeys } from './utils'\n\nexport const ReduxStateFactory = <S>(context: Context<Store<S>>) => <P extends {}, SP extends {}>(mapStateToProps: (state: S) => SP, stateKeysToWatch: (keyof S)[]) =>\n (props: P): TExtend<P, SP> => {\n const [, rerender] = useState(EMPTY_OBJECT)\n const prevStateRef = useRef<S>(EMPTY_OBJECT)\n const prevStatePropsRef = useRef<SP>(EMPTY_OBJECT)\n const store = useContext(context)\n const onMountRef = useRef<() => void>(NOOP)\n\n if (prevStatePropsRef.current === EMPTY_OBJECT) {\n const newState = store.getState()\n\n prevStateRef.current = newState\n prevStatePropsRef.current = mapStateToProps(newState)\n }\n\n if (onMountRef.current === NOOP) {\n onMountRef.current = () => store.subscribe(() => {\n const newState = store.getState()\n\n if (!shallowEqualByKeys(prevStateRef.current, newState, stateKeysToWatch)) {\n prevStatePropsRef.current = mapStateToProps(newState)\n\n rerender({})\n }\n\n prevStateRef.current = newState\n })\n }\n\n useEffect(onMountRef.current, EMPTY_ARRAY)\n\n return {\n ...props,\n ...prevStatePropsRef.current,\n }\n }\n"],"file":"redux-state-factory.js"}