react-remux
Version:
7 lines (6 loc) • 2.04 kB
JavaScript
/*!
* react-remux.js v0.0.4
* (c) 2018-2019 空鱼
* Released under the MIT License.
*/
import{connect as e,Provider as t,createStoreHook as r,useDispatch as o,createSelectorHook as s,shallowEqual as n,createDispatchHook as c}from"react-redux";export{shallowEqual}from"react-redux";import{globalStore as a,subscribeOtherStorePlugin as u,createStore as m}from"remux-core";export*from"remux-core";import{invariant as p}from"rmagix-lib";import i,{createContext as g}from"react";const{ACTION_REFRESH:l,subscribe:d}=u,x=(e,t)=>{const r=(e=>{const t=[];return e(new Proxy({},{get(e,r){t.push(r)}})),t})(e),o=(e=>{const t=Object.keys(e)[0];return a.get(t)})(t),s=o._namespace,n=o.getDispatch(),c=r.filter(e=>!(e.includes("loading")||e===s)),u=a.getAll();Object.keys(u).filter(e=>c.includes(e)).forEach(e=>{d(u[e],()=>{n({type:l})},s)})},f=e=>{return(t,...r)=>{x(e,t);const o=a.getStates();return e(o,...r)}},h=(...e)=>{p(!1,"当前react-redux版本过低,如果想使用hooks,请升级到react-redux@^7.1.1 redux@^4.0.4")};function b(t,...r){const o=f(t);return e(o,...r)}class y extends i.Component{constructor(e){super(e);const{model:t,...r}=e,{namespace:o}=t;p(o,`namespace except get string,but get ${typeof o}`),this.state={store:m({model:t,...r})}}componentWillUnmount(){this.state.store.unmount()}render(){return i.createElement(t,{store:this.state.store.getStore()},this.props.children)}}const S=e=>{const t=void 0===e,o=t?a.getAll():a.get(e),s=(g||h)({store:o});return r(t?void 0:s)()},E=o||h,k=(e,t)=>{return(s||h)()(f(e),t)},v=(e,t=n)=>{return(s||h)()(f(e),t)},A=(e,t,r={})=>{const{namespace:o}=e;p(o,`namespace except get string,but get ${typeof o}`);const{useHooks:u,...i}=r;a.getManager().has(e.namespace)||m({model:e,...i});const l=a.get(e.namespace).getStore(),d=(g||h)({store:l,storeState:l.getState()});return{state:((e,t=n)=>{return s(d)(f(e),t)})(t||function(t){return t[e.namespace]}),dispatch:c(d)()}};export{y as Provider,b as connect,E as useDispatch,A as useModel,k as useSelector,v as useSelectorSE,S as useStore};