Kernel-Promises.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. define(["amber/boot", "amber_core/Kernel-Objects"], function($boot){"use strict";
  2. if(!("nilAsValue" in $boot))$boot.nilAsValue=$boot.nilAsReceiver;
  3. var $core=$boot.api,nil=$boot.nilAsValue,$nil=$boot.nilAsReceiver,$recv=$boot.asReceiver,$globals=$boot.globals;
  4. $core.addPackage("Kernel-Promises");
  5. ($core.packageDescriptors||$core.packages)["Kernel-Promises"].innerEval = function (expr) { return eval(expr); };
  6. ($core.packageDescriptors||$core.packages)["Kernel-Promises"].transport = {"type":"amd","amdNamespace":"amber_core"};
  7. $core.addClass("Promise", $globals.Object, [], "Kernel-Promises");
  8. $core.addMethod(
  9. $core.method({
  10. selector: "all:",
  11. protocol: "composites",
  12. fn: function (aCollection){
  13. var self=this,$self=this;
  14. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  15. return $core.withContext(function($ctx1) {
  16. //>>excludeEnd("ctx");
  17. return Promise.all($recv(aCollection)._asArray());
  18. return self;
  19. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  20. }, function($ctx1) {$ctx1.fill(self,"all:",{aCollection:aCollection},$globals.Promise.a$cls)});
  21. //>>excludeEnd("ctx");
  22. },
  23. //>>excludeStart("ide", pragmas.excludeIdeData);
  24. args: ["aCollection"],
  25. source: "all: aCollection\x0a\x22Returns a Promise resolved with results of sub-promises.\x22\x0a<inlineJS: 'return Promise.all($recv(aCollection)._asArray())'>",
  26. referencedClasses: [],
  27. //>>excludeEnd("ide");
  28. messageSends: []
  29. }),
  30. $globals.Promise.a$cls);
  31. $core.addMethod(
  32. $core.method({
  33. selector: "any:",
  34. protocol: "composites",
  35. fn: function (aCollection){
  36. var self=this,$self=this;
  37. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  38. return $core.withContext(function($ctx1) {
  39. //>>excludeEnd("ctx");
  40. return Promise.race($recv(aCollection)._asArray());
  41. return self;
  42. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  43. }, function($ctx1) {$ctx1.fill(self,"any:",{aCollection:aCollection},$globals.Promise.a$cls)});
  44. //>>excludeEnd("ctx");
  45. },
  46. //>>excludeStart("ide", pragmas.excludeIdeData);
  47. args: ["aCollection"],
  48. source: "any: aCollection\x0a\x22Returns a Promise resolved with first result of sub-promises.\x22\x0a<inlineJS: 'return Promise.race($recv(aCollection)._asArray())'>",
  49. referencedClasses: [],
  50. //>>excludeEnd("ide");
  51. messageSends: []
  52. }),
  53. $globals.Promise.a$cls);
  54. $core.addMethod(
  55. $core.method({
  56. selector: "forBlock:",
  57. protocol: "instance creation",
  58. fn: function (aBlock){
  59. var self=this,$self=this;
  60. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  61. return $core.withContext(function($ctx1) {
  62. //>>excludeEnd("ctx");
  63. return $recv($self._new())._then_(aBlock);
  64. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  65. }, function($ctx1) {$ctx1.fill(self,"forBlock:",{aBlock:aBlock},$globals.Promise.a$cls)});
  66. //>>excludeEnd("ctx");
  67. },
  68. //>>excludeStart("ide", pragmas.excludeIdeData);
  69. args: ["aBlock"],
  70. source: "forBlock: aBlock\x0a\x22Returns a Promise that is resolved with the value of aBlock,\x0aand rejected if error happens while evaluating aBlock.\x22\x0a\x09^ self new then: aBlock",
  71. referencedClasses: [],
  72. //>>excludeEnd("ide");
  73. messageSends: ["then:", "new"]
  74. }),
  75. $globals.Promise.a$cls);
  76. $core.addMethod(
  77. $core.method({
  78. selector: "new",
  79. protocol: "instance creation",
  80. fn: function (){
  81. var self=this,$self=this;
  82. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  83. return $core.withContext(function($ctx1) {
  84. //>>excludeEnd("ctx");
  85. return Promise.resolve();
  86. return self;
  87. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  88. }, function($ctx1) {$ctx1.fill(self,"new",{},$globals.Promise.a$cls)});
  89. //>>excludeEnd("ctx");
  90. },
  91. //>>excludeStart("ide", pragmas.excludeIdeData);
  92. args: [],
  93. source: "new\x0a\x22Returns a dumb Promise resolved with nil.\x22\x0a<inlineJS: 'return Promise.resolve()'>",
  94. referencedClasses: [],
  95. //>>excludeEnd("ide");
  96. messageSends: []
  97. }),
  98. $globals.Promise.a$cls);
  99. $core.addMethod(
  100. $core.method({
  101. selector: "new:",
  102. protocol: "instance creation",
  103. fn: function (aBlock){
  104. var self=this,$self=this;
  105. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  106. return $core.withContext(function($ctx1) {
  107. //>>excludeEnd("ctx");
  108. return new Promise(function (resolve, reject) {
  109. var model = {value: resolve, signal: reject};
  110. aBlock._value_(model);
  111. });
  112. return self;
  113. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  114. }, function($ctx1) {$ctx1.fill(self,"new:",{aBlock:aBlock},$globals.Promise.a$cls)});
  115. //>>excludeEnd("ctx");
  116. },
  117. //>>excludeStart("ide", pragmas.excludeIdeData);
  118. args: ["aBlock"],
  119. source: "new: aBlock\x0a\x22Returns a Promise that is eventually resolved or rejected.\x0aPass a block that is called with one argument, model.\x0aYou should call model value: ... to resolve the promise\x0aand model signal: ... to reject the promise.\x0aIf error happens during run of the block,\x0apromise is rejected with that error as well.\x22\x0a<inlineJS: 'return new Promise(function (resolve, reject) {\x0a var model = {value: resolve, signal: reject};\x0a aBlock._value_(model);\x0a})'>",
  120. referencedClasses: [],
  121. //>>excludeEnd("ide");
  122. messageSends: []
  123. }),
  124. $globals.Promise.a$cls);
  125. $core.addMethod(
  126. $core.method({
  127. selector: "signal:",
  128. protocol: "instance creation",
  129. fn: function (anObject){
  130. var self=this,$self=this;
  131. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  132. return $core.withContext(function($ctx1) {
  133. //>>excludeEnd("ctx");
  134. return $recv(anObject)._in_(function (x) {return Promise.reject(x)});
  135. return self;
  136. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  137. }, function($ctx1) {$ctx1.fill(self,"signal:",{anObject:anObject},$globals.Promise.a$cls)});
  138. //>>excludeEnd("ctx");
  139. },
  140. //>>excludeStart("ide", pragmas.excludeIdeData);
  141. args: ["anObject"],
  142. source: "signal: anObject\x0a\x22Returns a Promise rejected with anObject.\x22\x0a<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>",
  143. referencedClasses: [],
  144. //>>excludeEnd("ide");
  145. messageSends: []
  146. }),
  147. $globals.Promise.a$cls);
  148. $core.addMethod(
  149. $core.method({
  150. selector: "value:",
  151. protocol: "instance creation",
  152. fn: function (anObject){
  153. var self=this,$self=this;
  154. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  155. return $core.withContext(function($ctx1) {
  156. //>>excludeEnd("ctx");
  157. return $recv(anObject)._in_(function (x) {return Promise.resolve(x)});
  158. return self;
  159. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  160. }, function($ctx1) {$ctx1.fill(self,"value:",{anObject:anObject},$globals.Promise.a$cls)});
  161. //>>excludeEnd("ctx");
  162. },
  163. //>>excludeStart("ide", pragmas.excludeIdeData);
  164. args: ["anObject"],
  165. source: "value: anObject\x0a\x22Returns a Promise resolved with anObject.\x22\x0a<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>",
  166. referencedClasses: [],
  167. //>>excludeEnd("ide");
  168. messageSends: []
  169. }),
  170. $globals.Promise.a$cls);
  171. $core.addTrait("TThenable", "Kernel-Promises");
  172. $core.addMethod(
  173. $core.method({
  174. selector: "catch:",
  175. protocol: "promises",
  176. fn: function (aBlock){
  177. var self=this,$self=this;
  178. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  179. return $core.withContext(function($ctx1) {
  180. //>>excludeEnd("ctx");
  181. return self.then(null, function (err) {return $core.seamless(function () {
  182. return aBlock._value_(err);
  183. })});
  184. return self;
  185. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  186. }, function($ctx1) {$ctx1.fill(self,"catch:",{aBlock:aBlock},$globals.TThenable)});
  187. //>>excludeEnd("ctx");
  188. },
  189. //>>excludeStart("ide", pragmas.excludeIdeData);
  190. args: ["aBlock"],
  191. source: "catch: aBlock\x0a<inlineJS: 'return self.then(null, function (err) {return $core.seamless(function () {\x0a return aBlock._value_(err);\x0a})})'>",
  192. referencedClasses: [],
  193. //>>excludeEnd("ide");
  194. messageSends: []
  195. }),
  196. $globals.TThenable);
  197. $core.addMethod(
  198. $core.method({
  199. selector: "on:do:",
  200. protocol: "promises",
  201. fn: function (aClass,aBlock){
  202. var self=this,$self=this;
  203. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  204. return $core.withContext(function($ctx1) {
  205. //>>excludeEnd("ctx");
  206. return self.then(null, function (err) {return $core.seamless(function () {
  207. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  208. else throw err;
  209. })});
  210. return self;
  211. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  212. }, function($ctx1) {$ctx1.fill(self,"on:do:",{aClass:aClass,aBlock:aBlock},$globals.TThenable)});
  213. //>>excludeEnd("ctx");
  214. },
  215. //>>excludeStart("ide", pragmas.excludeIdeData);
  216. args: ["aClass", "aBlock"],
  217. source: "on: aClass do: aBlock\x0a<inlineJS: 'return self.then(null, function (err) {return $core.seamless(function () {\x0a if (err._isKindOf_(aClass)) return aBlock._value_(err);\x0a else throw err;\x0a})})'>",
  218. referencedClasses: [],
  219. //>>excludeEnd("ide");
  220. messageSends: []
  221. }),
  222. $globals.TThenable);
  223. $core.addMethod(
  224. $core.method({
  225. selector: "on:do:catch:",
  226. protocol: "promises",
  227. fn: function (aClass,aBlock,anotherBlock){
  228. var self=this,$self=this;
  229. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  230. return $core.withContext(function($ctx1) {
  231. //>>excludeEnd("ctx");
  232. return $recv($self._on_do_(aClass,aBlock))._catch_(anotherBlock);
  233. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  234. }, function($ctx1) {$ctx1.fill(self,"on:do:catch:",{aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock},$globals.TThenable)});
  235. //>>excludeEnd("ctx");
  236. },
  237. //>>excludeStart("ide", pragmas.excludeIdeData);
  238. args: ["aClass", "aBlock", "anotherBlock"],
  239. source: "on: aClass do: aBlock catch: anotherBlock\x0a\x09^ (self on: aClass do: aBlock) catch: anotherBlock",
  240. referencedClasses: [],
  241. //>>excludeEnd("ide");
  242. messageSends: ["catch:", "on:do:"]
  243. }),
  244. $globals.TThenable);
  245. $core.addMethod(
  246. $core.method({
  247. selector: "then:",
  248. protocol: "promises",
  249. fn: function (aBlockOrArray){
  250. var self=this,$self=this;
  251. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  252. return $core.withContext(function($ctx1) {
  253. //>>excludeEnd("ctx");
  254. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  255. return array.reduce(function (soFar, aBlock) {
  256. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  257. function (result) {return $core.seamless(function () {
  258. if (Array.isArray(result)) {
  259. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  260. } else {
  261. return aBlock._value_(result);
  262. }
  263. })} :
  264. function (result) {return $core.seamless(function () {
  265. return aBlock._value_(result);
  266. })}
  267. );
  268. }, self);
  269. return self;
  270. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  271. }, function($ctx1) {$ctx1.fill(self,"then:",{aBlockOrArray:aBlockOrArray},$globals.TThenable)});
  272. //>>excludeEnd("ctx");
  273. },
  274. //>>excludeStart("ide", pragmas.excludeIdeData);
  275. args: ["aBlockOrArray"],
  276. source: "then: aBlockOrArray\x0a\x22Accepts a block or array of blocks.\x0aEach of blocks in the array or the singleton one is\x0aused in .then call to a promise, to accept a result\x0aand transform it to the result for the next one.\x0aIn case a block has more than one argument\x0aand result is an array, first n-1 elements of the array\x0aare put into additional arguments beyond the first.\x0aThe first argument always contains the result as-is.\x22\x0a<inlineJS: '\x0avar array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];\x0areturn array.reduce(function (soFar, aBlock) {\x0a return soFar.then(typeof aBlock === \x22function\x22 && aBlock.length > 1 ?\x0a function (result) {return $core.seamless(function () {\x0a if (Array.isArray(result)) {\x0a return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));\x0a } else {\x0a return aBlock._value_(result);\x0a }\x0a })} :\x0a function (result) {return $core.seamless(function () {\x0a return aBlock._value_(result);\x0a })}\x0a );\x0a}, self)'>",
  277. referencedClasses: [],
  278. //>>excludeEnd("ide");
  279. messageSends: []
  280. }),
  281. $globals.TThenable);
  282. $core.addMethod(
  283. $core.method({
  284. selector: "then:catch:",
  285. protocol: "promises",
  286. fn: function (aBlockOrArray,anotherBlock){
  287. var self=this,$self=this;
  288. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  289. return $core.withContext(function($ctx1) {
  290. //>>excludeEnd("ctx");
  291. return $recv($self._then_(aBlockOrArray))._catch_(anotherBlock);
  292. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  293. }, function($ctx1) {$ctx1.fill(self,"then:catch:",{aBlockOrArray:aBlockOrArray,anotherBlock:anotherBlock},$globals.TThenable)});
  294. //>>excludeEnd("ctx");
  295. },
  296. //>>excludeStart("ide", pragmas.excludeIdeData);
  297. args: ["aBlockOrArray", "anotherBlock"],
  298. source: "then: aBlockOrArray catch: anotherBlock\x0a\x09^ (self then: aBlockOrArray) catch: anotherBlock",
  299. referencedClasses: [],
  300. //>>excludeEnd("ide");
  301. messageSends: ["catch:", "then:"]
  302. }),
  303. $globals.TThenable);
  304. $core.addMethod(
  305. $core.method({
  306. selector: "then:on:do:",
  307. protocol: "promises",
  308. fn: function (aBlockOrArray,aClass,aBlock){
  309. var self=this,$self=this;
  310. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  311. return $core.withContext(function($ctx1) {
  312. //>>excludeEnd("ctx");
  313. return $recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock);
  314. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  315. }, function($ctx1) {$ctx1.fill(self,"then:on:do:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock},$globals.TThenable)});
  316. //>>excludeEnd("ctx");
  317. },
  318. //>>excludeStart("ide", pragmas.excludeIdeData);
  319. args: ["aBlockOrArray", "aClass", "aBlock"],
  320. source: "then: aBlockOrArray on: aClass do: aBlock\x0a\x09^ (self then: aBlockOrArray) on: aClass do: aBlock",
  321. referencedClasses: [],
  322. //>>excludeEnd("ide");
  323. messageSends: ["on:do:", "then:"]
  324. }),
  325. $globals.TThenable);
  326. $core.addMethod(
  327. $core.method({
  328. selector: "then:on:do:catch:",
  329. protocol: "promises",
  330. fn: function (aBlockOrArray,aClass,aBlock,anotherBlock){
  331. var self=this,$self=this;
  332. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  333. return $core.withContext(function($ctx1) {
  334. //>>excludeEnd("ctx");
  335. return $recv($recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock))._catch_(anotherBlock);
  336. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  337. }, function($ctx1) {$ctx1.fill(self,"then:on:do:catch:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock},$globals.TThenable)});
  338. //>>excludeEnd("ctx");
  339. },
  340. //>>excludeStart("ide", pragmas.excludeIdeData);
  341. args: ["aBlockOrArray", "aClass", "aBlock", "anotherBlock"],
  342. source: "then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock\x0a\x09^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock",
  343. referencedClasses: [],
  344. //>>excludeEnd("ide");
  345. messageSends: ["catch:", "on:do:", "then:"]
  346. }),
  347. $globals.TThenable);
  348. $core.setTraitComposition([{trait: $globals.TThenable}], $globals.Promise);
  349. });