|
12345678910111213141516171819202122232425262728293031323334353637383940 |
- package lock
-
- type LockChainOperator struct {
- ChainList []Lock
- }
-
- func NewLockChainOperator() *LockChainOperator {
- return &LockChainOperator{}
- }
-
- func (b *LockChainOperator) Add(l Lock) *LockChainOperator {
- b.ChainList = append(b.ChainList, l)
- return b
- }
-
- func (b *LockChainOperator) Lock(ctx *LockContext) string {
- for i := 0; i < len(b.ChainList); i++ {
- l := b.ChainList[i]
- if !l.IsMatch(ctx) {
- continue
- }
-
- if errCode := l.Lock(ctx); errCode != "" {
- b.Unlock(ctx)
- return errCode
- }
- ctx.LockedList = append(ctx.LockedList, l)
- }
- return ""
- }
-
- func (b *LockChainOperator) Unlock(ctx *LockContext) error {
- if b.ChainList == nil || len(b.ChainList) == 0 {
- return nil
- }
- for j := len(ctx.LockedList) - 1; j >= 0; j-- {
- ctx.LockedList[j].Unlock(ctx)
- }
- return nil
- }
|