Browse Source

Implement HasTopic.HasTopic

Volodymyr Tkach 2 years ago
parent
commit
cb964c9df7
2 changed files with 26 additions and 0 deletions
  1. 16 0
      pubsub/pubsub.go
  2. 10 0
      pubsub/pubsub_test.go

+ 16 - 0
pubsub/pubsub.go

@@ -144,6 +144,22 @@ func (p *PubSub) Unlisten(topic string, params ...interface{}) {
 	}
 }
 
+// HasTopic returns true if topic present.
+func (p *PubSub) HasTopic(topic string, params ...interface{}) bool {
+	p.Lock()
+	defer p.Unlock()
+
+	t := p.Topic(topic, params...)
+
+	for _, c := range p.Connections {
+		if c.HasTopic(t) {
+			return true
+		}
+	}
+
+	return false
+}
+
 // Topic generate correct topic for API.
 // Params can be as number or string.
 //

+ 10 - 0
pubsub/pubsub_test.go

@@ -71,6 +71,16 @@ var _ = Describe("PubSub", func() {
 			})
 		})
 
+		Context("HasTopic", func() {
+			It("checks topics", func() {
+				Expect(len(ps.Connections)).To(Equal(0))
+
+				ps.Listen("community-points-channel-v1", 1)
+				Expect(ps.HasTopic("unknown")).To(BeFalse())
+				Expect(ps.HasTopic("community-points-channel-v1", 1)).To(BeTrue())
+			})
+		})
+
 		Context("Topic", func() {
 			It("generate correct topic", func() {
 				Expect(ps.Topic("channel-bits-events-v1.123")).To(Equal("channel-bits-events-v1.123"))