我有以下代码,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
它运行。
是代码有错误,还是mu的理解cond
有误?