Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add document change event broadcast using members list #189

Merged
merged 12 commits into from
Jun 9, 2021
34 changes: 27 additions & 7 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/yorkie-team/yorkie/pkg/document/operation"
"github.com/yorkie-team/yorkie/pkg/document/time"
"github.com/yorkie-team/yorkie/pkg/types"
"github.com/yorkie-team/yorkie/yorkie/backend/sync"
)

// FromClient converts the given Protobuf format to model format.
Expand Down Expand Up @@ -127,16 +128,35 @@ func FromDocumentKeys(pbKeys []*api.DocumentKey) []*key.Key {
}

// FromEventType converts the given Protobuf format to model format.
func FromEventType(pbEventType api.EventType) (types.EventType, error) {
switch pbEventType {
case api.EventType_DOCUMENTS_CHANGED:
return types.DocumentsChangeEvent, nil
case api.EventType_DOCUMENTS_WATCHED:
func FromEventType(pbDocEventType api.DocEventType) (types.DocEventType, error) {
switch pbDocEventType {
case api.DocEventType_DOCUMENTS_CHANGED:
return types.DocumentsChangedEvent, nil
case api.DocEventType_DOCUMENTS_WATCHED:
return types.DocumentsWatchedEvent, nil
case api.EventType_DOCUMENTS_UNWATCHED:
case api.DocEventType_DOCUMENTS_UNWATCHED:
return types.DocumentsUnwatchedEvent, nil
}
return "", fmt.Errorf("%v: %w", pbEventType, ErrUnsupportedEventType)
return "", fmt.Errorf("%v: %w", pbDocEventType, ErrUnsupportedEventType)
}

// FromDocEvent converts the given Protobuf format to model format.
func FromDocEvent(docEvent *api.DocEvent) (*sync.DocEvent, error) {
client, err := FromClient(docEvent.Publisher)
if err != nil {
return nil, err
}

eventType, err := FromEventType(docEvent.Type)
if err != nil {
return nil, err
}

return &sync.DocEvent{
Type: eventType,
Publisher: *client,
DocumentKeys: FromDocumentKeys(docEvent.DocumentKeys),
}, nil
}

// FromOperations converts the given Protobuf format to model format.
Expand Down
29 changes: 22 additions & 7 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/yorkie-team/yorkie/pkg/document/operation"
"github.com/yorkie-team/yorkie/pkg/document/time"
"github.com/yorkie-team/yorkie/pkg/types"
"github.com/yorkie-team/yorkie/yorkie/backend/sync"
)

// ToClient converts the given model to Protobuf format.
Expand Down Expand Up @@ -96,7 +97,7 @@ func toChangeID(id *change.ID) *api.ChangeID {
}

// ToDocumentKeys converts the given model format to Protobuf format.
func ToDocumentKeys(keys ...*key.Key) []*api.DocumentKey {
func ToDocumentKeys(keys []*key.Key) []*api.DocumentKey {
var pbKeys []*api.DocumentKey
for _, k := range keys {
pbKeys = append(pbKeys, toDocumentKey(k))
Expand All @@ -122,20 +123,34 @@ func ToClientsMap(clientsMap map[string][]types.Client) map[string]*api.Clients
return pbClientsMap
}

// ToEventType converts the given model format to Protobuf format.
func ToEventType(eventType types.EventType) (api.EventType, error) {
// ToDocEventType converts the given model format to Protobuf format.
func ToDocEventType(eventType types.DocEventType) (api.DocEventType, error) {
switch eventType {
case types.DocumentsChangeEvent:
return api.EventType_DOCUMENTS_CHANGED, nil
case types.DocumentsChangedEvent:
return api.DocEventType_DOCUMENTS_CHANGED, nil
case types.DocumentsWatchedEvent:
return api.EventType_DOCUMENTS_WATCHED, nil
return api.DocEventType_DOCUMENTS_WATCHED, nil
case types.DocumentsUnwatchedEvent:
return api.EventType_DOCUMENTS_UNWATCHED, nil
return api.DocEventType_DOCUMENTS_UNWATCHED, nil
default:
return 0, fmt.Errorf("%s: %w", eventType, ErrUnsupportedEventType)
}
}

// ToDocEvent converts the given model to Protobuf format.
func ToDocEvent(docEvent sync.DocEvent) (*api.DocEvent, error) {
eventType, err := ToDocEventType(docEvent.Type)
if err != nil {
return nil, err
}

return &api.DocEvent{
Type: eventType,
Publisher: ToClient(docEvent.Publisher),
DocumentKeys: ToDocumentKeys(docEvent.DocumentKeys),
}, nil
}

// ToOperations converts the given model format to Protobuf format.
func ToOperations(operations []operation.Operation) ([]*api.Operation, error) {
var pbOperations []*api.Operation
Expand Down
Loading