commit 7ad515d7a439b10c896198c8baf67235926bd157
parent e0a162299ee6c690553aaefbf0f372b15d43cf04
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date: Mon, 1 Sep 2025 17:48:12 +0000
Unsubscribe method
Diffstat:
1 file changed, 16 insertions(+), 0 deletions(-)
diff --git a/natsbot.go b/natsbot.go
@@ -441,6 +441,7 @@ func wrapSubs(L *lua.LState, fn lua.LValue, ns *nats.Subscription, nc *nats.Conn
L.SetField(index, "publish", L.NewFunction(natsPublish))
L.SetField(index, "subscribe", L.NewFunction(natsSubscribe))
+ L.SetField(index, "unsubscribe", L.NewFunction(natsUnsubscribe))
mt := L.NewTable()
L.SetField(mt, "__call", fn)
@@ -479,6 +480,21 @@ func checkSubs(L *lua.LState, index int) *natsSubs {
return nil
}
+func natsUnsubscribe(L *lua.LState) int {
+ s := checkSubs(L, 1)
+ count := L.OptInt(2, 0)
+
+ if err := s.subs.AutoUnsubscribe(count); err != nil {
+ log.Println("Unsubscribe:", err)
+ L.Push(lua.LNil)
+ L.Push(lua.LString(err.Error()))
+ return 2
+ } else {
+ L.Push(lua.LTrue)
+ return 1
+ }
+}
+
/********** Lua Object for timers **********/
const luaTimerTypeName = "timer"