-
I am currently trying to wrap my head around moves. The issues I have with this are twofold:
But maybe I just have a lot of misconceptions about how the events-API actually works. If you explain it to me I could also contribute some new docs clarifying the API :) Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Question 1 A move should be pure in the sense that it should rely only on the state provided by its inputs. (It is also OK to rely on some immutable data in scope, but not something stateful that gets mutated.) I’m not sure I would think of events as side effects exactly. Think of the full state object: With an example move like this function move(G, ctx, arg) {
ctx.events.endTurn();
G.arg = arg;
} You can kind of think of this as dispatching two things to the reducer:
As per the docs:
So you can call events and know that the Question 2 This depends a bit what you want to test. If you want to test that a move that calls However if you want to test a move mutates |
Beta Was this translation helpful? Give feedback.
Question 1
A move should be pure in the sense that it should rely only on the state provided by its inputs. (It is also OK to rely on some immutable data in scope, but not something stateful that gets mutated.)
I’m not sure I would think of events as side effects exactly. Think of the full state object:
{ G, ctx, ... }
.G
is yours. You can do what you want with it. The rest of the state object —ctx
, the undo/redo stack, etc. — is managed by boardgame.io and shouldn’t be touched directly. When you dispatch a move on the client, you are actually sending an action to a reducer that operates on this whole state, not justG
. The framework would be unhelpful if you couldn’t also update the non-G
…