#console.log every action and guard call

1 messages · Page 1 of 1 (latest)

languid bloom
#

Is it possible to console.log any action or guard call automatically? Because right now I have log statement in every action/guard. It's very repetitive. These logs are helpful with debugging, because I can see clearly the machine state in any call.

autumn swallow
#

Do you just want to log the state whenever the state machine transitions?

#

You can do this:

#
const actor = interpret(machine);
actor.subscribe(state => {
  console.log(state);
});
actor.start();
languid bloom
#

No, I want to log every action/guard call. I have many actions or guards and I put a console.log in them so I can see what the context or event is while its being called. Its great for debugging, because when there are multiple actions call on entry/exit/transition I can see what state in each action is. And I wonder if its possible to avoid adding .log in each function declaration separately. I hope it make sense. I can create a sandbox for it, if its still confusing

autumn swallow
#

It makes sense. There's no abstraction for this directly in XState currently

robust venture
#

I also have a problem where console.log() gets ignored entirely when added inside a guard function

autumn swallow
#

That shouldn't happen. Can you post a repro?

robust venture
#

Not able to do that..
The machine is created like this

return createMachine(
  getConfig<T, TData>(arg1, arg2, arg3, data),
  getOptions<T, TData>(services, arg2, arg3, data)
);
autumn swallow
#

Right but the console should not be ignored when evaluating guards

robust venture
#

Hmm, if I do the guard inline the conosle.log works

target: ...
cond: (context, event) => {
  console.log(...);
}
robust venture
#

The guard function wasn't really a pure function but even after I've made it pure it still didn't do the console.log