Kernel-Promises.js 14 KB

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