PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/io/as/com/yui/util/io.as

https://github.com/nonano/yui3
ActionScript | 179 lines | 157 code | 22 blank | 0 comment | 22 complexity | e6e55f5ec270e7f68f00c4812ae53cd4 MD5 | raw file
  1. package com.yui.util
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import flash.events.IOErrorEvent;
  6. import flash.events.SecurityErrorEvent;
  7. import flash.events.TimerEvent;
  8. import flash.events.IEventDispatcher;
  9. import flash.net.URLRequest;
  10. import flash.net.URLRequestMethod;
  11. import flash.net.URLRequestHeader;
  12. import flash.net.URLLoader;
  13. import flash.net.URLVariables;
  14. import flash.utils.Timer;
  15. import flash.external.ExternalInterface;
  16. public class io extends Sprite
  17. {
  18. private var httpComplete:Function;
  19. private var httpError:Function;
  20. private var httpTimeout:Function;
  21. private var loaderMap:Object = {};
  22. private var vars:Object = root.loaderInfo.parameters;
  23. public function io() {
  24. ExternalInterface.addCallback("send", send);
  25. ExternalInterface.addCallback("abort", abort);
  26. ExternalInterface.addCallback("isInProgress", isInProgress);
  27. ExternalInterface.call('YUI.applyTo', vars.yid, 'io.xdrReady', [vars.yid, vars.uid]);
  28. }
  29. public function send(uri:String, cfg:Object):void {
  30. var loader:URLLoader = new URLLoader(),
  31. request:URLRequest = new URLRequest(uri),
  32. o:Object = { id: cfg.id, uid: cfg.uid },
  33. timer:Timer,
  34. k:String,
  35. p:String;
  36. for (p in cfg) {
  37. switch (p) {
  38. case "method":
  39. if(cfg.method === 'POST') {
  40. request.method = URLRequestMethod.POST;
  41. }
  42. break;
  43. case "data":
  44. if (cfg.method === 'POST') {
  45. request.data = cfg.data;
  46. }
  47. else {
  48. for (k in cfg.data) {
  49. request.data = new URLVariables(k + "=" + cfg.data[k]);
  50. }
  51. }
  52. break;
  53. case "headers":
  54. setRequestHeaders(request, cfg.headers);
  55. break;
  56. case "timeout":
  57. timer = new Timer(cfg.timeout, 1);
  58. break;
  59. }
  60. }
  61. loaderMap[cfg.id] = { c:loader, readyState: 0, t:timer };
  62. defineListeners(o, timer);
  63. addListeners(loader, timer);
  64. loader.load(request);
  65. start(o, timer);
  66. }
  67. private function defineListeners(o:Object, timer:Timer):void {
  68. httpComplete = function(e:Event):void { success(e, o, timer); };
  69. httpError = function(e:Event):void { failure(e, o, timer); };
  70. if (timer) {
  71. httpTimeout = function(e:TimerEvent):void { timeout(e, o.id); };
  72. }
  73. }
  74. private function addListeners(loader:IEventDispatcher, timer:IEventDispatcher):void {
  75. loader.addEventListener(Event.COMPLETE, httpComplete);
  76. loader.addEventListener(IOErrorEvent.IO_ERROR, httpError);
  77. loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, httpError);
  78. if (timer) {
  79. timer.addEventListener(TimerEvent.TIMER_COMPLETE, httpTimeout);
  80. }
  81. }
  82. private function removeListeners(id:uint):void {
  83. loaderMap[id].c.removeEventListener(Event.COMPLETE, httpComplete);
  84. loaderMap[id].c.removeEventListener(IOErrorEvent.IO_ERROR, httpError);
  85. loaderMap[id].c.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, httpError);
  86. if (loaderMap[id].t) {
  87. loaderMap[id].t.removeEventListener(TimerEvent.TIMER_COMPLETE, httpTimeout);
  88. }
  89. }
  90. private function start(o:Object, timer:Timer):void {
  91. if (timer) {
  92. timer.start();
  93. }
  94. loaderMap[o.id].readyState = 2;
  95. dispatch(['start', o, null]);
  96. }
  97. private function success(e:Event, o:Object, timer:Timer):void {
  98. o.c = { responseText: encodeURI(e.target.data) };
  99. loaderMap[o.id].readyState = 4;
  100. if (timer && timer.running) {
  101. timer.stop();
  102. }
  103. dispatch(['success', o]);
  104. destroy(o.id);
  105. }
  106. private function failure(e:Event, o:Object, timer:Timer):void {
  107. loaderMap[o.id].readyState = 4;
  108. if (timer && timer.running) {
  109. timer.stop();
  110. }
  111. if (e is IOErrorEvent) {
  112. o.c = { status: 0, statusText: e.type };
  113. }
  114. else if (e is SecurityErrorEvent) {
  115. o.c = { status: 0, statusText: 'Security Violation.' };
  116. }
  117. dispatch([e is TimerEvent ? 'timeout' : 'failure', o]);
  118. destroy(o.id);
  119. }
  120. public function abort(id:uint):void {
  121. loaderMap[id].c.close();
  122. if (loaderMap[id].t && loaderMap[id].t.running) {
  123. loaderMap[id].t.stop();
  124. }
  125. dispatch(['abort', { id: id }]);
  126. destroy(id);
  127. }
  128. public function isInProgress(id:uint):Boolean {
  129. if (loaderMap[id]) {
  130. return loaderMap[id].readyState !== 4;
  131. }
  132. else {
  133. return false;
  134. }
  135. }
  136. private function timeout(e:TimerEvent, id:uint):void {
  137. loaderMap[id].c.close();
  138. dispatch(['timeout', { id: id }]);
  139. destroy(id);
  140. }
  141. private function destroy(id:uint):void {
  142. removeListeners(id);
  143. delete loaderMap[id];
  144. }
  145. private function dispatch(a:Object):void {
  146. ExternalInterface.call('YUI.applyTo', vars.yid, 'io.xdrResponse', a);
  147. }
  148. private function setRequestHeaders(request:URLRequest, headers:Object):void {
  149. var header:URLRequestHeader,
  150. prop:String;
  151. for (prop in headers) {
  152. header = new URLRequestHeader(prop, headers[prop]);
  153. request.requestHeaders.push(header);
  154. }
  155. }
  156. }
  157. }