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

WIP: Add warnMutations middleware #138

Closed
wants to merge 6 commits into from

Conversation

leoasis
Copy link
Contributor

@leoasis leoasis commented Jun 18, 2015

Aims to fix #135

I've implemented the mutation warnings as a middleware, which expects to receive plain actions (should be the last in the chain or at least should be after the ones that transform the actions such as the thunkMiddleware).

Still need to provide support for stuff like ImmutableJS but should be straight forward. Also, I'm leaving it like this so that we can discuss implementation.

We can also discuss what's the best way to handle adding this middleware by default on dev, but not in prod (and avoid including the dependencies as well).

@goatslacker
Copy link

Copying deep on every dispatch is probably not feasible unless you're building apps with small state.

I can't think of another way to do this off the top of my head though.

@leoasis
Copy link
Contributor Author

leoasis commented Jun 18, 2015

This is meant to be used in development only.

@gaearon
Copy link
Contributor

gaearon commented Jun 18, 2015

This assumes dispatch is sync so it needs to be the last in chain right?

@leoasis
Copy link
Contributor Author

leoasis commented Jun 18, 2015

@gaearon yes:

I've implemented the mutation warnings as a middleware, which expects to receive plain actions (should be the last in the chain or at least should be after the ones that transform the actions such as the thunkMiddleware).

@@ -0,0 +1,54 @@
import isEqual from 'lodash/lang/isEqual';
import any from 'lodash/collection/any';
import cloneDeep from 'lodash/lang/cloneDeep';
Copy link
Contributor

Choose a reason for hiding this comment

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

A recent pull request removed lodash dependency. I think it would be better to avoid using lodash

@leoasis
Copy link
Contributor Author

leoasis commented Jun 19, 2015

Pushed some updates: Avoiding using lodash and checking for immutable types.

I'm not sure if it's a good thing to avoid using libraries that have been battle tested like lodash in spite of library size. We'll be maintaining and having to support edge cases for deep cloning or whatever we're replacing from lodash. Still, perhaps that's enough for our needs in this case, so if anyone sees something odd here or anything I'm missing let me know! I'll try to add more test cases to see I'm not missing any basic scenario

@gaearon
Copy link
Contributor

gaearon commented Jun 19, 2015

We'll be maintaining and having to support edge cases for deep cloning or whatever we're replacing from lodash.

I only think it makes sense to avoid lodash for the core stuff. Plugins (which should be in separate repos anyway) can use lodash, especially if they're dev-only.

@acdlite
Copy link
Collaborator

acdlite commented Jun 19, 2015

If/when you do use lodash, you should also require the specific module you want to use.

// This
import isEqual from "lodash.isequal"

// instead of this
import isEqual from "lodash/lang/isEqual"

@leoasis
Copy link
Contributor Author

leoasis commented Jun 19, 2015

If/when you do use lodash, you should also require the specific module you want to use.

@acdlite what's the reason for that? the latter option will still make the bundle require only the needed modules and no more

@gaearon
Copy link
Contributor

gaearon commented Jun 19, 2015

the latter option will still make the bundle require only the needed modules and no more

This is correct AFAIK.

@acdlite
Copy link
Collaborator

acdlite commented Jun 19, 2015

True, but the first way it makes it less likely another dev will see a lodash dependecy and be tempted to import the entire library elsewhere.

@johanneslumpe
Copy link
Contributor

To me personally there is no difference between the two versions, except that the latter is more descriptive. If at all, the second one would be more logical to me, as people see that you are specifically importing a subset/function directly, instead of a prop from the whole lib.

Sent from my iPhone

On 20 Jun 2015, at 02:28, Andrew Clark notifications@github.com wrote:

True, but the first way it makes it less likely another dev will see a lodash dependecy and be tempted to import the entire library elsewhere.


Reply to this email directly or view it on GitHub.

@acdlite
Copy link
Collaborator

acdlite commented Jun 20, 2015

Think about what the corresponding package.json files look like. The first has lodash.isequal and the second has lodash. Another developer may see a dependency on lodash and assume its safe to import the whole library, whereas a dependency on lodash.isequal forces restraint. In other words, the first version makes it impossible for an errant import statement to dramatically increase the size of the library; you'd have to also update package.json.

Clearly this is a matter of preference, though.

@leoasis
Copy link
Contributor Author

leoasis commented Jun 20, 2015

I agree it's mostly a matter of preference. The only concern I have is that
it is very likely that the end user will already be using lodash (compared
to using the separate function modules), and if we depend on just the
function module we will be making the final user bundle even larger since
it will contain duplicated stuff that webpack or any other bundler won't be
able to realize it's the same.

But again since this particular thing is just for development, that
argument perhaps it's not that important.

On Saturday, June 20, 2015, Andrew Clark notifications@github.com wrote:

Think about what the corresponding package.json files look like. The
first has lodash.isequal and the second has lodash. Another developer may
see a dependency on lodash and assume its safe to import the whole library,
whereas a dependency on lodash.isequal forces restraint. Clearly this is
a matter of preference, though.


Reply to this email directly or view it on GitHub
#138 (comment).

@acdlite
Copy link
Collaborator

acdlite commented Jun 20, 2015

Huh, I never import the entire lodash module ¯_(ツ)_/¯

@leoasis
Copy link
Contributor Author

leoasis commented Jun 20, 2015

Not meant import, but have lodash as a dependency in package.json

On Saturday, June 20, 2015, Andrew Clark notifications@github.com wrote:

Huh, I never import the entire lodash module ¯_(ツ)_/¯


Reply to this email directly or view it on GitHub
#138 (comment).

@leoasis
Copy link
Contributor Author

leoasis commented Jun 20, 2015

In any case, since the argument I mentioned is not strong in this case
since this middleware is meant to be used in development only, let's go
with the function module dependency

On Saturday, June 20, 2015, Leonardo Andrés Garcia Crespo leoasis@gmail.com
wrote:

Not meant import, but have lodash as a dependency in package.json

On Saturday, June 20, 2015, Andrew Clark <notifications@github.com
javascript:_e(%7B%7D,'cvml','notifications@github.com');> wrote:

Huh, I never import the entire lodash module ¯_(ツ)_/¯


Reply to this email directly or view it on GitHub
#138 (comment).

@leoasis
Copy link
Contributor Author

leoasis commented Jun 20, 2015

Ok I think this is now mostly complete. We now need to decide if we'll leave it here, move it to a different repo, and if leaving it here if we should automatically add it to the default redux middleware if we detect we're in the development env (assuming process.env.NODE_ENV !== 'production'?).

@gaearon
Copy link
Contributor

gaearon commented Jul 12, 2015

Please can you move this to a separate repo and try to get it working with the breaking-changes-1.0 branch? I'm closing for now, as it doesn't belong in the core.

@gaearon gaearon closed this Jul 12, 2015
@leoasis
Copy link
Contributor Author

leoasis commented Jul 13, 2015

@gaearon will do! Any particular name for this one you would prefer? redux-warn-mutations? redux-dev-warn-mutations (to make it clear that it's just for dev)?

@0xR
Copy link

0xR commented Sep 12, 2015

@leoasis's library is here in case you are looking for it in this thread:
https://github.com/leoasis/redux-immutable-state-invariant

@leoasis
Copy link
Contributor Author

leoasis commented Sep 12, 2015

@0xR thanks for adding this here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Freeze the state tree in dev?
9 participants