Skip to content

Commit

Permalink
Refuse to subscribe to document type for broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
sejongk committed Sep 6, 2023
1 parent 8b2a9a7 commit b59928d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
3 changes: 2 additions & 1 deletion api/types/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions pkg/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package document

import (
gojson "encoding/json"
"errors"
"fmt"

"github.com/yorkie-team/yorkie/pkg/document/change"
Expand All @@ -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
Expand Down Expand Up @@ -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" {

Check failure on line 387 in pkg/document/document.go

View workflow job for this annotation

GitHub Actions / build

string `document` has 3 occurrences, make it a constant (goconst)
return ErrReservedEventType
}

marshaled, err := gojson.Marshal(payload)
if err != nil {
return fmt.Errorf("marshal payload in broadcast event: %w", err)
Expand All @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions test/integration/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b59928d

Please sign in to comment.