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

feat(ofType): integration with redux-actions. #348

Merged

Conversation

theengineear
Copy link
Contributor

The actionCreators you make with redux-actions implement their own
toString, which means you don't have to keep track of action types at
all. Before this commit, you need to:

ofType(myActionCreator.toString())

In order to get this to work as expected. As of this commit, you can:

ofType(myActionCreator)

Closes: #192

@theengineear
Copy link
Contributor Author

I'd also really appreciate support for toString implementations. If someone wants to detail the concern surrounding Symbols mentioned in #192, I'd be happy to incorporate that into the spec.

@jayphelps
Copy link
Member

Hey @theengineear thanks for the PR! I think if we only applied toString when the type is a function, this will continue to work for Symbols too.

Something like..

const key = keys[i];
if (key === type || (typeof key === 'function' && key.toString() === type)) {

}

@theengineear
Copy link
Contributor Author

@jayphelps yup 👌 , more or less what I'm doing:

const keyHasType = (type, key) => {
  return type === key || typeof key === 'function' && type === String(key);
};

@jayphelps
Copy link
Member

jayphelps commented Oct 31, 2017

@theengineear hmm I'm not seeing that code. Did you forget to push perhaps?

image

@theengineear
Copy link
Contributor Author

@jayphelps just locking down the spec. I realized that if the toString implementation of an actionCreator returns a Symbol, you will get an error converting a Symbol to a string. I'll ping you when it's up/done.

expect(LARF_TYPE.toString()).to.equal(HAHA_TYPE.toString());
expect(String(HAHA_TYPE)).to.equal(String(LULZ_TYPE));
expect(String(LULZ_TYPE)).to.equal(String(LARF_TYPE));
expect(String(LARF_TYPE)).to.equal(String(HAHA_TYPE));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was surprised to see that String(Symbol()) doesn't throw an error. But...

const foo = () => {};
foo.toString = () => Symbol();
String(foo);

DOES throw an error. Is this something that I'm not understanding that I need to take into account?

Copy link
Member

Choose a reason for hiding this comment

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

toString should always return a string. If someone overrides it and does not return a string, that's their fault lol 😄

@theengineear
Copy link
Contributor Author

@jayphelps OK. All set. However, it might be a bad idea to allow a function's toString to return a symbol for now. That's the last thing I could be hung up on. Thoughts?

@theengineear
Copy link
Contributor Author

Some context. Looks like redux-actions has discussed dropping symbols support before redux-utilities/redux-actions#126

Copy link
Member

@jayphelps jayphelps left a comment

Choose a reason for hiding this comment

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

Looks good except I don't think we need those sanity checks on Symbols themselves, only the tests against ofType

const LARF_TYPE = Symbol();

// Sanity check.
expect(HAHA_TYPE).not.to.equal(LULZ_TYPE);
Copy link
Member

Choose a reason for hiding this comment

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

Curious..why are these sanity checks needed? I don't think we need to test Symbols themselves in contexts unrelated to ofType.

Copy link
Member

Choose a reason for hiding this comment

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

obv do let me know if you think they're needed 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha, ya I can take them out. I thought it might be helpful to document why the test is there, but I think it's clear from the test string.

The actionCreators you make with redux-actions implement their own
toString, which means you don't have to keep track of action types at
all. Before this commit, you need to:

```
ofType(myActionCreator.toString())
```

In order to get this to work as expected. As of this commit, you can:

```
ofType(myActionCreator)
```
@theengineear
Copy link
Contributor Author

@jayphelps ok, updated 🎉

@jayphelps jayphelps merged commit c4d0ccf into redux-observable:master Oct 31, 2017
@jayphelps
Copy link
Member

So awesome. Thank you very much @theengineear!!!

@jayphelps jayphelps mentioned this pull request Oct 31, 2017
@jayphelps
Copy link
Member

Released in v0.17.0 🎈

@rgbkrk
Copy link
Member

rgbkrk commented Oct 31, 2017

Thanks! I had some dangling comments in here, which I'll go ahead and post even though this is merged and we're good to go.

Even though redux-actions does it, Symbol so shouldn't be a return for toString:

  • Symbols are unique Symbol('a') !== Symbol('a'), whereas 'a' === 'a' for strings
  • Coercing Symbols to strings leads to them no longer being Symbols whatsoever
> Symbol('a').toString() === Symbol('b').toString()
false
> Symbol('a').toString() === Symbol('a').toString()
true

Generally I'd say my statement on toString is that it should return a string, always. Not something that can be coerced (and Symbols won't cast to string without some force). Symbols can be types, I just want to clarify my thinking on toString returning a Symbol not just being an anti-pattern -- it's downright wrong so we shouldn't have to account for it.

@rgbkrk
Copy link
Member

rgbkrk commented Oct 31, 2017

Thanks for this release @jayphelps, it's awesome.

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.

None yet

3 participants