2018-03-25 18:07:04 +08:00
|
|
|
import shallowEqual from 'shallowequal'
|
|
|
|
import omit from 'omit.js'
|
|
|
|
import { getOptionProps } from '../props-util'
|
|
|
|
import PropTypes from '../vue-types'
|
2018-09-29 15:07:37 +08:00
|
|
|
import proxyComponent from '../proxyComponent'
|
2018-03-25 18:07:04 +08:00
|
|
|
|
|
|
|
function getDisplayName (WrappedComponent) {
|
|
|
|
return WrappedComponent.name || 'Component'
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
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: {
|
2018-09-05 21:28:54 +08:00
|
|
|
storeContext: { default: {}},
|
2018-03-25 18:07:04 +08:00
|
|
|
},
|
|
|
|
data () {
|
2018-09-05 21:28:54 +08:00
|
|
|
this.store = this.storeContext.store
|
2018-12-19 12:22:55 +08:00
|
|
|
this.preProps = omit(getOptionProps(this), ['__propsSymbol__'])
|
2018-03-25 18:07:04 +08:00
|
|
|
return {
|
|
|
|
subscribed: finnalMapStateToProps(this.store.getState(), this.$props),
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
|
|
|
watch: {
|
2018-09-29 15:07:37 +08:00
|
|
|
__propsSymbol__ () {
|
|
|
|
if (mapStateToProps && mapStateToProps.length === 2) {
|
|
|
|
this.subscribed = finnalMapStateToProps(this.store.getState(), this.$props)
|
|
|
|
}
|
|
|
|
},
|
2018-03-25 18:07:04 +08:00
|
|
|
},
|
|
|
|
mounted () {
|
|
|
|
this.trySubscribe()
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeDestroy () {
|
|
|
|
this.tryUnsubscribe()
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
handleChange () {
|
|
|
|
if (!this.unsubscribe) {
|
|
|
|
return
|
|
|
|
}
|
2018-12-19 12:22:55 +08:00
|
|
|
const props = omit(getOptionProps(this), ['__propsSymbol__'])
|
2018-11-06 10:05:23 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
trySubscribe () {
|
|
|
|
if (shouldSubscribe) {
|
|
|
|
this.unsubscribe = this.store.subscribe(this.handleChange)
|
|
|
|
this.handleChange()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
tryUnsubscribe () {
|
|
|
|
if (this.unsubscribe) {
|
|
|
|
this.unsubscribe()
|
|
|
|
this.unsubscribe = null
|
|
|
|
}
|
|
|
|
},
|
2018-09-05 21:28:54 +08:00
|
|
|
getWrappedInstance () {
|
|
|
|
return this.$refs.wrappedInstance
|
|
|
|
},
|
2018-03-25 18:07:04 +08:00
|
|
|
},
|
|
|
|
render () {
|
2018-11-06 10:05:23 +08:00
|
|
|
this.preProps = { ...this.$props }
|
2018-09-05 21:28:54 +08:00
|
|
|
const { $listeners, $slots = {}, $attrs, $scopedSlots, subscribed, store } = this
|
2018-03-25 18:07:04 +08:00
|
|
|
const props = getOptionProps(this)
|
2018-11-06 10:05:23 +08:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
return (
|
2018-09-05 21:28:54 +08:00
|
|
|
<WrappedComponent {...wrapProps} ref='wrappedInstance'>
|
|
|
|
{Object.keys($slots).map(name => {
|
|
|
|
return <template slot={name}>{$slots[name]}</template>
|
|
|
|
})}
|
|
|
|
</WrappedComponent>
|
2018-03-25 18:07:04 +08:00
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
2018-09-29 15:07:37 +08:00
|
|
|
return proxyComponent(Connect)
|
2018-03-25 18:07:04 +08:00
|
|
|
}
|
|
|
|
}
|