@@ -85,9 +85,9 @@ type Attachment struct {
8585
8686 isSubscribed atomic.Bool
8787
88- rch <- chan WatchResponse
89- ctx context.Context
90- cancelFunc context.CancelFunc
88+ rch <- chan WatchResponse
89+ watchCtx context.Context
90+ closeWatchStream context.CancelFunc
9191}
9292
9393// Client is a normal client that can communicate with the server.
@@ -333,8 +333,8 @@ func (c *Client) Attach(ctx context.Context, doc *document.Document, options ...
333333 }
334334
335335 watchCtx , cancelFunc := context .WithCancel (ctx )
336- c .attachments [doc .Key ()].ctx = watchCtx
337- c .attachments [doc .Key ()].cancelFunc = cancelFunc
336+ c .attachments [doc .Key ()].watchCtx = watchCtx
337+ c .attachments [doc .Key ()].closeWatchStream = cancelFunc
338338
339339 err = c .runWatchLoop (watchCtx , doc )
340340 if err != nil {
@@ -365,7 +365,7 @@ func (c *Client) Detach(ctx context.Context, doc *document.Document, options ...
365365 return ErrDocumentNotAttached
366366 }
367367
368- attachment .cancelFunc ()
368+ attachment .closeWatchStream ()
369369
370370 if err := doc .Update (func (root * json.Object , p * presence.Presence ) error {
371371 p .Clear ()
@@ -442,12 +442,13 @@ func (c *Client) Subscribe(
442442 }
443443 attachment .isSubscribed .Store (true )
444444
445- return attachment .rch , attachment .cancelFunc , nil
445+ return attachment .rch , attachment .closeWatchStream , nil
446446}
447447
448+ // TODO(krapie): need restart logic if watch loop is closed.
448449// runWatchLoop subscribes to events on a given documentIDs.
449450// If an error occurs before stream initialization, the second response, error,
450- // is returned. If the context "ctx " is canceled or timed out, returned channel
451+ // is returned. If the context "watchCtx " is canceled or timed out, returned channel
451452// is closed, and "WatchResponse" from this closed channel has zero events and
452453// nil "Err()".
453454func (c * Client ) runWatchLoop (
0 commit comments