~whereswaldon/wisteria

5c1c1cff11e2966561d4e041d114b0ac5bb8270d — Chris Waldon 2 months ago 53a00c8
feat: ensure we don't block in replylist subscription
1 files changed, 15 insertions(+), 12 deletions(-)

M replylist/replylist.go
M replylist/replylist.go => replylist/replylist.go +15 -12
@@ 34,20 34,23 @@ func New(s store.ExtendedStore) (*ReplyList, error) {
// you if you construct the ReplyList that way.
func (r *ReplyList) SubscribeTo(s store.ExtendedStore) error {
	s.SubscribeToNewMessages(func(node forest.Node) {
		r.Lock()
		defer r.Unlock()
		if reply, ok := node.(*forest.Reply); ok {
			alreadyInList := false
			for _, element := range r.replies {
				if element.Equals(reply) {
					alreadyInList = true
					break
		// cannot block in subscription
		go func() {
			r.Lock()
			defer r.Unlock()
			if reply, ok := node.(*forest.Reply); ok {
				alreadyInList := false
				for _, element := range r.replies {
					if element.Equals(reply) {
						alreadyInList = true
						break
					}
				}
				if !alreadyInList {
					r.replies = append(r.replies, reply)
				}
			}
			if !alreadyInList {
				r.replies = append(r.replies, reply)
			}
		}
		}()
	})
	const defaultArchiveReplyListLen = 1024