Kernel-Promises.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. define("amber_core/Kernel-Promises", ["amber/boot", "amber_core/Kernel-Objects", "amber_core/Kernel-Infrastructure"], function($boot){"use strict";
  2. var $core=$boot.api,nil=$boot.nil,$recv=$boot.asReceiver,$globals=$boot.globals;
  3. $core.addPackage('Kernel-Promises');
  4. $core.packages["Kernel-Promises"].innerEval = function (expr) { return eval(expr); };
  5. $core.packages["Kernel-Promises"].transport = {"type":"amd","amdNamespace":"amber_core"};
  6. $core.addClass('Thenable', $globals.Object, [], 'Kernel-Promises');
  7. //>>excludeStart("ide", pragmas.excludeIdeData);
  8. $globals.Thenable.comment="I am the abstract base class for Promises.\x0a\x0aMy subclasses should wrap existing JS implementations.\x0a\x0aI contain methods that wrap Promises/A+ `.then` behaviour.";
  9. //>>excludeEnd("ide");
  10. $core.addMethod(
  11. $core.method({
  12. selector: "all:",
  13. protocol: 'promises',
  14. fn: function (nadicBlock){
  15. var self=this;
  16. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  17. return $core.withContext(function($ctx1) {
  18. //>>excludeEnd("ctx");
  19. return self.then(function (array) {
  20. return nadicBlock._valueWithPossibleArguments_(array);
  21. });
  22. return self;
  23. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  24. }, function($ctx1) {$ctx1.fill(self,"all:",{nadicBlock:nadicBlock},$globals.Thenable)});
  25. //>>excludeEnd("ctx");
  26. },
  27. //>>excludeStart("ide", pragmas.excludeIdeData);
  28. args: ["nadicBlock"],
  29. source: "all: nadicBlock\x0a<return self.then(function (array) {\x0a return nadicBlock._valueWithPossibleArguments_(array);\x0a})>",
  30. referencedClasses: [],
  31. //>>excludeEnd("ide");
  32. messageSends: []
  33. }),
  34. $globals.Thenable);
  35. $core.addMethod(
  36. $core.method({
  37. selector: "catch:",
  38. protocol: 'promises',
  39. fn: function (aBlock){
  40. var self=this;
  41. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  42. return $core.withContext(function($ctx1) {
  43. //>>excludeEnd("ctx");
  44. return self.then(null, function (err) {
  45. return aBlock._value_(err);
  46. });
  47. return self;
  48. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  49. }, function($ctx1) {$ctx1.fill(self,"catch:",{aBlock:aBlock},$globals.Thenable)});
  50. //>>excludeEnd("ctx");
  51. },
  52. //>>excludeStart("ide", pragmas.excludeIdeData);
  53. args: ["aBlock"],
  54. source: "catch: aBlock\x0a<return self.then(null, function (err) {\x0a return aBlock._value_(err);\x0a})>",
  55. referencedClasses: [],
  56. //>>excludeEnd("ide");
  57. messageSends: []
  58. }),
  59. $globals.Thenable);
  60. $core.addMethod(
  61. $core.method({
  62. selector: "on:do:",
  63. protocol: 'promises',
  64. fn: function (aClass,aBlock){
  65. var self=this;
  66. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  67. return $core.withContext(function($ctx1) {
  68. //>>excludeEnd("ctx");
  69. return self.then(null, function (err) {
  70. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  71. else throw err;
  72. });
  73. return self;
  74. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  75. }, function($ctx1) {$ctx1.fill(self,"on:do:",{aClass:aClass,aBlock:aBlock},$globals.Thenable)});
  76. //>>excludeEnd("ctx");
  77. },
  78. //>>excludeStart("ide", pragmas.excludeIdeData);
  79. args: ["aClass", "aBlock"],
  80. source: "on: aClass do: aBlock\x0a<return self.then(null, function (err) {\x0a if (err._isKindOf_(aClass)) return aBlock._value_(err);\x0a else throw err;\x0a})>",
  81. referencedClasses: [],
  82. //>>excludeEnd("ide");
  83. messageSends: []
  84. }),
  85. $globals.Thenable);
  86. $core.addMethod(
  87. $core.method({
  88. selector: "on:do:catch:",
  89. protocol: 'promises',
  90. fn: function (aClass,aBlock,anotherBlock){
  91. var self=this;
  92. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  93. return $core.withContext(function($ctx1) {
  94. //>>excludeEnd("ctx");
  95. return self.then(null, function (err) {
  96. try { if (err._isKindOf_(aClass)) return aBlock._value_(err); } catch (e) { err = e; }
  97. return anotherBlock._value_(err);
  98. });
  99. return self;
  100. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  101. }, function($ctx1) {$ctx1.fill(self,"on:do:catch:",{aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock},$globals.Thenable)});
  102. //>>excludeEnd("ctx");
  103. },
  104. //>>excludeStart("ide", pragmas.excludeIdeData);
  105. args: ["aClass", "aBlock", "anotherBlock"],
  106. source: "on: aClass do: aBlock catch: anotherBlock\x0a<return self.then(null, function (err) {\x0a try { if (err._isKindOf_(aClass)) return aBlock._value_(err); } catch (e) { err = e; }\x0a return anotherBlock._value_(err);\x0a})>",
  107. referencedClasses: [],
  108. //>>excludeEnd("ide");
  109. messageSends: []
  110. }),
  111. $globals.Thenable);
  112. $core.addMethod(
  113. $core.method({
  114. selector: "then:",
  115. protocol: 'promises',
  116. fn: function (aBlockOrArray){
  117. var self=this;
  118. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  119. return $core.withContext(function($ctx1) {
  120. //>>excludeEnd("ctx");
  121. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  122. return array.reduce(function (soFar, aBlock) {
  123. return soFar.then(function (result) {
  124. return aBlock._value_(result);
  125. });
  126. }, self);
  127. return self;
  128. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  129. }, function($ctx1) {$ctx1.fill(self,"then:",{aBlockOrArray:aBlockOrArray},$globals.Thenable)});
  130. //>>excludeEnd("ctx");
  131. },
  132. //>>excludeStart("ide", pragmas.excludeIdeData);
  133. args: ["aBlockOrArray"],
  134. source: "then: aBlockOrArray\x0a<\x0avar array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];\x0areturn array.reduce(function (soFar, aBlock) {\x0a return soFar.then(function (result) {\x0a return aBlock._value_(result);\x0a });\x0a}, self)>",
  135. referencedClasses: [],
  136. //>>excludeEnd("ide");
  137. messageSends: []
  138. }),
  139. $globals.Thenable);
  140. $core.addClass('Promise', $globals.Thenable, [], 'Kernel-Promises');
  141. $core.addMethod(
  142. $core.method({
  143. selector: "all:",
  144. protocol: '*Kernel-Promises',
  145. fn: function (nadicBlock){
  146. var self=this;
  147. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  148. return $core.withContext(function($ctx1) {
  149. //>>excludeEnd("ctx");
  150. var js = self["@jsObject"];
  151. if (typeof js.then === "function")
  152. return $globals.Thenable.fn.prototype._all_.call(js, nadicBlock);
  153. else
  154. return self._doesNotUnderstand_(
  155. $globals.Message._new()
  156. ._selector_("all:")
  157. ._arguments_([nadicBlock])
  158. );
  159. return self;
  160. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  161. }, function($ctx1) {$ctx1.fill(self,"all:",{nadicBlock:nadicBlock},$globals.JSObjectProxy)});
  162. //>>excludeEnd("ctx");
  163. },
  164. //>>excludeStart("ide", pragmas.excludeIdeData);
  165. args: ["nadicBlock"],
  166. source: "all: nadicBlock\x0a<var js = self[\x22@jsObject\x22];\x0aif (typeof js.then === \x22function\x22)\x0a return $globals.Thenable.fn.prototype._all_.call(js, nadicBlock);\x0aelse\x0a return self._doesNotUnderstand_(\x0a $globals.Message._new()\x0a ._selector_(\x22all:\x22)\x0a ._arguments_([nadicBlock])\x0a )>",
  167. referencedClasses: [],
  168. //>>excludeEnd("ide");
  169. messageSends: []
  170. }),
  171. $globals.JSObjectProxy);
  172. $core.addMethod(
  173. $core.method({
  174. selector: "catch:",
  175. protocol: '*Kernel-Promises',
  176. fn: function (aBlock){
  177. var self=this;
  178. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  179. return $core.withContext(function($ctx1) {
  180. //>>excludeEnd("ctx");
  181. var js = self["@jsObject"];
  182. if (typeof js.then === "function")
  183. return $globals.Thenable.fn.prototype._catch_.call(js, aBlock);
  184. else
  185. return self._doesNotUnderstand_(
  186. $globals.Message._new()
  187. ._selector_("catch:")
  188. ._arguments_([aBlock])
  189. );
  190. return self;
  191. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  192. }, function($ctx1) {$ctx1.fill(self,"catch:",{aBlock:aBlock},$globals.JSObjectProxy)});
  193. //>>excludeEnd("ctx");
  194. },
  195. //>>excludeStart("ide", pragmas.excludeIdeData);
  196. args: ["aBlock"],
  197. source: "catch: aBlock\x0a<var js = self[\x22@jsObject\x22];\x0aif (typeof js.then === \x22function\x22)\x0a return $globals.Thenable.fn.prototype._catch_.call(js, aBlock);\x0aelse\x0a return self._doesNotUnderstand_(\x0a $globals.Message._new()\x0a ._selector_(\x22catch:\x22)\x0a ._arguments_([aBlock])\x0a )>",
  198. referencedClasses: [],
  199. //>>excludeEnd("ide");
  200. messageSends: []
  201. }),
  202. $globals.JSObjectProxy);
  203. $core.addMethod(
  204. $core.method({
  205. selector: "on:do:",
  206. protocol: '*Kernel-Promises',
  207. fn: function (aClass,aBlock){
  208. var self=this;
  209. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  210. return $core.withContext(function($ctx1) {
  211. //>>excludeEnd("ctx");
  212. var js = self["@jsObject"];
  213. if (typeof js.then === "function")
  214. return $globals.Thenable.fn.prototype._on_do_.call(js, aClass, aBlock);
  215. else
  216. return self._doesNotUnderstand_(
  217. $globals.Message._new()
  218. ._selector_("on:do:")
  219. ._arguments_([aClass, aBlock])
  220. );
  221. return self;
  222. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  223. }, function($ctx1) {$ctx1.fill(self,"on:do:",{aClass:aClass,aBlock:aBlock},$globals.JSObjectProxy)});
  224. //>>excludeEnd("ctx");
  225. },
  226. //>>excludeStart("ide", pragmas.excludeIdeData);
  227. args: ["aClass", "aBlock"],
  228. source: "on: aClass do: aBlock\x0a<var js = self[\x22@jsObject\x22];\x0aif (typeof js.then === \x22function\x22)\x0a return $globals.Thenable.fn.prototype._on_do_.call(js, aClass, aBlock);\x0aelse\x0a return self._doesNotUnderstand_(\x0a $globals.Message._new()\x0a ._selector_(\x22on:do:\x22)\x0a ._arguments_([aClass, aBlock])\x0a )>",
  229. referencedClasses: [],
  230. //>>excludeEnd("ide");
  231. messageSends: []
  232. }),
  233. $globals.JSObjectProxy);
  234. $core.addMethod(
  235. $core.method({
  236. selector: "on:do:catch:",
  237. protocol: '*Kernel-Promises',
  238. fn: function (aClass,aBlock,anotherBlock){
  239. var self=this;
  240. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  241. return $core.withContext(function($ctx1) {
  242. //>>excludeEnd("ctx");
  243. var js = self["@jsObject"];
  244. if (typeof js.then === "function")
  245. return $globals.Thenable.fn.prototype._on_do_catch_.call(js, aClass, aBlock, anotherBlock);
  246. else
  247. return self._doesNotUnderstand_(
  248. $globals.Message._new()
  249. ._selector_("on:do:catch:")
  250. ._arguments_([aClass, aBlock, anotherBlock])
  251. );
  252. return self;
  253. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  254. }, function($ctx1) {$ctx1.fill(self,"on:do:catch:",{aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock},$globals.JSObjectProxy)});
  255. //>>excludeEnd("ctx");
  256. },
  257. //>>excludeStart("ide", pragmas.excludeIdeData);
  258. args: ["aClass", "aBlock", "anotherBlock"],
  259. source: "on: aClass do: aBlock catch: anotherBlock\x0a<var js = self[\x22@jsObject\x22];\x0aif (typeof js.then === \x22function\x22)\x0a return $globals.Thenable.fn.prototype._on_do_catch_.call(js, aClass, aBlock, anotherBlock);\x0aelse\x0a return self._doesNotUnderstand_(\x0a $globals.Message._new()\x0a ._selector_(\x22on:do:catch:\x22)\x0a ._arguments_([aClass, aBlock, anotherBlock])\x0a )>",
  260. referencedClasses: [],
  261. //>>excludeEnd("ide");
  262. messageSends: []
  263. }),
  264. $globals.JSObjectProxy);
  265. $core.addMethod(
  266. $core.method({
  267. selector: "then:",
  268. protocol: '*Kernel-Promises',
  269. fn: function (aBlock){
  270. var self=this;
  271. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  272. return $core.withContext(function($ctx1) {
  273. //>>excludeEnd("ctx");
  274. var js = self["@jsObject"];
  275. if (typeof js.then === "function")
  276. return $globals.Thenable.fn.prototype._then_.call(js, aBlock);
  277. else
  278. return self._doesNotUnderstand_(
  279. $globals.Message._new()
  280. ._selector_("then:")
  281. ._arguments_([aBlock])
  282. );
  283. return self;
  284. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  285. }, function($ctx1) {$ctx1.fill(self,"then:",{aBlock:aBlock},$globals.JSObjectProxy)});
  286. //>>excludeEnd("ctx");
  287. },
  288. //>>excludeStart("ide", pragmas.excludeIdeData);
  289. args: ["aBlock"],
  290. source: "then: aBlock\x0a<var js = self[\x22@jsObject\x22];\x0aif (typeof js.then === \x22function\x22)\x0a return $globals.Thenable.fn.prototype._then_.call(js, aBlock);\x0aelse\x0a return self._doesNotUnderstand_(\x0a $globals.Message._new()\x0a ._selector_(\x22then:\x22)\x0a ._arguments_([aBlock])\x0a )>",
  291. referencedClasses: [],
  292. //>>excludeEnd("ide");
  293. messageSends: []
  294. }),
  295. $globals.JSObjectProxy);
  296. });