Skip to content

Commit 43bc7bc

Browse files
authored
Merge pull request #42 from python36/separate_call_callbacks
Fixed a bug when subscribing and unsubscribing in callback handling
2 parents d2ca770 + af0d070 commit 43bc7bc

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

nmqtt.nim

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -835,18 +835,19 @@ proc onPublish(ctx: MqttCtx, pkt: Pkt) {.async.} =
835835
verbose("Retained ", mqttbroker.retained)
836836

837837
when not defined(broker):
838+
var callbacks: seq[PubCallback]
838839
for top, cb in ctx.pubCallbacks:
839840
if top == topic or top == "#":
840-
cb.cb(topic, message)
841+
callbacks.add(cb)
841842
if top.endsWith("/#"):
842843
# the multi-level wildcard can represent zero levels.
843844
if topic == top[0 .. ^3]:
844-
cb.cb(topic, message)
845+
callbacks.add(cb)
845846
continue
846847
var topicw = top
847848
topicw.removeSuffix("#")
848849
if topic.contains(topicw):
849-
cb.cb(topic, message)
850+
callbacks.add(cb)
850851
if top.contains("+"):
851852
var topelem = split(top, '/')
852853
if len(topelem) == count(topic, '/') + 1:
@@ -855,7 +856,9 @@ proc onPublish(ctx: MqttCtx, pkt: Pkt) {.async.} =
855856
if topelem[i] != "+" and e != topelem[i]: break
856857
i = i+1
857858
if i == len(topelem):
858-
cb.cb(topic, message)
859+
callbacks.add(cb)
860+
for cb in callbacks:
861+
cb.cb(topic, message)
859862

860863
if qos == 1:
861864
ctx.workQueue[msgId] = Work(wk: PubWork, msgId: msgId, state: WorkNew, qos: 1, typ: PubAck)

0 commit comments

Comments
 (0)