topic.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.lang.async.topic"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.async.topic"] = true;
  8. dojo.provide("dojox.lang.async.topic");
  9. // Source of Deferred for topics
  10. (function(){
  11. var d = dojo, topic = dojox.lang.async.topic;
  12. topic.from = function(topic){
  13. return function(){
  14. var h, cancel = function(){
  15. if(h){
  16. d.unsubscribe(h);
  17. h = null;
  18. }
  19. },
  20. x = new d.Deferred(cancel);
  21. h = d.subscribe(topic, function(){
  22. cancel();
  23. x.callback(arguments);
  24. });
  25. return x;
  26. };
  27. };
  28. topic.failOn = function(topic){
  29. return function(){
  30. var h, cancel = function(){
  31. if(h){
  32. d.unsubscribe(h);
  33. h = null;
  34. }
  35. },
  36. x = new d.Deferred(cancel);
  37. h = d.subscribe(topic, function(evt){
  38. cancel();
  39. x.errback(new Error(arguments));
  40. });
  41. return x;
  42. };
  43. };
  44. })();
  45. }