2019-01-12 11:33:27 +08:00
|
|
|
import shallowEqual from 'shallowequal';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import { getOptionProps } from '../props-util';
|
|
|
|
import PropTypes from '../vue-types';
|
|
|
|
import proxyComponent from '../proxyComponent';
|
2018-03-25 18:07:04 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getDisplayName(WrappedComponent) {
|
|
|
|
return WrappedComponent.name || 'Component';
|
2018-03-25 18:07:04 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const defaultMapStateToProps = () => ({});
|
|
|
|
export default function connect(mapStateToProps) {
|
|
|
|
const shouldSubscribe = !!mapStateToProps;
|
|
|
|
const finnalMapStateToProps = mapStateToProps || defaultMapStateToProps;
|
|
|
|
return function wrapWithConnect(WrappedComponent) {
|
|
|
|
const tempProps = omit(WrappedComponent.props || {}, ['store']);
|
2018-09-29 15:07:37 +08:00
|
|
|
const props = {
|
|
|
|
__propsSymbol__: PropTypes.any,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
Object.keys(tempProps).forEach(k => {
|
|
|
|
props[k] = { ...k, required: false };
|
|
|
|
});
|
2018-03-25 18:07:04 +08:00
|
|
|
const Connect = {
|
|
|
|
name: `Connect_${getDisplayName(WrappedComponent)}`,
|
|
|
|
props,
|
|
|
|
inject: {
|
2019-01-12 11:33:27 +08:00
|
|
|
storeContext: { default: {} },
|
2018-03-25 18:07:04 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
|
|
|
this.store = this.storeContext.store;
|
|
|
|
this.preProps = omit(getOptionProps(this), ['__propsSymbol__']);
|
2018-03-25 18:07:04 +08:00
|
|
|
return {
|
|
|
|
subscribed: finnalMapStateToProps(this.store.getState(), this.$props),
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
|
|
|
watch: {
|
2019-01-12 11:33:27 +08:00
|
|
|
__propsSymbol__() {
|
2018-09-29 15:07:37 +08:00
|
|
|
if (mapStateToProps && mapStateToProps.length === 2) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.subscribed = finnalMapStateToProps(this.store.getState(), this.$props);
|
2018-09-29 15:07:37 +08:00
|
|
|
}
|
|
|
|
},
|
2018-03-25 18:07:04 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
mounted() {
|
|
|
|
this.trySubscribe();
|
2018-03-25 18:07:04 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
beforeDestroy() {
|
|
|
|
this.tryUnsubscribe();
|
2018-03-25 18:07:04 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
handleChange() {
|
2018-03-25 18:07:04 +08:00
|
|
|
if (!this.unsubscribe) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-03-25 18:07:04 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const props = omit(getOptionProps(this), ['__propsSymbol__']);
|
|
|
|
const nextSubscribed = finnalMapStateToProps(this.store.getState(), props);
|
|
|
|
if (
|
|
|
|
!shallowEqual(this.preProps, props) ||
|
|
|
|
!shallowEqual(this.subscribed, nextSubscribed)
|
|
|
|
) {
|
|
|
|
this.subscribed = nextSubscribed;
|
2018-03-25 18:07:04 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
trySubscribe() {
|
2018-03-25 18:07:04 +08:00
|
|
|
if (shouldSubscribe) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.unsubscribe = this.store.subscribe(this.handleChange);
|
|
|
|
this.handleChange();
|
2018-03-25 18:07:04 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
tryUnsubscribe() {
|
2018-03-25 18:07:04 +08:00
|
|
|
if (this.unsubscribe) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.unsubscribe();
|
|
|
|
this.unsubscribe = null;
|
2018-03-25 18:07:04 +08:00
|
|
|
}
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
getWrappedInstance() {
|
|
|
|
return this.$refs.wrappedInstance;
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2018-03-25 18:07:04 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
|
|
|
this.preProps = { ...this.$props };
|
|
|
|
const { $listeners, $slots = {}, $attrs, $scopedSlots, subscribed, store } = this;
|
|
|
|
const props = getOptionProps(this);
|
|
|
|
this.preProps = { ...omit(props, ['__propsSymbol__']) };
|
2018-03-25 18:07:04 +08:00
|
|
|
const wrapProps = {
|
|
|
|
props: {
|
|
|
|
...props,
|
|
|
|
...subscribed,
|
|
|
|
store,
|
|
|
|
},
|
|
|
|
on: $listeners,
|
|
|
|
attrs: $attrs,
|
|
|
|
scopedSlots: $scopedSlots,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-25 18:07:04 +08:00
|
|
|
return (
|
2019-01-12 11:33:27 +08:00
|
|
|
<WrappedComponent {...wrapProps} ref="wrappedInstance">
|
2018-09-05 21:28:54 +08:00
|
|
|
{Object.keys($slots).map(name => {
|
2019-01-12 11:33:27 +08:00
|
|
|
return <template slot={name}>{$slots[name]}</template>;
|
2018-09-05 21:28:54 +08:00
|
|
|
})}
|
|
|
|
</WrappedComponent>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-25 18:07:04 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
return proxyComponent(Connect);
|
|
|
|
};
|
2018-03-25 18:07:04 +08:00
|
|
|
}
|