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

Only warn for unexpected key once per key #1818

Merged
merged 1 commit into from
Jun 23, 2016

Conversation

aweary
Copy link
Contributor

@aweary aweary commented Jun 17, 2016

Resolves #1748

Pretty simple, just keeps a caches and filters keys that have already been warned about.

I had to update one of the related tests as it expected some keys to be warned about multiple times.

@aweary aweary force-pushed the warn-unexpected-keys-once branch from e016021 to c9b5c3a Compare June 17, 2016 21:55
@@ -34,7 +36,9 @@ function getUnexpectedStateShapeWarningMessage(inputState, reducers, action) {
)
}

var unexpectedKeys = Object.keys(inputState).filter(key => !reducers.hasOwnProperty(key))
var unexpectedKeys = Object.keys(inputState).filter(key => !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s split this into several lines so they format more nicely?

@gaearon
Copy link
Contributor

gaearon commented Jun 21, 2016

Looks good to me after fixing the nits.

@aweary aweary force-pushed the warn-unexpected-keys-once branch from c9b5c3a to 0a3eee0 Compare June 21, 2016 22:04
@aweary
Copy link
Contributor Author

aweary commented Jun 21, 2016

@gaearon the nits have been fixed 👍

@@ -2,6 +2,8 @@ import { ActionTypes } from './createStore'
import isPlainObject from 'lodash/isPlainObject'
import warning from './utils/warning'

const unexpectedKeyCache = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s use var for consistency (we’ll codemod ’em all over later)

@aweary aweary force-pushed the warn-unexpected-keys-once branch from 0a3eee0 to 2c48641 Compare June 22, 2016 01:31
@aweary
Copy link
Contributor Author

aweary commented Jun 22, 2016

@gaearon changes made 👍

@@ -237,5 +237,26 @@ describe('Utils', () => {

spy.restore()
})

it('only warns for unexpected keys once', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I undestand correctly that tests now depend on global shared state and run order? This doesn’t look very good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that should be resolved by using individual cache instances per combineReducers call

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

@aweary
Copy link
Contributor Author

aweary commented Jun 22, 2016

@gaearon I updated the PR so that it creates and passes a new cache to getUnexpectedStateShapeWarningMessage whenever combineReducers is called. I didn't see any existing code that indicated how you'd want to format these functions now that the argument list is rather long, so let me know if any changes are required 👍

@@ -101,6 +99,7 @@ function assertReducerSanity(reducers) {
*/
export default function combineReducers(reducers) {
var reducerKeys = Object.keys(reducers)
var unexpectedKeyCache = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably no need to declare it unless we’re in DEV.

Create key cache in combineReducers

Only define unexpectedKeyCache in dev environment
@aweary aweary force-pushed the warn-unexpected-keys-once branch from 13ddb17 to 2d6316d Compare June 22, 2016 21:11
@aweary
Copy link
Contributor Author

aweary commented Jun 22, 2016

@gaearon updated again. Fourth try's the charm.

@gaearon
Copy link
Contributor

gaearon commented Jun 22, 2016

Have you tested this on a real project to check that it behaves as expected?

@aweary
Copy link
Contributor Author

aweary commented Jun 22, 2016

I haven't tested it any real project, no. I can run it on some of the example projects and verify the results if you like.

@aweary
Copy link
Contributor Author

aweary commented Jun 22, 2016

@gaearon

I tested the real-world example branch and verified it's working as expected. I changed the rootReducer call so it passed an invalid foobar value to the pagination reducer

const rootReducer = combineReducers({
  entities,
  pagination: (state, action) => {
    return pagination(
      Object.assign({}, state, {foobar: false}),
      action
    )
  },
  errorMessage,
  routing
})

With the current release of redux this caused multiple warnings. After running npm run build and replacing the lib folder in the example project's node_modules/redux folder the error was only logged once no matter what actions were performed.

@gaearon gaearon merged commit ea43078 into reduxjs:master Jun 23, 2016
@gaearon
Copy link
Contributor

gaearon commented Jun 23, 2016

👍

seantcoyote pushed a commit to seantcoyote/redux that referenced this pull request Jan 14, 2018
Create key cache in combineReducers

Only define unexpectedKeyCache in dev environment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants