Changeset

347:48cc6cad9bd6

plugins.pubsub: Keep track of wrapped callbacks
author Kim Alvefur <zash@zash.se>
date Sat, 06 Jul 2013 08:38:31 +0200
parents 346:f7854dd16ed3
children 348:34b878d58948
files plugins/pubsub.lua
diffstat 1 files changed, 10 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/pubsub.lua	Sat Jul 06 08:35:37 2013 +0200
+++ b/plugins/pubsub.lua	Sat Jul 06 08:38:31 2013 +0200
@@ -140,6 +140,7 @@
 end
 
 function pubsub_node:hook(callback, prio)
+	self._hooks = self._hooks or setmetatable({}, { __mode = 'kv' });
 	local function hook(event)
 		-- FIXME service == nil would mean anyone,
 		-- publishing would be go to your bare jid.
@@ -149,12 +150,20 @@
 			return callback(event)
 		end
 	end
+	self._hooks[callback] = hook;
 	self.stream:hook("pubsub/event", hook, prio);
 	return hook;
 end
 
 function pubsub_node:unhook(callback)
-	self.stream:unhook("pubsub/event", callback);
+	if callback then
+		local hook = self._hooks[callback];
+		self.stream:unhook("pubsub/event", hook);
+	elseif self._hooks then
+		for hook in pairs(self._hooks) do
+			self.stream:unhook("pubsub/event", hook);
+		end
+	end
 end
 
 function pubsub_node:create(config, callback)