Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duck-type subscription in prop checks #628

Merged
merged 2 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/components/Provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 +50,6 @@ Provider.propTypes = {
}
Provider.childContextTypes = {
store: storeShape.isRequired,
storeSubscription: PropTypes.instanceOf(Subscription)
storeSubscription: subscriptionShape
}
Provider.displayName = 'Provider'
17 changes: 8 additions & 9 deletions src/components/connectAdvanced.js
Original file line number Diff line number Diff line change
@@ -1,14 +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() {}

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

const contextTypes = {
[storeKey]: storeShape,
[subscriptionKey]: PropTypes.instanceOf(Subscription),
[subscriptionKey]: subscriptionShape,
}
const childContextTypes = {
[subscriptionKey]: PropTypes.instanceOf(Subscription)
[subscriptionKey]: subscriptionShape,
}

return function wrapWithConnect(WrappedComponent) {
Expand Down Expand Up @@ -194,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 @@ -218,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.