From 242d9e20c44e356bbda7a8334935cf14ac39856e Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Fri, 5 Jan 2018 15:42:37 +0100 Subject: [PATCH] chore(addSubscriptions): move into own module --- src/common/add-subscriptions.js | 28 ++++++++++++++++++++ src/common/utils.js | 46 +++------------------------------ 2 files changed, 32 insertions(+), 42 deletions(-) create mode 100644 src/common/add-subscriptions.js diff --git a/src/common/add-subscriptions.js b/src/common/add-subscriptions.js new file mode 100644 index 000000000..ec7e897cd --- /dev/null +++ b/src/common/add-subscriptions.js @@ -0,0 +1,28 @@ +import map from 'lodash/map' +import React from 'react' + +const call = fn => fn() + +// `subscriptions` can be a function if we want to ensure that the subscription +// callbacks have been correctly initialized when there are circular dependencies +export const addSubscriptions = subscriptions => Component => + class SubscriptionWrapper extends React.PureComponent { + _unsubscribes = null + + componentWillMount () { + this._unsubscribes = map( + typeof subscriptions === 'function' ? subscriptions(this.props) : subscriptions, + (subscribe, prop) => + subscribe(value => this.setState({ [prop]: value })) + ) + } + + componentWillUnmount () { + this._unsubscribes.forEach(call) + this._unsubscribes = null + } + + render () { + return + } + } diff --git a/src/common/utils.js b/src/common/utils.js index dccbda9d2..f41dcb1ad 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -35,6 +35,10 @@ export const EMPTY_OBJECT = Object.freeze({}) // =================================================================== +export addSubscriptions from './add-subscriptions' + +// =================================================================== + export const ensureArray = value => { if (value === undefined) { return [] @@ -57,48 +61,6 @@ export const propsEqual = (o1, o2, props) => { // =================================================================== -// `subscriptions` can be a function if we want to ensure that the subscription -// callbacks have been correctly initialized when there are circular dependencies -export const addSubscriptions = subscriptions => Component => { - class SubscriptionWrapper extends BaseComponent { - constructor () { - super() - - this._unsubscribes = null - } - - componentWillMount () { - this._unsubscribes = map( - isFunction(subscriptions) ? subscriptions(this.props) : subscriptions, - (subscribe, prop) => - subscribe(value => this._setState({ [prop]: value })) - ) - } - - componentDidMount () { - this._setState = this.setState - } - - componentWillUnmount () { - forEach(this._unsubscribes, unsubscribe => unsubscribe()) - this._unsubscribes = null - delete this._setState - } - - _setState (nextState) { - this.state = { ...this.state, nextState } - } - - render () { - return - } - } - - return SubscriptionWrapper -} - -// ------------------------------------------------------------------- - export const checkPropsState = (propsNames, stateNames) => Component => { const nProps = propsNames && propsNames.length const nState = stateNames && stateNames.length