From af91060278bc9538f2eed1d18fef0de823550d42 Mon Sep 17 00:00:00 2001 From: NorthLan <6995syu@163.com> Date: Fri, 27 May 2022 10:21:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8F=90=E4=BE=9BMember=E7=9A=84?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E6=96=B9=E6=B3=95=EF=BC=8C=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E4=BB=8EsessionId=E8=8E=B7=E5=8F=96session=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group.go | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/group.go b/group.go index 2af8402..1cd5053 100644 --- a/group.go +++ b/group.go @@ -51,24 +51,48 @@ func (c *Group) Member(uid int64) (*session.Session, error) { return nil, ErrMemberNotFound } -// Members returns all members in current group -func (c *Group) Members() map[int64]*session.Session { +// MemberBySID returns specified sId's session +func (c *Group) MemberBySID(id int64) (*session.Session, error) { c.mu.RLock() defer c.mu.RUnlock() - return c.sessions + for sId, s := range c.sessions { + if sId == id { + return s, nil + } + } + + return nil, ErrMemberNotFound +} + +func (c *Group) Members() []*session.Session { + var resp []*session.Session + c.PeekMembers(func(_ int64, s *session.Session) bool { + resp = append(resp, s) + return true + }) + return resp } -// MemberIDs returns all member's UID in current group -func (c *Group) MemberIDs() []int64 { +// PeekMembers returns all members in current group +func (c *Group) PeekMembers(fn func(sId int64, s *session.Session) bool) { c.mu.RLock() defer c.mu.RUnlock() - var members []int64 - for _, s := range c.sessions { - members = append(members, s.UID()) + for sId, s := range c.sessions { + if !fn(sId, s) { + break + } } +} +// MemberUIDs returns all member's UID in current group +func (c *Group) MemberUIDs() []int64 { + var members []int64 + c.PeekMembers(func(_ int64, s *session.Session) bool { + members = append(members, s.UID()) + return false + }) return members }