previousState
方法用于获取之前的状态。
const previousState = stateMachine.previousState();
方法不接受任何参数。
方法返回表示之前状态的字符串。
如果之前没有状态,则返回 null。
const stateMachine = new StateMachine({
init: 'green',
transitions: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
]
});
stateMachine.state; // 'green'
stateMachine.warn();
stateMachine.state; // 'yellow'
stateMachine.previousState; // 'green'
stateMachine.calm();
stateMachine.state; // 'yellow'
stateMachine.previousState; // 'green'
如果当前状态是初始状态,则 previousState
函数将返回 null。