У меня есть следующий код, условие в состоянии start
доставляет мне проблемы.
const machine = require('xstate');
let test = true;
// Stateless machine definition
// machine.transition(...) is a pure function used by the interpreter.
const helloMachine = machine.Machine({
id: 'hello',
initial: 'start',
states: {
start: {
on: {
STOP: {
target: 'stop',
actions: ['hi'],
// ! something is wrong with cond structure
cond: () => test === false // returns either true or false, which signifies whether the transition should be allowed to take place
}
}
},
stop: {
entry: ['hello'], // The action(s) to be executed upon entering the state node.
type: 'final'
}
}
}, {
actions: {
hello: (context, event) => {
const hello = 'Hello World';
console.log(hello)
},
hi: () => {
test = false;
console.log(`${test} -> ${typeof(test)}`);
}
}
})
// Machine instance with internal state
const helloService = machine.interpret(helloMachine)
helloService.start();
helloService.send('STOP');
Как я вижу, если проверка ложна, она должна перейти к следующему состоянию. Первоначально test = true
, в действии hi
я переключаю на false, а затем проверяю условие, если cond: () => test === false
следующее состояние не запускается, но если cond: () => test === true
оно выполняется.
Isв коде есть ошибка, или мю понимание cond
неверно?