-
Notifications
You must be signed in to change notification settings - Fork 710
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
consider using Immer #295
Comments
I would like to suggest the following syntax, as to avoid breaking existing moves, and is similar to normal Redux action creators vs. Redux-thunk action creators.
// default (current) behavior, process without Immer
drawCard: (G, ctx) => ({
...G,
hand: G.hand + 1,
deck: G.deck - 1,
}) // process with Immer
drawCard: (prevG, ctx) => nextG => {
nextG.hand++;
nextG.deck--;
} In this manner, an invalid move returning undefined could be expressed as drawCard: (prevG, ctx) => {
if(prevG.deck === 0) return undefined
return nextG => {
nextG.hand++;
nextG.deck--;
}
} |
Immer allows you to return the new state, so we can just turn on Immer in a backwards compatible way without needing to return a thunk. I think both the following would be valid: drawCard: (G, ctx) => {
G.hand++;
G.deck--;
} drawCard: (G, ctx) => ({
...G,
hand: G.hand + 1,
deck: G.deck - 1,
}) so people can just choose the style that they prefer. The main problem is the invalid move API. One option would be to do something like this: import { INVALID_MOVE } from 'boardgame.io/core';
drawCard: (G, ctx) => {
if (G.deck === 0) return INVALID_MOVE;
...
} I think it's reasonable to disallow setting |
It feels like that's enabling people to continue to get immutability wrong. How about drawCard: (G, ctx) => {
this.hand++;
this.deck--;
} Regarding INVALID_MOVE, that looks like a reasonable syntax. The immer import { nothing } from "immer"
export const INVALID_MOVE = nothing |
I prefer to think of it more as Immer allowing people not to think about immutability (rather than enabling them to get it wrong). For people that are not familiar with the concept, I think there is a basic expectation that they should be able to mutate So I think we should just allow (Immer handled) mutations on |
I can see the advantage of having current code errantly mutating state being safe once this is implemented. Immer automatically binds |
Supported in |
People get immutability wrong all the time (self included). Even the ones that know what they're doing often have to write some really messy code when deep state updates are involved.
Immer seems to achieve the best of both worlds. We should consider bundling it in (and possible have a flag that allows turning it off for people that prefer not to use it).
vs.
Open questions:
The text was updated successfully, but these errors were encountered: