Kernel-Promises.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 (aBlock){
  117. var self=this;
  118. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  119. return $core.withContext(function($ctx1) {
  120. //>>excludeEnd("ctx");
  121. return self.then(function (result) {
  122. return aBlock._value_(result);
  123. });
  124. return self;
  125. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  126. }, function($ctx1) {$ctx1.fill(self,"then:",{aBlock:aBlock},$globals.Thenable)});
  127. //>>excludeEnd("ctx");
  128. },
  129. //>>excludeStart("ide", pragmas.excludeIdeData);
  130. args: ["aBlock"],
  131. source: "then: aBlock\x0a<return self.then(function (result) {\x0a return aBlock._value_(result);\x0a})>",
  132. referencedClasses: [],
  133. //>>excludeEnd("ide");
  134. messageSends: []
  135. }),
  136. $globals.Thenable);
  137. $core.addMethod(
  138. $core.method({
  139. selector: "all:",
  140. protocol: '*Kernel-Promises',
  141. fn: function (nadicBlock){
  142. var self=this;
  143. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  144. return $core.withContext(function($ctx1) {
  145. //>>excludeEnd("ctx");
  146. var js = self["@jsObject"];
  147. if (typeof js.then === "function")
  148. return $globals.Thenable.fn.prototype._all_.call(js, nadicBlock);
  149. else
  150. return self._doesNotUnderstand_(
  151. $globals.Message._new()
  152. ._selector_("all:")
  153. ._arguments_([nadicBlock])
  154. );
  155. return self;
  156. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  157. }, function($ctx1) {$ctx1.fill(self,"all:",{nadicBlock:nadicBlock},$globals.JSObjectProxy)});
  158. //>>excludeEnd("ctx");
  159. },
  160. //>>excludeStart("ide", pragmas.excludeIdeData);
  161. args: ["nadicBlock"],
  162. 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 )>",
  163. referencedClasses: [],
  164. //>>excludeEnd("ide");
  165. messageSends: []
  166. }),
  167. $globals.JSObjectProxy);
  168. $core.addMethod(
  169. $core.method({
  170. selector: "catch:",
  171. protocol: '*Kernel-Promises',
  172. fn: function (aBlock){
  173. var self=this;
  174. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  175. return $core.withContext(function($ctx1) {
  176. //>>excludeEnd("ctx");
  177. var js = self["@jsObject"];
  178. if (typeof js.then === "function")
  179. return $globals.Thenable.fn.prototype._catch_.call(js, aBlock);
  180. else
  181. return self._doesNotUnderstand_(
  182. $globals.Message._new()
  183. ._selector_("catch:")
  184. ._arguments_([aBlock])
  185. );
  186. return self;
  187. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  188. }, function($ctx1) {$ctx1.fill(self,"catch:",{aBlock:aBlock},$globals.JSObjectProxy)});
  189. //>>excludeEnd("ctx");
  190. },
  191. //>>excludeStart("ide", pragmas.excludeIdeData);
  192. args: ["aBlock"],
  193. 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 )>",
  194. referencedClasses: [],
  195. //>>excludeEnd("ide");
  196. messageSends: []
  197. }),
  198. $globals.JSObjectProxy);
  199. $core.addMethod(
  200. $core.method({
  201. selector: "on:do:",
  202. protocol: '*Kernel-Promises',
  203. fn: function (aClass,aBlock){
  204. var self=this;
  205. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  206. return $core.withContext(function($ctx1) {
  207. //>>excludeEnd("ctx");
  208. var js = self["@jsObject"];
  209. if (typeof js.then === "function")
  210. return $globals.Thenable.fn.prototype._on_do_.call(js, aClass, aBlock);
  211. else
  212. return self._doesNotUnderstand_(
  213. $globals.Message._new()
  214. ._selector_("on:do:")
  215. ._arguments_([aClass, aBlock])
  216. );
  217. return self;
  218. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  219. }, function($ctx1) {$ctx1.fill(self,"on:do:",{aClass:aClass,aBlock:aBlock},$globals.JSObjectProxy)});
  220. //>>excludeEnd("ctx");
  221. },
  222. //>>excludeStart("ide", pragmas.excludeIdeData);
  223. args: ["aClass", "aBlock"],
  224. 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 )>",
  225. referencedClasses: [],
  226. //>>excludeEnd("ide");
  227. messageSends: []
  228. }),
  229. $globals.JSObjectProxy);
  230. $core.addMethod(
  231. $core.method({
  232. selector: "on:do:catch:",
  233. protocol: '*Kernel-Promises',
  234. fn: function (aClass,aBlock,anotherBlock){
  235. var self=this;
  236. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  237. return $core.withContext(function($ctx1) {
  238. //>>excludeEnd("ctx");
  239. var js = self["@jsObject"];
  240. if (typeof js.then === "function")
  241. return $globals.Thenable.fn.prototype._on_do_catch_.call(js, aClass, aBlock, anotherBlock);
  242. else
  243. return self._doesNotUnderstand_(
  244. $globals.Message._new()
  245. ._selector_("on:do:catch:")
  246. ._arguments_([aClass, aBlock, anotherBlock])
  247. );
  248. return self;
  249. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  250. }, function($ctx1) {$ctx1.fill(self,"on:do:catch:",{aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock},$globals.JSObjectProxy)});
  251. //>>excludeEnd("ctx");
  252. },
  253. //>>excludeStart("ide", pragmas.excludeIdeData);
  254. args: ["aClass", "aBlock", "anotherBlock"],
  255. 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 )>",
  256. referencedClasses: [],
  257. //>>excludeEnd("ide");
  258. messageSends: []
  259. }),
  260. $globals.JSObjectProxy);
  261. $core.addMethod(
  262. $core.method({
  263. selector: "then:",
  264. protocol: '*Kernel-Promises',
  265. fn: function (aBlock){
  266. var self=this;
  267. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  268. return $core.withContext(function($ctx1) {
  269. //>>excludeEnd("ctx");
  270. var js = self["@jsObject"];
  271. if (typeof js.then === "function")
  272. return $globals.Thenable.fn.prototype._then_.call(js, aBlock);
  273. else
  274. return self._doesNotUnderstand_(
  275. $globals.Message._new()
  276. ._selector_("then:")
  277. ._arguments_([aBlock])
  278. );
  279. return self;
  280. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  281. }, function($ctx1) {$ctx1.fill(self,"then:",{aBlock:aBlock},$globals.JSObjectProxy)});
  282. //>>excludeEnd("ctx");
  283. },
  284. //>>excludeStart("ide", pragmas.excludeIdeData);
  285. args: ["aBlock"],
  286. 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 )>",
  287. referencedClasses: [],
  288. //>>excludeEnd("ide");
  289. messageSends: []
  290. }),
  291. $globals.JSObjectProxy);
  292. });