Kernel-Promises.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.addMethod(
  141. $core.method({
  142. selector: "then:catch:",
  143. protocol: 'promises',
  144. fn: function (aBlockOrArray,anotherBlock){
  145. var self=this;
  146. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  147. return $core.withContext(function($ctx1) {
  148. //>>excludeEnd("ctx");
  149. return $recv(self._then_(aBlockOrArray))._catch_(anotherBlock);
  150. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  151. }, function($ctx1) {$ctx1.fill(self,"then:catch:",{aBlockOrArray:aBlockOrArray,anotherBlock:anotherBlock},$globals.Thenable)});
  152. //>>excludeEnd("ctx");
  153. },
  154. //>>excludeStart("ide", pragmas.excludeIdeData);
  155. args: ["aBlockOrArray", "anotherBlock"],
  156. source: "then: aBlockOrArray catch: anotherBlock\x0a\x09^ (self then: aBlockOrArray) catch: anotherBlock",
  157. referencedClasses: [],
  158. //>>excludeEnd("ide");
  159. messageSends: ["catch:", "then:"]
  160. }),
  161. $globals.Thenable);
  162. $core.addMethod(
  163. $core.method({
  164. selector: "then:on:do:",
  165. protocol: 'promises',
  166. fn: function (aBlockOrArray,aClass,aBlock){
  167. var self=this;
  168. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  169. return $core.withContext(function($ctx1) {
  170. //>>excludeEnd("ctx");
  171. return $recv(self._then_(aBlockOrArray))._on_do_(aClass,aBlock);
  172. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  173. }, function($ctx1) {$ctx1.fill(self,"then:on:do:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock},$globals.Thenable)});
  174. //>>excludeEnd("ctx");
  175. },
  176. //>>excludeStart("ide", pragmas.excludeIdeData);
  177. args: ["aBlockOrArray", "aClass", "aBlock"],
  178. source: "then: aBlockOrArray on: aClass do: aBlock\x0a\x09^ (self then: aBlockOrArray) on: aClass do: aBlock",
  179. referencedClasses: [],
  180. //>>excludeEnd("ide");
  181. messageSends: ["on:do:", "then:"]
  182. }),
  183. $globals.Thenable);
  184. $core.addMethod(
  185. $core.method({
  186. selector: "then:on:do:catch:",
  187. protocol: 'promises',
  188. fn: function (aBlockOrArray,aClass,aBlock,anotherBlock){
  189. var self=this;
  190. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  191. return $core.withContext(function($ctx1) {
  192. //>>excludeEnd("ctx");
  193. return $recv($recv(self._then_(aBlockOrArray))._on_do_(aClass,aBlock))._catch_(anotherBlock);
  194. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  195. }, function($ctx1) {$ctx1.fill(self,"then:on:do:catch:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock},$globals.Thenable)});
  196. //>>excludeEnd("ctx");
  197. },
  198. //>>excludeStart("ide", pragmas.excludeIdeData);
  199. args: ["aBlockOrArray", "aClass", "aBlock", "anotherBlock"],
  200. source: "then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock\x0a\x09^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock",
  201. referencedClasses: [],
  202. //>>excludeEnd("ide");
  203. messageSends: ["catch:", "on:do:", "then:"]
  204. }),
  205. $globals.Thenable);
  206. $core.addClass('Promise', $globals.Thenable, [], 'Kernel-Promises');
  207. $core.addMethod(
  208. $core.method({
  209. selector: "all:",
  210. protocol: '*Kernel-Promises',
  211. fn: function (nadicBlock){
  212. var self=this;
  213. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  214. return $core.withContext(function($ctx1) {
  215. //>>excludeEnd("ctx");
  216. var js = self["@jsObject"];
  217. if (typeof js.then === "function")
  218. return $globals.Thenable.fn.prototype._all_.call(js, nadicBlock);
  219. else
  220. return self._doesNotUnderstand_(
  221. $globals.Message._new()
  222. ._selector_("all:")
  223. ._arguments_([nadicBlock])
  224. );
  225. return self;
  226. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  227. }, function($ctx1) {$ctx1.fill(self,"all:",{nadicBlock:nadicBlock},$globals.JSObjectProxy)});
  228. //>>excludeEnd("ctx");
  229. },
  230. //>>excludeStart("ide", pragmas.excludeIdeData);
  231. args: ["nadicBlock"],
  232. 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 )>",
  233. referencedClasses: [],
  234. //>>excludeEnd("ide");
  235. messageSends: []
  236. }),
  237. $globals.JSObjectProxy);
  238. $core.addMethod(
  239. $core.method({
  240. selector: "catch:",
  241. protocol: '*Kernel-Promises',
  242. fn: function (aBlock){
  243. var self=this;
  244. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  245. return $core.withContext(function($ctx1) {
  246. //>>excludeEnd("ctx");
  247. var js = self["@jsObject"];
  248. if (typeof js.then === "function")
  249. return $globals.Thenable.fn.prototype._catch_.call(js, aBlock);
  250. else
  251. return self._doesNotUnderstand_(
  252. $globals.Message._new()
  253. ._selector_("catch:")
  254. ._arguments_([aBlock])
  255. );
  256. return self;
  257. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  258. }, function($ctx1) {$ctx1.fill(self,"catch:",{aBlock:aBlock},$globals.JSObjectProxy)});
  259. //>>excludeEnd("ctx");
  260. },
  261. //>>excludeStart("ide", pragmas.excludeIdeData);
  262. args: ["aBlock"],
  263. 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 )>",
  264. referencedClasses: [],
  265. //>>excludeEnd("ide");
  266. messageSends: []
  267. }),
  268. $globals.JSObjectProxy);
  269. $core.addMethod(
  270. $core.method({
  271. selector: "on:do:",
  272. protocol: '*Kernel-Promises',
  273. fn: function (aClass,aBlock){
  274. var self=this;
  275. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  276. return $core.withContext(function($ctx1) {
  277. //>>excludeEnd("ctx");
  278. var js = self["@jsObject"];
  279. if (typeof js.then === "function")
  280. return $globals.Thenable.fn.prototype._on_do_.call(js, aClass, aBlock);
  281. else
  282. return self._doesNotUnderstand_(
  283. $globals.Message._new()
  284. ._selector_("on:do:")
  285. ._arguments_([aClass, aBlock])
  286. );
  287. return self;
  288. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  289. }, function($ctx1) {$ctx1.fill(self,"on:do:",{aClass:aClass,aBlock:aBlock},$globals.JSObjectProxy)});
  290. //>>excludeEnd("ctx");
  291. },
  292. //>>excludeStart("ide", pragmas.excludeIdeData);
  293. args: ["aClass", "aBlock"],
  294. 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 )>",
  295. referencedClasses: [],
  296. //>>excludeEnd("ide");
  297. messageSends: []
  298. }),
  299. $globals.JSObjectProxy);
  300. $core.addMethod(
  301. $core.method({
  302. selector: "on:do:catch:",
  303. protocol: '*Kernel-Promises',
  304. fn: function (aClass,aBlock,anotherBlock){
  305. var self=this;
  306. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  307. return $core.withContext(function($ctx1) {
  308. //>>excludeEnd("ctx");
  309. var js = self["@jsObject"];
  310. if (typeof js.then === "function")
  311. return $globals.Thenable.fn.prototype._on_do_catch_.call(js, aClass, aBlock, anotherBlock);
  312. else
  313. return self._doesNotUnderstand_(
  314. $globals.Message._new()
  315. ._selector_("on:do:catch:")
  316. ._arguments_([aClass, aBlock, anotherBlock])
  317. );
  318. return self;
  319. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  320. }, function($ctx1) {$ctx1.fill(self,"on:do:catch:",{aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock},$globals.JSObjectProxy)});
  321. //>>excludeEnd("ctx");
  322. },
  323. //>>excludeStart("ide", pragmas.excludeIdeData);
  324. args: ["aClass", "aBlock", "anotherBlock"],
  325. 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 )>",
  326. referencedClasses: [],
  327. //>>excludeEnd("ide");
  328. messageSends: []
  329. }),
  330. $globals.JSObjectProxy);
  331. $core.addMethod(
  332. $core.method({
  333. selector: "then:",
  334. protocol: '*Kernel-Promises',
  335. fn: function (aBlock){
  336. var self=this;
  337. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  338. return $core.withContext(function($ctx1) {
  339. //>>excludeEnd("ctx");
  340. var js = self["@jsObject"];
  341. if (typeof js.then === "function")
  342. return $globals.Thenable.fn.prototype._then_.call(js, aBlock);
  343. else
  344. return self._doesNotUnderstand_(
  345. $globals.Message._new()
  346. ._selector_("then:")
  347. ._arguments_([aBlock])
  348. );
  349. return self;
  350. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  351. }, function($ctx1) {$ctx1.fill(self,"then:",{aBlock:aBlock},$globals.JSObjectProxy)});
  352. //>>excludeEnd("ctx");
  353. },
  354. //>>excludeStart("ide", pragmas.excludeIdeData);
  355. args: ["aBlock"],
  356. 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 )>",
  357. referencedClasses: [],
  358. //>>excludeEnd("ide");
  359. messageSends: []
  360. }),
  361. $globals.JSObjectProxy);
  362. });