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
merged 1 commit into from Oct 31, 2017

Conversation

Projects
None yet
3 participants
@theengineear
Contributor

theengineear commented Oct 31, 2017

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

This comment has been minimized.

Contributor

theengineear commented Oct 31, 2017

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

This comment has been minimized.

Member

jayphelps commented Oct 31, 2017

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

This comment has been minimized.

Contributor

theengineear commented Oct 31, 2017

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

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

This comment has been minimized.

Member

jayphelps commented Oct 31, 2017

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

image

@theengineear

This comment has been minimized.

Contributor

theengineear commented Oct 31, 2017

@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.

@theengineear theengineear force-pushed the theengineear:rxjs-ofType-toString branch from da56a27 to d8806b4 Oct 31, 2017

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));

This comment has been minimized.

@theengineear

theengineear Oct 31, 2017

Contributor

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?

This comment has been minimized.

@jayphelps

jayphelps Oct 31, 2017

Member

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

@theengineear

This comment has been minimized.

Contributor

theengineear commented Oct 31, 2017

@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

This comment has been minimized.

Contributor

theengineear commented Oct 31, 2017

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

@jayphelps

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);

This comment has been minimized.

@jayphelps

jayphelps Oct 31, 2017

Member

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

This comment has been minimized.

@jayphelps

jayphelps Oct 31, 2017

Member

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

This comment has been minimized.

@theengineear

theengineear Oct 31, 2017

Contributor

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.

feat(ofType): integration with redux-actions.
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 theengineear force-pushed the theengineear:rxjs-ofType-toString branch from d8806b4 to cad200c Oct 31, 2017

@theengineear

This comment has been minimized.

Contributor

theengineear commented Oct 31, 2017

@jayphelps ok, updated 🎉

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

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@jayphelps

This comment has been minimized.

Member

jayphelps commented Oct 31, 2017

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

@jayphelps jayphelps referenced this pull request Oct 31, 2017

Closed

Pass function to ofType #71

@jayphelps

This comment has been minimized.

Member

jayphelps commented Oct 31, 2017

Released in v0.17.0 🎈

@rgbkrk

This comment has been minimized.

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

This comment has been minimized.

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