@@ -13,6 +13,7 @@ import (
1313 "fmt"
1414 "iter"
1515 "log"
16+ "maps"
1617 "net/url"
1718 "path/filepath"
1819 "slices"
@@ -43,7 +44,7 @@ type Server struct {
4344 sessions []* ServerSession
4445 sendingMethodHandler_ MethodHandler [* ServerSession ]
4546 receivingMethodHandler_ MethodHandler [* ServerSession ]
46- resourceSubscriptions map [string ]map [string ]bool // uri -> session ID -> bool
47+ resourceSubscriptions map [string ]map [* ServerSession ]bool // uri -> session -> bool
4748}
4849
4950// ServerOptions is used to configure behavior of the server.
@@ -108,7 +109,7 @@ func NewServer(impl *Implementation, opts *ServerOptions) *Server {
108109 resourceTemplates : newFeatureSet (func (t * serverResourceTemplate ) string { return t .resourceTemplate .URITemplate }),
109110 sendingMethodHandler_ : defaultSendingMethodHandler [* ServerSession ],
110111 receivingMethodHandler_ : defaultReceivingMethodHandler [* ServerSession ],
111- resourceSubscriptions : make (map [string ]map [string ]bool ),
112+ resourceSubscriptions : make (map [string ]map [* ServerSession ]bool ),
112113 }
113114}
114115
@@ -441,19 +442,13 @@ func fileResourceHandler(dir string) ResourceHandler {
441442 }
442443}
443444
445+ // ResourceUpdated sends a notification to all clients that have subscribed to the
446+ // resource specified in params. This method is the primary way for a
447+ // server author to signal that a resource has changed.
444448func (s * Server ) ResourceUpdated (ctx context.Context , params * ResourceUpdatedNotificationParams ) error {
445449 s .mu .Lock ()
446- subscribedSessionIDs := s .resourceSubscriptions [params .URI ]
447- if len (subscribedSessionIDs ) == 0 {
448- s .mu .Unlock ()
449- return nil
450- }
451- var sessions []* ServerSession
452- for _ , session := range s .sessions {
453- if _ , ok := subscribedSessionIDs [session .ID ()]; ok {
454- sessions = append (sessions , session )
455- }
456- }
450+ subscribedSessions := s .resourceSubscriptions [params .URI ]
451+ sessions := slices .Collect (maps .Keys (subscribedSessions ))
457452 s .mu .Unlock ()
458453 notifySessions (sessions , notificationResourceUpdated , params )
459454 return nil
@@ -466,12 +461,14 @@ func (s *Server) subscribe(ctx context.Context, ss *ServerSession, params *Subsc
466461 if err := s .opts .SubscribeHandler (ctx , params ); err != nil {
467462 return nil , err
468463 }
464+
469465 s .mu .Lock ()
470466 defer s .mu .Unlock ()
471467 if s .resourceSubscriptions [params .URI ] == nil {
472- s .resourceSubscriptions [params .URI ] = make (map [string ]bool )
468+ s .resourceSubscriptions [params .URI ] = make (map [* ServerSession ]bool )
473469 }
474- s .resourceSubscriptions [params .URI ][ss .ID ()] = true
470+ s.resourceSubscriptions [params.URI ][ss ] = true
471+
475472 return & emptyResult {}, nil
476473}
477474
@@ -486,10 +483,9 @@ func (s *Server) unsubscribe(ctx context.Context, ss *ServerSession, params *Uns
486483
487484 s .mu .Lock ()
488485 defer s .mu .Unlock ()
489-
490- if subscribedSessionIDs , ok := s .resourceSubscriptions [params .URI ]; ok {
491- delete (subscribedSessionIDs , ss .ID ())
492- if len (subscribedSessionIDs ) == 0 {
486+ if subscribedSessions , ok := s .resourceSubscriptions [params .URI ]; ok {
487+ delete (subscribedSessions , ss )
488+ if len (subscribedSessions ) == 0 {
493489 delete (s .resourceSubscriptions , params .URI )
494490 }
495491 }
@@ -545,8 +541,8 @@ func (s *Server) disconnect(cc *ServerSession) {
545541 return cc2 == cc
546542 })
547543
548- for _ , subscribedSessionIDs := range s .resourceSubscriptions {
549- delete (subscribedSessionIDs , cc . ID () )
544+ for _ , subscribedSessions := range s .resourceSubscriptions {
545+ delete (subscribedSessions , cc )
550546 }
551547}
552548
0 commit comments