diff --git a/api/types/event.go b/api/types/event.go index 394614781..e3507848f 100644 --- a/api/types/event.go +++ b/api/types/event.go @@ -9,7 +9,8 @@ type DocEvent struct { DocumentID ID } -// BroadcastEvent represents events that are delievered to subscribers. +// BroadcastEvent represents events that are delievered to broadcast +// event subscribers. type BroadcastEvent struct { Type string Publisher *time.ActorID diff --git a/pkg/document/document.go b/pkg/document/document.go index 9e7b1fd36..ad4f8164d 100644 --- a/pkg/document/document.go +++ b/pkg/document/document.go @@ -19,6 +19,7 @@ package document import ( gojson "encoding/json" + "errors" "fmt" "github.com/yorkie-team/yorkie/pkg/document/change" @@ -30,6 +31,12 @@ import ( "github.com/yorkie-team/yorkie/pkg/document/time" ) +var ( + // ErrReservedEventType is returned when the event type is "document". + ErrReservedEventType = errors.New( + "the \"document\" event type is reserved for document events") +) + // DocEvent represents the event that occurred in the document. type DocEvent struct { Type DocEventType @@ -377,6 +384,10 @@ func (d *Document) BroadcastRequests() <-chan BroadcastRequest { // Broadcast encodes the payload and makes a "Broadcast" type request. func (d *Document) Broadcast(eventType string, payload any) error { + if eventType == "document" { + return ErrReservedEventType + } + marshaled, err := gojson.Marshal(payload) if err != nil { return fmt.Errorf("marshal payload in broadcast event: %w", err) @@ -395,26 +406,36 @@ func (d *Document) Broadcast(eventType string, payload any) error { func (d *Document) SubscribeBroadcastEvent( eventType string, handler func(eventType, publisher string, payload []byte) error, -) { +) error { + if eventType == "document" { + return ErrReservedEventType + } + d.broadcastEventHandlers[eventType] = handler d.broadcastRequests <- BroadcastRequest{ RequestType: Subscribe, EventType: eventType, } + return nil } // UnsubscribeBroadcastEvent deregisters the event handler and makes // a "Unsubscribe" type request. func (d *Document) UnsubscribeBroadcastEvent( eventType string, -) { +) error { + if eventType == "document" { + return ErrReservedEventType + } + delete(d.broadcastEventHandlers, eventType) d.broadcastRequests <- BroadcastRequest{ RequestType: Unsubscribe, EventType: eventType, } + return nil } // BroadcastEventHandlers returns registered event handlers for events. diff --git a/test/integration/document_test.go b/test/integration/document_test.go index c424a4df9..87bf0a7b3 100644 --- a/test/integration/document_test.go +++ b/test/integration/document_test.go @@ -435,13 +435,15 @@ func TestDocument(t *testing.T) { assert.NoError(t, c1.Attach(ctx, d1)) rch1, err := c1.Watch(ctx, d1) assert.NoError(t, err) - d1.SubscribeBroadcastEvent("mention", handler) + err = d1.SubscribeBroadcastEvent("mention", handler) + assert.NoError(t, err) d2 := document.New(helper.TestDocKey(t)) assert.NoError(t, c2.Attach(ctx, d2)) rch2, err := c2.Watch(ctx, d2) assert.NoError(t, err) - d2.SubscribeBroadcastEvent("mention", handler) + err = d2.SubscribeBroadcastEvent("mention", handler) + assert.NoError(t, err) err = d2.Broadcast("mention", "yorkie") assert.NoError(t, err) @@ -472,6 +474,7 @@ func TestDocument(t *testing.T) { // TODO(sejongk): broadcast to multiple subscribers // TODO(sejongk): dont broadcast to unsubscribers + // TODO(sejongk): reject subscribing document for broadcast } func TestDocumentWithProjects(t *testing.T) {