From 685719fb9bfc7fe0c608dd4fffa99d17824344be Mon Sep 17 00:00:00 2001 From: rachel-mp4 Date: Sun, 6 Jul 2025 09:33:46 -0400 Subject: [PATCH] add initialID option --- options.go | 18 +++++++++++++----- server.go | 18 ++++++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/options.go b/options.go index ed0512b..6f4418e 100644 --- a/options.go +++ b/options.go @@ -9,11 +9,12 @@ import ( ) type options struct { - welcome *string - writer *io.Writer - verbose bool - pubChan chan PubEvent - initChan chan lrcpb.Event_Init + welcome *string + writer *io.Writer + verbose bool + pubChan chan PubEvent + initChan chan lrcpb.Event_Init + initialID *uint32 } type Option func(option *options) error @@ -28,6 +29,13 @@ func WithWelcome(welcome string) Option { } } +func WithInitialID(id uint32) Option { + return func(options *options) error { + options.initialID = &id + return nil + } +} + func WithLogging(w io.Writer, verbose bool) Option { return func(options *options) error { if w == nil { diff --git a/server.go b/server.go index 839eab5..28f5c7e 100644 --- a/server.go +++ b/server.go @@ -6,10 +6,10 @@ import ( "github.com/gorilla/websocket" "github.com/rachel-mp4/lrcproto/gen/go" "google.golang.org/protobuf/proto" - "unicode/utf16" "log" "net/http" "sync" + "unicode/utf16" ) type Server struct { @@ -84,13 +84,15 @@ func NewServer(opts ...Option) (*Server, error) { if options.pubChan != nil { s.pubChan = options.pubChan } + if options.initialID != nil { + s.lastID = *options.initialID + } s.clients = make(map[*client]bool) s.clientsMu = sync.Mutex{} s.idmapsMu = sync.Mutex{} s.clientToID = make(map[*client]*uint32) s.idToClient = make(map[uint32]*client) - s.lastID = 0 s.eventBus = make(chan clientEvent, 100) return &s, nil } @@ -117,17 +119,17 @@ func (s *Server) Start() error { } // Stop stops a server if it has started, and returns an error if it is already stopped -func (s *Server) Stop() error { +func (s *Server) Stop() (uint32, error) { if s.ctx == nil { - return nil + return s.lastID, nil } select { case <-s.ctx.Done(): - return errors.New("cannot stop already stopped server") + return s.lastID, errors.New("cannot stop already stopped server") default: s.cancel() s.logDebug("Goodbye world :c") - return nil + return s.lastID, nil } } @@ -404,7 +406,7 @@ func insertAtUTF16Index(base string, index uint32, insert string) (string, error insertRunes := []rune(insert) insertUTF16Units := utf16.Encode(insertRunes) - result := make([]uint16, 0, len(baseUTF16Units) + len(insertUTF16Units)) + result := make([]uint16, 0, len(baseUTF16Units)+len(insertUTF16Units)) result = append(result, baseUTF16Units[:index]...) result = append(result, insertUTF16Units...) result = append(result, baseUTF16Units[index:]...) @@ -436,7 +438,7 @@ func deleteBtwnUTF16Indices(base string, start uint32, end uint32) (string, erro if uint32(len(baseUTF16Units)) < end { return "", errors.New("index out of range") } - result := make([]uint16, 0, uint32(len(baseUTF16Units)) + start - end) + result := make([]uint16, 0, uint32(len(baseUTF16Units))+start-end) result = append(result, baseUTF16Units[:start]...) result = append(result, baseUTF16Units[end:]...) resultRunes := utf16.Decode(result) -- 2.51.2