@ActionState
动作态装饰器。异步执行期间临时进入指定状态,结束后回到原状态。
签名
function ActionState(name?: string): MethodDecorator参数
| 参数 | 说明 |
|---|---|
name | 临时状态名。省略时默认使用方法名 |
行为
class Doc extends FSM { @ActionState('saving') async save() { await persist() }}执行 save():
- 记录
old = this.state setState('saving')(注意:是直接切到'saving',不是MiddleState)- 执行原方法
- 成功:
setState(old),返回结果 - 失败:
setState(old, err),抛出原错误
- 成功:
与 @ChangeState 的区别
@ChangeState | @ActionState | |
|---|---|---|
| 执行前校验 | 校验 from | 不校验 |
| 执行后状态 | 进入新的稳定状态 to | 回到原状态 |
| 中间态 | MiddleState(${action}ing) | 直接进入 name |
| 状态图 | 出现在 stateDiagram | 不出现 |
| 失败回滚 | 回到 from | 回到原状态 |
同步方法支持
原方法若返回同步值,行为一致(同步返回,状态切回):
@ActionState('refreshing')refresh() { return this.cache}示例
dataFetch 示例用 @ActionState('retrying') 体现重试等待的中间态。