Kernel-Promises.js 14 KB

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