1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-07 21:47:02 +09:00
any-sync/commonspace/sync/synctest/counterrequesthandler.go
2024-08-02 11:14:12 +02:00

31 lines
868 B
Go

package synctest
import (
"context"
"github.com/anyproto/protobuf/proto"
"golang.org/x/exp/slices"
"github.com/anyproto/any-sync/commonspace/sync/syncdeps"
"github.com/anyproto/any-sync/commonspace/sync/synctestproto"
)
type CounterRequestHandler struct {
counter *Counter
}
func (c *CounterRequestHandler) HandleStreamRequest(ctx context.Context, rq syncdeps.Request, send func(resp proto.Message) error) (syncdeps.Request, error) {
counterRequest := rq.(CounterRequest)
toSend, toAsk := c.counter.DiffCurrentNew(counterRequest.ExistingValues)
slices.Sort(toSend)
for _, value := range toSend {
_ = send(&synctestproto.CounterIncrease{
Value: value,
ObjectId: counterRequest.ObjectId(),
})
}
if len(toAsk) == 0 {
return nil, nil
}
return NewCounterRequest(counterRequest.PeerId(), counterRequest.ObjectId(), c.counter.Dump()), nil
}