1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00
any-sync/commonspace/headsync/headupdater.go
2024-12-31 00:10:41 +01:00

43 lines
837 B
Go

package headsync
import (
"context"
"github.com/cheggaaa/mb/v3"
"github.com/anyproto/any-sync/commonspace/headsync/headstorage"
)
type headUpdater struct {
updateFunc func(update headstorage.HeadsUpdate)
batcher *mb.MB[headstorage.HeadsUpdate]
}
func newHeadUpdater(update func(update headstorage.HeadsUpdate)) *headUpdater {
return &headUpdater{
batcher: mb.New[headstorage.HeadsUpdate](0),
updateFunc: update,
}
}
func (hu *headUpdater) Add(update headstorage.HeadsUpdate) error {
return hu.batcher.Add(context.Background(), update)
}
func (hu *headUpdater) Run() {
go hu.process()
}
func (hu *headUpdater) process() {
for {
msg, err := hu.batcher.WaitOne(context.Background())
if err != nil {
return
}
hu.updateFunc(msg)
}
}
func (hu *headUpdater) Close() error {
return hu.batcher.Close()
}