smallnest / gofsm

a featured FSM that can export state images
Apache License 2.0
308 stars 52 forks source link

在 Action 回调中如何返回执行的结果给调用方? #6

Open Michael2008S opened 5 years ago

Michael2008S commented 5 years ago

这里是执行方,怎么样把 Action回调 的执行结果返回给调用方比较好?

func (this *ShopsDB) GetShopsEventFSM(o orm.DB) (*fsm.StateMachine) {
    return model.ImplShopsStatusFSM(func(fromState string, args []interface{}) {
        // OnExit
    }, func(action string, fromState string, toState string, args []interface{}) error {
        // Action
        switch action {
        case retail_shopspb.ShopsEvent_EVENT_SHOP_RECOMMIT_VERIFY.String():
            if len(args) >= 1 {
                if s, ok := args[0].(*model.Shops); ok {
                    s.Status = retail_shopspb.ShopsStatus(retail_shopspb.ShopsStatus_value[toState])
                    if result, err := o.Model(s).Column("status", "name", "desc").WherePK().Update(); err != nil {
                        return errors.New("更新失败,未能进行状态修改。" + err.Error())
                    } else {
                        // FIXME 如何返回执行的结果 给调用方?
                        fmt.Println(result)
                        return nil
                    }
                }
            }
            return errors.New("未知类型,未能进行状态修改。")
        default:
            return errors.New("未知事件,未能进行状态修改。")
        }
        return nil
    }, func(action string, fromState string, toState string, args []interface{}, err error) {
        // OnActionFailure
    }, func(toState string, args []interface{}) {
        // OnEnter
    })
}

下面的代码是调用方:

    shopsFsm := s.idb.GetShopsEventFSM(s.dbcl.WithContext(c))
    err = shopsFsm.Trigger(view_result.Status.String(),
        retail_shopspb.ShopsEvent_EVENT_SHOP_RECOMMIT_VERIFY.String(), view_result)
    if err != nil {
        return nil, twirp.NewError(twirp.InvalidArgument, "数据保存失败。")
    }
    // FIXME 返回结果处理