/flight/domain/AsyncCommand.as

https://code.google.com/ · ActionScript · 107 lines · 64 code · 14 blank · 29 comment · 10 complexity · 21177647dcbb70d7f0d259513f2f3013 MD5 · raw file

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2009 Tyler Wright, Robert Taylor, Jacob Wright
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. ////////////////////////////////////////////////////////////////////////////////
  24. package flight.domain
  25. {
  26. import flash.events.Event;
  27. import flash.utils.clearTimeout;
  28. import flash.utils.setTimeout;
  29. import flight.commands.IAsyncCommand;
  30. import flight.net.IResponse;
  31. import flight.net.Response;
  32. [Event(name="complete", type="flash.events.Event")]
  33. [Event(name="cancel", type="flash.events.Event")]
  34. /**
  35. * An abstract command class that supports Asynchronous commands through dispatching
  36. * the ExecutionComplete event upon completion of the asynchronous action.
  37. */
  38. public class AsyncCommand extends Command implements IAsyncCommand
  39. {
  40. private var interval:uint;
  41. private var _response:IResponse;
  42. [Bindable(event="responseChange")]
  43. public function get response():IResponse
  44. {
  45. if (_response == null) {
  46. // set response through the setter for appropriate handlers
  47. response = new Response();
  48. }
  49. return _response;
  50. }
  51. public function set response(value:IResponse):void
  52. {
  53. if (_response == value) {
  54. return;
  55. }
  56. if (_response != null) {
  57. _response.removeResultHandler(onResult);
  58. _response.removeFaultHandler(onFault);
  59. // link old response to the new response
  60. if (value != null) {
  61. _response.progress = value.progress;
  62. _response.status = value.status;
  63. value.addResultHandler(_response.complete);
  64. value.addFaultHandler(_response.cancel);
  65. }
  66. }
  67. var oldValue:IResponse = _response;
  68. _response = value;
  69. if (_response != null) {
  70. _response.addResultHandler(onResult);
  71. _response.addFaultHandler(onFault);
  72. }
  73. propertyChange("response", oldValue, _response);
  74. }
  75. private function complete():void
  76. {
  77. dispatchEvent(new Event(Event.COMPLETE));
  78. }
  79. private function cancel():void
  80. {
  81. dispatchEvent(new Event(Event.CANCEL));
  82. }
  83. private function onResult(data:Object):void
  84. {
  85. interval = setTimeout(complete, 1);
  86. }
  87. private function onFault(error:Error):void
  88. {
  89. clearTimeout(interval);
  90. setTimeout(cancel, 1);
  91. }
  92. }
  93. }