diff --git a/packages/xo-web/src/common/add-subscriptions.js b/packages/xo-web/src/common/add-subscriptions.js index 43d868668..5f092e3cd 100644 --- a/packages/xo-web/src/common/add-subscriptions.js +++ b/packages/xo-web/src/common/add-subscriptions.js @@ -1,20 +1,31 @@ import React from 'react' -import { map, mapValues, noop } from 'lodash' +import { forOwn, map } from 'lodash' 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 +// +// each subscription can be either a `subscribe` function or a `[subscribe, initialValue]` array const addSubscriptions = subscriptions => Component => class SubscriptionWrapper extends React.PureComponent { constructor(props) { super(props) // provide all props since the beginning (better behavior with Freactal) - this.state = mapValues( - (this._subscribes = typeof subscriptions === 'function' ? subscriptions(props) : subscriptions), - noop - ) + const state = {} + const subscribes = {} + forOwn(typeof subscriptions === 'function' ? subscriptions(props) : subscriptions, (subscription, prop) => { + if (typeof subscription === 'function') { + subscribes[prop] = subscription + state[prop] = undefined + } else { + subscribes[prop] = subscription[0] + state[prop] = subscription[1] + } + }) + this.state = state + this._subscribes = subscribes } _unsubscribes = undefined