You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think it's reasonable to allow action names to be Symbols, not only strings. It will make possible real encapsulation between modules and avoid any ction name collisions. Now I have to use very long action names to make them unique (some kind of reducerName.actionName or similar). That's really annoying when your project grows. The only thing that might break is user appearance in Redux Dev Tools, as it has action names list and expects them to be strings.
Thank you.
If the action type were a symbol, how would that work with the dev tools, which show the actions listed by type? Would it show just the symbol? And if you use symbols to avoid problems with collisions, you will have ambiguities in your devTools action list.
I don't see that as being a big deal. It should be totally up to users to determine how they want to compose their actions. Personally, I prefer to make it more explicit, but that's just me. Not only that, but it seems like global namespaced symbols should be supported Symbol.for('root.feature.init'). Again, not my cup of tea, but I don't see why we can't support it.
@karptonite, @bfricka Thank you for your response!
I don't think DevTools limitations should impact on the library API and application design, and my point is that it's a developer who decides whether to use DevTools advantages and String as actions, or use Symbol and make DevTools useless in that case. As no backward compatibility is broken, I think this update will be useful for some users, whereas others will just continue to use strings and see no difference in their workflow. As for DevTools, I can't say for now how to deal with it and Symbol-typed actions. The only idea I have now is to use symbol descriptions and show them in actions list.
The only thing that might break is user appearance in Redux Dev Tools, as it has action names list and expects them to be strings.
Here is another (big?) think that would break. As I understand, a historical reason to require strings, not symbols, was that the former can take a round trip ride to/from JSON in the trivial manner. This is a very useful property, Actions as "just data". Some systems (including code we have in production...) enjoy and rely on this property.
Of course, it should be possible to carry by my moving to a more sophisticated serialization library and data format, something that round trips symbols. This is workable, though less convenient, another library to pick out and bundle, and use, etc
(I just wrote a very similar comment about a move away from "just data" to "must be the right class" in #160 .)
If a change like this goes to to Store, I would appreciate very much if that included an explanation in the README about the need for more-than-just-JSON for Actions, and a recommendation link or two, or libraries that effectively read/write symbols, classes, etc.
@kylecordes I didn't think about serialization at all. You're right, non-strings will be way too hard to serialize and deserialize. If many of users rely on this functionaity, using strings is a best case. Compared to serialization issues, incapsulation seems to be a minor problem.
First of all, "just data" is not even implemented in the ngrx library itself. Look at router-store. It uses RouterStateSnapshot and because of this is running in to serialization issues associated with redux-devtools. To me serialization only matters if it's not support by the structured cloning algorithm. In case of Symbols, it's not, so I'd agree that is a decent argument for not using them. This would be problematic, for example, if we wanted to move forward with Worker Effects because structured clone is what is used to pass data to worker threads.
So in the context of this ticket, for sure, good thoughts on serialization!
That said, I don't think this is as strong a consideration when it comes to JSON, as a good implementation will define interfaces for (de)serialization (e.g. ngrx-store-localstorage has this). In this case, you can easily rehydrate to the correct types.
I only mention this, b/c I don't think it's helpful to consider Redux a "POJO-only" sort of pattern. Serialization is a consideration, and in certain cases issues with serialization need to be weighed against the benefits. Because Symbols don't buy you much, I don't think the case is good, so I'm grateful you brought this up.
Just a tip: If you are looking for type safety and you create classes to represent your actions, you can make your type a static property...
export class SomeAction implements Action { static get getType(): string { return 'SOME_ACTION'; } readonly type = SomeAction.getType; constructor(public someRequiredValue: string) { } }
Now you can use SomeAction.getType anywhere you need the type and now there is only one place to change the magic string... but at this point it doesn't feel so magical.
Activity
bfricka commentedon Jul 21, 2017
So you're just looking for:
?
govorov commentedon Jul 21, 2017
Yes, but I can't say for sure that this is enough and something deep inside the sources would not break.
karptonite commentedon Jul 21, 2017
If the action type were a symbol, how would that work with the dev tools, which show the actions listed by type? Would it show just the symbol? And if you use symbols to avoid problems with collisions, you will have ambiguities in your devTools action list.
bfricka commentedon Jul 23, 2017
I don't see that as being a big deal. It should be totally up to users to determine how they want to compose their actions. Personally, I prefer to make it more explicit, but that's just me. Not only that, but it seems like global namespaced symbols should be supported
Symbol.for('root.feature.init'). Again, not my cup of tea, but I don't see why we can't support it.Am I missing something?
govorov commentedon Jul 23, 2017
@karptonite, @bfricka Thank you for your response!
I don't think DevTools limitations should impact on the library API and application design, and my point is that it's a developer who decides whether to use DevTools advantages and
Stringas actions, or useSymboland make DevTools useless in that case. As no backward compatibility is broken, I think this update will be useful for some users, whereas others will just continue to use strings and see no difference in their workflow. As for DevTools, I can't say for now how to deal with it andSymbol-typed actions. The only idea I have now is to use symbol descriptions and show them in actions list.kylecordes commentedon Jul 23, 2017
Here is another (big?) think that would break. As I understand, a historical reason to require strings, not symbols, was that the former can take a round trip ride to/from JSON in the trivial manner. This is a very useful property, Actions as "just data". Some systems (including code we have in production...) enjoy and rely on this property.
Of course, it should be possible to carry by my moving to a more sophisticated serialization library and data format, something that round trips symbols. This is workable, though less convenient, another library to pick out and bundle, and use, etc
(I just wrote a very similar comment about a move away from "just data" to "must be the right class" in #160 .)
If a change like this goes to to Store, I would appreciate very much if that included an explanation in the README about the need for more-than-just-JSON for Actions, and a recommendation link or two, or libraries that effectively read/write symbols, classes, etc.
govorov commentedon Jul 23, 2017
@kylecordes I didn't think about serialization at all. You're right, non-strings will be way too hard to serialize and deserialize. If many of users rely on this functionaity, using strings is a best case. Compared to serialization issues, incapsulation seems to be a minor problem.
bfricka commentedon Jul 24, 2017
First of all, "just data" is not even implemented in the ngrx library itself. Look at router-store. It uses
RouterStateSnapshotand because of this is running in to serialization issues associated with redux-devtools. To me serialization only matters if it's not support by the structured cloning algorithm. In case of Symbols, it's not, so I'd agree that is a decent argument for not using them. This would be problematic, for example, if we wanted to move forward with Worker Effects because structured clone is what is used to pass data to worker threads.So in the context of this ticket, for sure, good thoughts on serialization!
That said, I don't think this is as strong a consideration when it comes to JSON, as a good implementation will define interfaces for (de)serialization (e.g. ngrx-store-localstorage has this). In this case, you can easily rehydrate to the correct types.
I only mention this, b/c I don't think it's helpful to consider Redux a "POJO-only" sort of pattern. Serialization is a consideration, and in certain cases issues with serialization need to be weighed against the benefits. Because Symbols don't buy you much, I don't think the case is good, so I'm grateful you brought this up.
ShiftySituation commentedon Jul 26, 2017
Just a tip: If you are looking for type safety and you create classes to represent your actions, you can make your type a static property...
export class SomeAction implements Action { static get getType(): string { return 'SOME_ACTION'; } readonly type = SomeAction.getType; constructor(public someRequiredValue: string) { } }Now you can use
SomeAction.getTypeanywhere you need the type and now there is only one place to change the magic string... but at this point it doesn't feel so magical.brandonroberts commentedon Aug 3, 2017
Closing this as serialization is still a priority for actions