Skip to content

Commit

Permalink
feat: publish Subscribe function
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Meier <[email protected]>
  • Loading branch information
astromechza committed Nov 4, 2024
1 parent cfa6e49 commit c522956
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ help:
## Execute Go tests with gotestsum (go install gotest.tools/gotestsum@latest)
.PHONY: test
test:
gotestsum --format testname
gotestsum --format testname -- -race

## Lint Go code with golangci-lint (install this if you don't have it)
.PHONY: lint
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (b *SharedDoc) HttpPushPullChanges(ctx context.Context, url string, opts ..
wg := new(sync.WaitGroup)
defer wg.Wait()

sub, fin := b.subscribeToReceivedChanges()
sub, fin := b.SubscribeToReceivedChanges()
defer fin()

ctx, cancel := context.WithCancel(ctx)
Expand Down
2 changes: 1 addition & 1 deletion read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNotifyPossibleChanges_with_sub(t *testing.T) {
t.Parallel()

a := NewSharedDoc(automerge.New())
sub, fin := a.subscribeToReceivedChanges()
sub, fin := a.SubscribeToReceivedChanges()

t.Run("no message available by default", func(t *testing.T) {
select {
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (b *SharedDoc) ServeChanges(rw http.ResponseWriter, req *http.Request, opts
wg := new(sync.WaitGroup)
defer wg.Wait()

sub, fin := b.subscribeToReceivedChanges()
sub, fin := b.SubscribeToReceivedChanges()
defer fin()

// We piggyback on the context and ensure we cancel it before waiting for the wait group.
Expand Down
4 changes: 3 additions & 1 deletion write.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/automerge/automerge-go"
)

func (b *SharedDoc) subscribeToReceivedChanges() (chan bool, func()) {
// SubscribeToReceivedChanges allows the caller to subscribe to changes received by the doc. Call the finish function
// to clean up.
func (b *SharedDoc) SubscribeToReceivedChanges() (chan bool, func()) {
b.mutex.Lock()
defer b.mutex.Unlock()
if b.channels == nil {
Expand Down

0 comments on commit c522956

Please sign in to comment.