feat: build wahrwelt A/D toolkit

This commit is contained in:
TakuyaYagam1 committed 2026-10-14 11:30:41 +03:00
1 parent 303438d206
commit 653924d8c6
564 files changed
+36310 -49998

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
package pubsub
import "sync"
type PubSub[T any] struct {
subs map[string]*Subscription[T]
mu sync.RWMutex
}
func NewPubSub[T any]() *PubSub[T] {
return &PubSub[T]{
subs: make(map[string]*Subscription[T]),
}
}
func (ps *PubSub[T]) Publish(msg T) {
ps.mu.RLock()
defer ps.mu.RUnlock()
for _, sub := range ps.subs {
sub.Push(msg)
}
}
func (ps *PubSub[T]) Subscribe(onMsg MessageHandler[T]) *Subscription[T] {
ps.mu.Lock()
defer ps.mu.Unlock()
sub := NewSubscription(onMsg)
ps.subs[sub.GetID()] = sub
return sub
}
func (ps *PubSub[T]) Unsubscribe(sub *Subscription[T]) {
ps.mu.Lock()
defer ps.mu.Unlock()
delete(ps.subs, sub.GetID())
}