sync_client_disconnect.go

 1package ygo
 2
 3import "context"
 4
 5// DisconnectReason indicates why the sync client disconnected.
 6type DisconnectReason int
 7
 8const (
 9	// DisconnectReasonClientInitiated indicates the context was cancelled
10	// or the client was explicitly closed by the user.
11	DisconnectReasonClientInitiated DisconnectReason = iota
12
13	// DisconnectReasonYSweetInitiated indicates the y-sweet server closed
14	// the connection from its end.
15	DisconnectReasonYSweetInitiated
16
17	// DisconnectReasonDocumentIdle indicates the client disconnected because
18	// no other clients were connected and the idle timeout expired.
19	DisconnectReasonDocumentIdle
20)
21
22// String returns a human-readable description of the disconnect reason.
23func (r DisconnectReason) String() string {
24	switch r {
25	case DisconnectReasonClientInitiated:
26		return "ClientInitiated"
27	case DisconnectReasonYSweetInitiated:
28		return "YSweetInitiated"
29	case DisconnectReasonDocumentIdle:
30		return "DocumentIdle"
31	default:
32		return "Unknown"
33	}
34}
35
36// DisconnectHook is the callback signature for disconnect events with reason.
37type DisconnectHook func(doc *Doc, stats SyncStats, reason DisconnectReason) error
38
39// DisconnectHookCtx is the context-aware callback signature for disconnect events.
40type DisconnectHookCtx func(ctx context.Context, doc *Doc, stats SyncStats, reason DisconnectReason) error