1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-10 18:10:54 +09:00

Simplify and improve request manager logic

This commit is contained in:
mcrakhman 2024-06-13 09:47:47 +02:00
parent 8d3fbd4415
commit 3ce1c9bc91
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
6 changed files with 53 additions and 58 deletions

View file

@ -0,0 +1,22 @@
package synctest
import (
"context"
"github.com/anyproto/any-sync/commonspace/sync/syncdeps"
"github.com/anyproto/any-sync/commonspace/sync/synctestproto"
)
type CounterResponseCollector struct {
counter *Counter
}
func NewCounterResponseCollector(counter *Counter) *CounterResponseCollector {
return &CounterResponseCollector{counter: counter}
}
func (c *CounterResponseCollector) CollectResponse(ctx context.Context, peerId, objectId string, resp syncdeps.Response) error {
counterResp := resp.(*synctestproto.CounterIncrease)
c.counter.Add(counterResp.Value)
return nil
}

View file

@ -1,22 +0,0 @@
package synctest
import (
"context"
"github.com/anyproto/any-sync/commonspace/sync/syncdeps"
"github.com/anyproto/any-sync/commonspace/sync/synctestproto"
)
type CounterResponseHandler struct {
counter *Counter
}
func (c *CounterResponseHandler) NewResponse() syncdeps.Response {
return &synctestproto.CounterIncrease{}
}
func (c *CounterResponseHandler) HandleResponse(ctx context.Context, peerId, objectId string, resp syncdeps.Response) error {
counterResp := resp.(*synctestproto.CounterIncrease)
c.counter.Add(counterResp.Value)
return nil
}

View file

@ -9,13 +9,19 @@ import (
"github.com/anyproto/any-sync/app"
"github.com/anyproto/any-sync/commonspace/sync/syncdeps"
"github.com/anyproto/any-sync/commonspace/sync/synctestproto"
)
type CounterSyncHandler struct {
requestHandler *CounterRequestHandler
requestSender *CounterRequestSender
responseHandler *CounterResponseHandler
updateHandler *CounterUpdateHandler
counter *Counter
requestHandler *CounterRequestHandler
requestSender *CounterRequestSender
updateHandler *CounterUpdateHandler
}
func (c *CounterSyncHandler) ApplyRequest(ctx context.Context, rq syncdeps.Request, requestSender syncdeps.RequestSender) error {
collector := NewCounterResponseCollector(c.counter)
return requestSender.SendRequest(ctx, rq, collector)
}
func NewCounterSyncHandler() syncdeps.SyncHandler {
@ -38,21 +44,16 @@ func (c *CounterSyncHandler) SendStreamRequest(ctx context.Context, rq syncdeps.
return c.requestSender.SendStreamRequest(ctx, rq, receive)
}
func (c *CounterSyncHandler) HandleResponse(ctx context.Context, peerId, objectId string, resp syncdeps.Response) error {
return c.responseHandler.HandleResponse(ctx, peerId, objectId, resp)
}
func (c *CounterSyncHandler) NewResponse() syncdeps.Response {
return c.responseHandler.NewResponse()
return &synctestproto.CounterIncrease{}
}
func (c *CounterSyncHandler) Init(a *app.App) (err error) {
counter := a.MustComponent(CounterName).(*Counter)
peerProvider := a.MustComponent(PeerName).(*PeerProvider)
c.requestHandler = &CounterRequestHandler{counter: counter}
c.counter = a.MustComponent(CounterName).(*Counter)
c.requestHandler = &CounterRequestHandler{counter: c.counter}
c.requestSender = &CounterRequestSender{peerProvider: a.MustComponent(PeerName).(*PeerProvider)}
c.responseHandler = &CounterResponseHandler{counter: counter}
c.updateHandler = &CounterUpdateHandler{counter: counter, peerProvider: peerProvider}
c.updateHandler = &CounterUpdateHandler{counter: c.counter, peerProvider: peerProvider}
return nil
}