Mercurial > eldonilo > lightstring
comparison lightstring.js @ 43:136df1708856
Better iq callbacks. (breaks everything)
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 31 Jan 2012 18:12:19 +0100 |
parents | ee874d064650 |
children | 3dfb596cf669 |
comparison
equal
deleted
inserted
replaced
42:ee874d064650 | 43:136df1708856 |
---|---|
307 | 307 |
308 if (stanza.DOM.tagName === 'iq') { | 308 if (stanza.DOM.tagName === 'iq') { |
309 var payload = stanza.DOM.firstChild; | 309 var payload = stanza.DOM.firstChild; |
310 if (payload) | 310 if (payload) |
311 that.emit('iq/' + payload.namespaceURI + ':' + payload.localName, stanza); | 311 that.emit('iq/' + payload.namespaceURI + ':' + payload.localName, stanza); |
312 that.emit(stanza.DOM.getAttribute('id'), stanza); //FIXME: possible attack vector. | 312 |
313 var id = stanza.DOM.getAttributeNS(null, 'id'); | |
314 if (!(id && id in that.callbacks)) | |
315 return; | |
316 | |
317 var type = stanza.DOM.getAttributeNS(null, 'type'); | |
318 if (type !== 'result' || type !== 'error') | |
319 return; //TODO: emit an warning, perhaps. | |
320 | |
321 var callback = that.callbacks[id]; | |
322 if (type === 'result' && callback.result) | |
323 callback.result(stanza); | |
324 else if (type === 'error' && callback.error) | |
325 callback.error(stanza); | |
326 | |
327 delete that.callbacks[id]; | |
313 | 328 |
314 //TODO: really needed? | 329 //TODO: really needed? |
315 } else if (stanza.DOM.tagName === 'message') { | 330 } else if (stanza.DOM.tagName === 'message') { |
316 var payloads = stanza.DOM.children; | 331 var payloads = stanza.DOM.children; |
317 for (var i = 0; i < payloads.length; i++) | 332 for (var i = 0; i < payloads.length; i++) |
322 /** | 337 /** |
323 * @function Send a message. | 338 * @function Send a message. |
324 * @param {String|Object} aStanza The message to send. | 339 * @param {String|Object} aStanza The message to send. |
325 * @param {Function} [aCallback] Executed on answer. (stanza must be iq) | 340 * @param {Function} [aCallback] Executed on answer. (stanza must be iq) |
326 */ | 341 */ |
327 send: function(aStanza, aCallback) { | 342 send: function(aStanza, aResult, aError) { |
328 if (!(aStanza instanceof Lightstring.Stanza)) | 343 if (!(aStanza instanceof Lightstring.Stanza)) |
329 var stanza = new Lightstring.Stanza(aStanza); | 344 var stanza = new Lightstring.Stanza(aStanza); |
330 else | 345 else |
331 var stanza = aStanza; | 346 var stanza = aStanza; |
332 | 347 |
336 if (stanza.DOM.tagName === 'iq') { | 351 if (stanza.DOM.tagName === 'iq') { |
337 var type = stanza.DOM.getAttributeNS(null, 'type'); | 352 var type = stanza.DOM.getAttributeNS(null, 'type'); |
338 if (type !== 'get' || type !== 'set') | 353 if (type !== 'get' || type !== 'set') |
339 ; //TODO: emit an error. | 354 ; //TODO: emit an error. |
340 | 355 |
356 var callback = {result: aResult, error: aError}; | |
357 | |
341 var id = stanza.DOM.getAttributeNS(null, 'id'); | 358 var id = stanza.DOM.getAttributeNS(null, 'id'); |
342 if (!id) { | 359 if (!id) |
343 if (aCallback) | 360 ; //TODO: emit an warning. |
344 ; //TODO: emit an error. | 361 else |
345 else | 362 this.callback[id] = callback; |
346 ; //TODO: emit an warning. | 363 |
347 } else if (aCallback) | 364 } else if (aResult || aError) |
348 this.on(id, aCallback); | |
349 } | |
350 else if (aCallback) | |
351 ; //TODO: callback can’t be called with non-iq stanza. | 365 ; //TODO: callback can’t be called with non-iq stanza. |
352 | 366 |
353 | 367 |
354 //TODO this.socket.send(stanza.XML); (need some work on Lightstring.Stanza) | 368 //TODO this.socket.send(stanza.XML); (need some work on Lightstring.Stanza) |
355 var fixme = Lightstring.DOM2XML(stanza.DOM); | 369 var fixme = Lightstring.DOM2XML(stanza.DOM); |
377 if (!handlers) | 391 if (!handlers) |
378 return; | 392 return; |
379 | 393 |
380 for (var i = 0; i < handlers.length; i++) | 394 for (var i = 0; i < handlers.length; i++) |
381 handlers[i].call(this, aData); | 395 handlers[i].call(this, aData); |
382 | |
383 if (aName.match('sendiq:')) | |
384 delete this.handlers[aName]; | |
385 }, | 396 }, |
386 /** | 397 /** |
387 * @function Register an event handler. | 398 * @function Register an event handler. |
388 * @param {String} aName The event name. | 399 * @param {String} aName The event name. |
389 * @param {Function} aCallback The callback to call when the event is emitted. | 400 * @param {Function} aCallback The callback to call when the event is emitted. |
391 on: function(aName, callback) { | 402 on: function(aName, callback) { |
392 if (!this.handlers[aName]) | 403 if (!this.handlers[aName]) |
393 this.handlers[aName] = []; | 404 this.handlers[aName] = []; |
394 this.handlers[aName].push(callback); | 405 this.handlers[aName].push(callback); |
395 } | 406 } |
396 //FIXME do this! | |
397 //~ this.once = function(name, callback) { | |
398 //~ if(!this.handlers[name]) | |
399 //~ this.handlers[name] = []; | |
400 //~ this.handlers[name].push(callback); | |
401 //~ }; | |
402 }; | 407 }; |