Skip to content

Commit

Permalink
Centralize PropType shapes.
Browse files Browse the repository at this point in the history
  • Loading branch information
timdorr committed Feb 22, 2017
1 parent cc8c197 commit 6a75604
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/components/Provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, PropTypes, Children } from 'react'
import Subscription from '../utils/Subscription'
import storeShape from '../utils/storeShape'
import { storeShape, subscriptionShape } from '../utils/PropTypes'
import warning from '../utils/warning'

let didWarnAboutReceivingStore = false
Expand Down Expand Up @@ -51,6 +51,6 @@ Provider.propTypes = {
}
Provider.childContextTypes = {
store: storeShape.isRequired,
storeSubscription: PropTypes.instanceOf(Subscription)
storeSubscription: subscriptionShape
}
Provider.displayName = 'Provider'
20 changes: 6 additions & 14 deletions src/components/connectAdvanced.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import hoistStatics from 'hoist-non-react-statics'
import invariant from 'invariant'
import { Component, PropTypes, createElement } from 'react'
import { Component, createElement } from 'react'

import Subscription from '../utils/Subscription'
import storeShape from '../utils/storeShape'
import { storeShape, subscriptionShape } from '../utils/PropTypes'

let hotReloadingVersion = 0
const dummyState = {}
function noop() {}

const subscriptionShape = PropTypes.shape({
trySubscribe: PropTypes.func.isRequired,
tryUnsubscribe: PropTypes.func.isRequired,
notifyNestedSubs: PropTypes.func.isRequired,
isSubscribed: PropTypes.func.isRequired,
})

function makeSelectorStateful(sourceSelector, store) {
// wrap the selector in an object that tracks its results between runs.
const selector = {
Expand Down Expand Up @@ -201,16 +193,16 @@ export default function connectAdvanced(

initSubscription() {
if (!shouldHandleStateChanges) return

// parentSub's source should match where store came from: props vs. context. A component
// connected to the store via props shouldn't use subscription from context, or vice versa.
const parentSub = (this.propsMode ? this.props : this.context)[subscriptionKey]
this.subscription = new Subscription(this.store, parentSub, this.onStateChange.bind(this))

// `notifyNestedSubs` is duplicated to handle the case where the component is unmounted in
// the middle of the notification loop, where `this.subscription` will then be null. An
// extra null check every change can be avoided by copying the method onto `this` and then
// replacing it with a no-op on unmount. This can probably be avoided if Subscription's
// replacing it with a no-op on unmount. This can probably be avoided if Subscription's
// listeners logic is changed to not call listeners that have been unsubscribed in the
// middle of the notification loop.
this.notifyNestedSubs = this.subscription.notifyNestedSubs.bind(this.subscription)
Expand All @@ -225,7 +217,7 @@ export default function connectAdvanced(
this.componentDidUpdate = this.notifyNestedSubsOnComponentDidUpdate
this.setState(dummyState)
}
}
}

notifyNestedSubsOnComponentDidUpdate() {
// `componentDidUpdate` is conditionally implemented when `onStateChange` determines it
Expand Down
14 changes: 14 additions & 0 deletions src/utils/PropTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PropTypes } from 'react'

export const subscriptionShape = PropTypes.shape({
trySubscribe: PropTypes.func.isRequired,
tryUnsubscribe: PropTypes.func.isRequired,
notifyNestedSubs: PropTypes.func.isRequired,
isSubscribed: PropTypes.func.isRequired,
})

export const storeShape = PropTypes.shape({
subscribe: PropTypes.func.isRequired,
dispatch: PropTypes.func.isRequired,
getState: PropTypes.func.isRequired
})
7 changes: 0 additions & 7 deletions src/utils/storeShape.js

This file was deleted.

0 comments on commit 6a75604

Please sign in to comment.