]> 127.0.0.1 Git - part/.git/commitdiff
improve v0.22.19
authorqydysky <32743305+qydysky@users.noreply.github.com>
Thu, 23 Feb 2023 13:22:09 +0000 (21:22 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Thu, 23 Feb 2023 13:22:09 +0000 (21:22 +0800)
msgq/Msgq.go
msgq/Msgq_test.go

index 368ebf5bb6fb8b5b8594575a431d7645f4b3bfb9..debdc5dd26d20e9c75fe2a938e1f92dc0c431b47 100644 (file)
@@ -11,7 +11,7 @@ import (
 type Msgq struct {
        funcs          *list.List
        someNeedRemove atomic.Int32
-       sync.RWMutex
+       lock           sync.RWMutex
 }
 
 type FuncMap map[string]func(any) (disable bool)
@@ -23,9 +23,9 @@ func New() *Msgq {
 }
 
 func (m *Msgq) Register(f func(any) (disable bool)) {
-       m.Lock()
+       m.lock.Lock()
        m.funcs.PushBack(f)
-       m.Unlock()
+       m.lock.Unlock()
 }
 
 func (m *Msgq) Push(msg any) {
@@ -36,22 +36,22 @@ func (m *Msgq) Push(msg any) {
 
        var removes []*list.Element
 
-       m.RLock()
+       m.lock.RLock()
        for el := m.funcs.Front(); el != nil; el = el.Next() {
                if disable := el.Value.(func(any) bool)(msg); disable {
                        m.someNeedRemove.Add(1)
                        removes = append(removes, el)
                }
        }
-       m.RUnlock()
+       m.lock.RUnlock()
 
        if len(removes) != 0 {
-               m.Lock()
+               m.lock.Lock()
                m.someNeedRemove.Add(-int32(len(removes)))
                for i := 0; i < len(removes); i++ {
                        m.funcs.Remove(removes[i])
                }
-               m.Unlock()
+               m.lock.Unlock()
                removes = nil
        }
 }
@@ -78,3 +78,31 @@ func (m *Msgq) Pull_tag(func_map map[string]func(any) (disable bool)) {
                return false
        })
 }
+
+type MsgType[T any] struct {
+       m *Msgq
+}
+
+func (m *MsgType[T]) Push_tag(Tag string, Data T) {
+       if m.m == nil {
+               m.m = New()
+       }
+       m.m.Push(Msgq_tag_data{
+               Tag:  Tag,
+               Data: Data,
+       })
+}
+
+func (m *MsgType[T]) Pull_tag(func_map map[string]func(T) (disable bool)) {
+       if m.m == nil {
+               m.m = New()
+       }
+       m.m.Register(func(data any) (disable bool) {
+               if d, ok := data.(Msgq_tag_data); ok {
+                       if f, ok := func_map[d.Tag]; ok {
+                               return f(d.Data.(T))
+                       }
+               }
+               return false
+       })
+}
index 8548baa7910e19c723f6345619d648e7e411ea2c..9c90a4612d160dc79559cca42226fc8c522a2d96 100644 (file)
@@ -333,6 +333,21 @@ func Test_msgq5(t *testing.T) {
        t.Log(`fin`)
 }
 
+func Test_msgq6(t *testing.T) {
+       msg := MsgType[int]{}
+       msg.Pull_tag(map[string]func(int) (disable bool){
+               `1`: func(b int) (disable bool) {
+                       if b != 0 {
+                               t.Fatal()
+                       }
+                       t.Log(b)
+                       return false
+               },
+       })
+       msg.Push_tag(`1`, 0)
+       time.Sleep(time.Second)
+}
+
 // func Test_msgq6(t *testing.T) {
 //     mq := New()
 //     go mq.Pull_tag(map[string]func(interface{}) bool{