Software /
code /
verse
Comparison
plugins/pubsub.lua @ 347:48cc6cad9bd6
plugins.pubsub: Keep track of wrapped callbacks
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 06 Jul 2013 08:38:31 +0200 |
parent | 346:f7854dd16ed3 |
child | 348:34b878d58948 |
comparison
equal
deleted
inserted
replaced
346:f7854dd16ed3 | 347:48cc6cad9bd6 |
---|---|
138 local s = self:service(service); | 138 local s = self:service(service); |
139 return node and s:node(node) or s; | 139 return node and s:node(node) or s; |
140 end | 140 end |
141 | 141 |
142 function pubsub_node:hook(callback, prio) | 142 function pubsub_node:hook(callback, prio) |
143 self._hooks = self._hooks or setmetatable({}, { __mode = 'kv' }); | |
143 local function hook(event) | 144 local function hook(event) |
144 -- FIXME service == nil would mean anyone, | 145 -- FIXME service == nil would mean anyone, |
145 -- publishing would be go to your bare jid. | 146 -- publishing would be go to your bare jid. |
146 -- So if you're only interestied in your own | 147 -- So if you're only interestied in your own |
147 -- events, hook your own bare jid. | 148 -- events, hook your own bare jid. |
148 if (not event.service or event.from == self.service) and event.node == self.node then | 149 if (not event.service or event.from == self.service) and event.node == self.node then |
149 return callback(event) | 150 return callback(event) |
150 end | 151 end |
151 end | 152 end |
153 self._hooks[callback] = hook; | |
152 self.stream:hook("pubsub/event", hook, prio); | 154 self.stream:hook("pubsub/event", hook, prio); |
153 return hook; | 155 return hook; |
154 end | 156 end |
155 | 157 |
156 function pubsub_node:unhook(callback) | 158 function pubsub_node:unhook(callback) |
157 self.stream:unhook("pubsub/event", callback); | 159 if callback then |
160 local hook = self._hooks[callback]; | |
161 self.stream:unhook("pubsub/event", hook); | |
162 elseif self._hooks then | |
163 for hook in pairs(self._hooks) do | |
164 self.stream:unhook("pubsub/event", hook); | |
165 end | |
166 end | |
158 end | 167 end |
159 | 168 |
160 function pubsub_node:create(config, callback) | 169 function pubsub_node:create(config, callback) |
161 if config ~= nil then | 170 if config ~= nil then |
162 error("Not implemented yet."); | 171 error("Not implemented yet."); |