Skip to content

Commit

Permalink
Add document change event broadcast using members list (yorkie-team#189)
Browse files Browse the repository at this point in the history
Co-authored-by: Hackerwins <[email protected]>
  • Loading branch information
2 people authored and jeonjonghyeok committed Aug 4, 2022
1 parent a67af08 commit e6e232e
Show file tree
Hide file tree
Showing 18 changed files with 1,613 additions and 779 deletions.
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

0 comments on commit e6e232e

Please sign in to comment.