@@ -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.
@@ -109,7 +110,7 @@ func NewServer(impl *Implementation, opts *ServerOptions) *Server {
109110 resourceTemplates : newFeatureSet (func (t * serverResourceTemplate ) string { return t .resourceTemplate .URITemplate }),
110111 sendingMethodHandler_ : defaultSendingMethodHandler [* ServerSession ],
111112 receivingMethodHandler_ : defaultReceivingMethodHandler [* ServerSession ],
112- resourceSubscriptions : make (map [string ]map [string ]bool ),
113+ resourceSubscriptions : make (map [string ]map [* ServerSession ]bool ),
113114 }
114115}
115116
@@ -442,19 +443,13 @@ func fileResourceHandler(dir string) ResourceHandler {
442443 }
443444}
444445
446+ // ResourceUpdated sends a notification to all clients that have subscribed to the
447+ // resource specified in params. This method is the primary way for a
448+ // server author to signal that a resource has changed.
445449func (s * Server ) ResourceUpdated (ctx context.Context , params * ResourceUpdatedNotificationParams ) error {
446450 s .mu .Lock ()
447- subscribedSessionIDs := s .resourceSubscriptions [params .URI ]
448- if len (subscribedSessionIDs ) == 0 {
449- s .mu .Unlock ()
450- return nil
451- }
452- var sessions []* ServerSession
453- for _ , session := range s .sessions {
454- if _ , ok := subscribedSessionIDs [session .ID ()]; ok {
455- sessions = append (sessions , session )
456- }
457- }
451+ subscribedSessions := s .resourceSubscriptions [params .URI ]
452+ sessions := slices .Collect (maps .Keys (subscribedSessions ))
458453 s .mu .Unlock ()
459454 notifySessions (sessions , notificationResourceUpdated , params )
460455 return nil
@@ -467,12 +462,14 @@ func (s *Server) subscribe(ctx context.Context, ss *ServerSession, params *Subsc
467462 if err := s .opts .SubscribeHandler (ctx , params ); err != nil {
468463 return nil , err
469464 }
465+
470466 s .mu .Lock ()
471467 defer s .mu .Unlock ()
472468 if s .resourceSubscriptions [params .URI ] == nil {
473- s .resourceSubscriptions [params .URI ] = make (map [string ]bool )
469+ s .resourceSubscriptions [params .URI ] = make (map [* ServerSession ]bool )
474470 }
475- s .resourceSubscriptions [params .URI ][ss .ID ()] = true
471+ s.resourceSubscriptions [params.URI ][ss ] = true
472+
476473 return & emptyResult {}, nil
477474}
478475
@@ -487,10 +484,9 @@ func (s *Server) unsubscribe(ctx context.Context, ss *ServerSession, params *Uns
487484
488485 s .mu .Lock ()
489486 defer s .mu .Unlock ()
490-
491- if subscribedSessionIDs , ok := s .resourceSubscriptions [params .URI ]; ok {
492- delete (subscribedSessionIDs , ss .ID ())
493- if len (subscribedSessionIDs ) == 0 {
487+ if subscribedSessions , ok := s .resourceSubscriptions [params .URI ]; ok {
488+ delete (subscribedSessions , ss )
489+ if len (subscribedSessions ) == 0 {
494490 delete (s .resourceSubscriptions , params .URI )
495491 }
496492 }
@@ -546,8 +542,8 @@ func (s *Server) disconnect(cc *ServerSession) {
546542 return cc2 == cc
547543 })
548544
549- for _ , subscribedSessionIDs := range s .resourceSubscriptions {
550- delete (subscribedSessionIDs , cc . ID () )
545+ for _ , subscribedSessions := range s .resourceSubscriptions {
546+ delete (subscribedSessions , cc )
551547 }
552548}
553549
0 commit comments