Kernel-Promises.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. define(["amber/boot", "require", "amber/core/Kernel-Objects"], function($boot,requirejs){"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})});
  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. pragmas: [["inlineJS:", ["return Promise.all($recv(aCollection)._asArray())"]]],
  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})});
  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. pragmas: [["inlineJS:", ["return Promise.race($recv(aCollection)._asArray())"]]],
  52. messageSends: []
  53. }),
  54. $globals.Promise.a$cls);
  55. $core.addMethod(
  56. $core.method({
  57. selector: "forBlock:",
  58. protocol: "instance creation",
  59. fn: function (aBlock){
  60. var self=this,$self=this;
  61. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  62. return $core.withContext(function($ctx1) {
  63. //>>excludeEnd("ctx");
  64. return $recv($self._new())._then_(aBlock);
  65. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  66. }, function($ctx1) {$ctx1.fill(self,"forBlock:",{aBlock:aBlock})});
  67. //>>excludeEnd("ctx");
  68. },
  69. //>>excludeStart("ide", pragmas.excludeIdeData);
  70. args: ["aBlock"],
  71. 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",
  72. referencedClasses: [],
  73. //>>excludeEnd("ide");
  74. pragmas: [],
  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",{})});
  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. pragmas: [["inlineJS:", ["return Promise.resolve()"]]],
  99. messageSends: []
  100. }),
  101. $globals.Promise.a$cls);
  102. $core.addMethod(
  103. $core.method({
  104. selector: "new:",
  105. protocol: "instance creation",
  106. fn: function (aBlock){
  107. var self=this,$self=this;
  108. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  109. return $core.withContext(function($ctx1) {
  110. //>>excludeEnd("ctx");
  111. return new Promise(function (resolve, reject) {
  112. var model = {value: resolve, signal: reject};
  113. aBlock._value_(model);
  114. });
  115. return self;
  116. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  117. }, function($ctx1) {$ctx1.fill(self,"new:",{aBlock:aBlock})});
  118. //>>excludeEnd("ctx");
  119. },
  120. //>>excludeStart("ide", pragmas.excludeIdeData);
  121. args: ["aBlock"],
  122. 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})'>",
  123. referencedClasses: [],
  124. //>>excludeEnd("ide");
  125. pragmas: [["inlineJS:", ["return new Promise(function (resolve, reject) {\x0a var model = {value: resolve, signal: reject};\x0a aBlock._value_(model);\x0a})"]]],
  126. messageSends: []
  127. }),
  128. $globals.Promise.a$cls);
  129. $core.addMethod(
  130. $core.method({
  131. selector: "signal:",
  132. protocol: "instance creation",
  133. fn: function (anObject){
  134. var self=this,$self=this;
  135. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  136. return $core.withContext(function($ctx1) {
  137. //>>excludeEnd("ctx");
  138. return $recv(anObject)._in_(function (x) {return Promise.reject(x)});
  139. return self;
  140. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  141. }, function($ctx1) {$ctx1.fill(self,"signal:",{anObject:anObject})});
  142. //>>excludeEnd("ctx");
  143. },
  144. //>>excludeStart("ide", pragmas.excludeIdeData);
  145. args: ["anObject"],
  146. source: "signal: anObject\x0a\x22Returns a Promise rejected with anObject.\x22\x0a<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>",
  147. referencedClasses: [],
  148. //>>excludeEnd("ide");
  149. pragmas: [["inlineJS:", ["return $recv(anObject)._in_(function (x) {return Promise.reject(x)})"]]],
  150. messageSends: []
  151. }),
  152. $globals.Promise.a$cls);
  153. $core.addMethod(
  154. $core.method({
  155. selector: "value:",
  156. protocol: "instance creation",
  157. fn: function (anObject){
  158. var self=this,$self=this;
  159. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  160. return $core.withContext(function($ctx1) {
  161. //>>excludeEnd("ctx");
  162. return $recv(anObject)._in_(function (x) {return Promise.resolve(x)});
  163. return self;
  164. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  165. }, function($ctx1) {$ctx1.fill(self,"value:",{anObject:anObject})});
  166. //>>excludeEnd("ctx");
  167. },
  168. //>>excludeStart("ide", pragmas.excludeIdeData);
  169. args: ["anObject"],
  170. source: "value: anObject\x0a\x22Returns a Promise resolved with anObject.\x22\x0a<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>",
  171. referencedClasses: [],
  172. //>>excludeEnd("ide");
  173. pragmas: [["inlineJS:", ["return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})"]]],
  174. messageSends: []
  175. }),
  176. $globals.Promise.a$cls);
  177. $core.addTrait("TThenable", "Kernel-Promises");
  178. $core.addMethod(
  179. $core.method({
  180. selector: "catch:",
  181. protocol: "promises",
  182. fn: function (aBlock){
  183. var self=this,$self=this;
  184. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  185. return $core.withContext(function($ctx1) {
  186. //>>excludeEnd("ctx");
  187. return self.then(null, function (err) {return $core.seamless(function () {
  188. return aBlock._value_(err);
  189. })});
  190. return self;
  191. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  192. }, function($ctx1) {$ctx1.fill(self,"catch:",{aBlock:aBlock})});
  193. //>>excludeEnd("ctx");
  194. },
  195. //>>excludeStart("ide", pragmas.excludeIdeData);
  196. args: ["aBlock"],
  197. source: "catch: aBlock\x0a<inlineJS: 'return self.then(null, function (err) {return $core.seamless(function () {\x0a return aBlock._value_(err);\x0a})})'>",
  198. referencedClasses: [],
  199. //>>excludeEnd("ide");
  200. pragmas: [["inlineJS:", ["return self.then(null, function (err) {return $core.seamless(function () {\x0a return aBlock._value_(err);\x0a})})"]]],
  201. messageSends: []
  202. }),
  203. $globals.TThenable);
  204. $core.addMethod(
  205. $core.method({
  206. selector: "on:do:",
  207. protocol: "promises",
  208. fn: function (aClass,aBlock){
  209. var self=this,$self=this;
  210. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  211. return $core.withContext(function($ctx1) {
  212. //>>excludeEnd("ctx");
  213. return self.then(null, function (err) {return $core.seamless(function () {
  214. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  215. else throw err;
  216. })});
  217. return self;
  218. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  219. }, function($ctx1) {$ctx1.fill(self,"on:do:",{aClass:aClass,aBlock:aBlock})});
  220. //>>excludeEnd("ctx");
  221. },
  222. //>>excludeStart("ide", pragmas.excludeIdeData);
  223. args: ["aClass", "aBlock"],
  224. 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})})'>",
  225. referencedClasses: [],
  226. //>>excludeEnd("ide");
  227. pragmas: [["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})})"]]],
  228. messageSends: []
  229. }),
  230. $globals.TThenable);
  231. $core.addMethod(
  232. $core.method({
  233. selector: "on:do:catch:",
  234. protocol: "promises",
  235. fn: function (aClass,aBlock,anotherBlock){
  236. var self=this,$self=this;
  237. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  238. return $core.withContext(function($ctx1) {
  239. //>>excludeEnd("ctx");
  240. return $recv($self._on_do_(aClass,aBlock))._catch_(anotherBlock);
  241. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  242. }, function($ctx1) {$ctx1.fill(self,"on:do:catch:",{aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock})});
  243. //>>excludeEnd("ctx");
  244. },
  245. //>>excludeStart("ide", pragmas.excludeIdeData);
  246. args: ["aClass", "aBlock", "anotherBlock"],
  247. source: "on: aClass do: aBlock catch: anotherBlock\x0a\x09^ (self on: aClass do: aBlock) catch: anotherBlock",
  248. referencedClasses: [],
  249. //>>excludeEnd("ide");
  250. pragmas: [],
  251. messageSends: ["catch:", "on:do:"]
  252. }),
  253. $globals.TThenable);
  254. $core.addMethod(
  255. $core.method({
  256. selector: "then:",
  257. protocol: "promises",
  258. fn: function (aBlockOrArray){
  259. var self=this,$self=this;
  260. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  261. return $core.withContext(function($ctx1) {
  262. //>>excludeEnd("ctx");
  263. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  264. return array.reduce(function (soFar, aBlock) {
  265. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  266. function (result) {return $core.seamless(function () {
  267. if (Array.isArray(result)) {
  268. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  269. } else {
  270. return aBlock._value_(result);
  271. }
  272. })} :
  273. function (result) {return $core.seamless(function () {
  274. return aBlock._value_(result);
  275. })}
  276. );
  277. }, self);
  278. return self;
  279. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  280. }, function($ctx1) {$ctx1.fill(self,"then:",{aBlockOrArray:aBlockOrArray})});
  281. //>>excludeEnd("ctx");
  282. },
  283. //>>excludeStart("ide", pragmas.excludeIdeData);
  284. args: ["aBlockOrArray"],
  285. 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)'>",
  286. referencedClasses: [],
  287. //>>excludeEnd("ide");
  288. pragmas: [["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)"]]],
  289. messageSends: []
  290. }),
  291. $globals.TThenable);
  292. $core.addMethod(
  293. $core.method({
  294. selector: "then:catch:",
  295. protocol: "promises",
  296. fn: function (aBlockOrArray,anotherBlock){
  297. var self=this,$self=this;
  298. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  299. return $core.withContext(function($ctx1) {
  300. //>>excludeEnd("ctx");
  301. return $recv($self._then_(aBlockOrArray))._catch_(anotherBlock);
  302. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  303. }, function($ctx1) {$ctx1.fill(self,"then:catch:",{aBlockOrArray:aBlockOrArray,anotherBlock:anotherBlock})});
  304. //>>excludeEnd("ctx");
  305. },
  306. //>>excludeStart("ide", pragmas.excludeIdeData);
  307. args: ["aBlockOrArray", "anotherBlock"],
  308. source: "then: aBlockOrArray catch: anotherBlock\x0a\x09^ (self then: aBlockOrArray) catch: anotherBlock",
  309. referencedClasses: [],
  310. //>>excludeEnd("ide");
  311. pragmas: [],
  312. messageSends: ["catch:", "then:"]
  313. }),
  314. $globals.TThenable);
  315. $core.addMethod(
  316. $core.method({
  317. selector: "then:on:do:",
  318. protocol: "promises",
  319. fn: function (aBlockOrArray,aClass,aBlock){
  320. var self=this,$self=this;
  321. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  322. return $core.withContext(function($ctx1) {
  323. //>>excludeEnd("ctx");
  324. return $recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock);
  325. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  326. }, function($ctx1) {$ctx1.fill(self,"then:on:do:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock})});
  327. //>>excludeEnd("ctx");
  328. },
  329. //>>excludeStart("ide", pragmas.excludeIdeData);
  330. args: ["aBlockOrArray", "aClass", "aBlock"],
  331. source: "then: aBlockOrArray on: aClass do: aBlock\x0a\x09^ (self then: aBlockOrArray) on: aClass do: aBlock",
  332. referencedClasses: [],
  333. //>>excludeEnd("ide");
  334. pragmas: [],
  335. messageSends: ["on:do:", "then:"]
  336. }),
  337. $globals.TThenable);
  338. $core.addMethod(
  339. $core.method({
  340. selector: "then:on:do:catch:",
  341. protocol: "promises",
  342. fn: function (aBlockOrArray,aClass,aBlock,anotherBlock){
  343. var self=this,$self=this;
  344. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  345. return $core.withContext(function($ctx1) {
  346. //>>excludeEnd("ctx");
  347. return $recv($recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock))._catch_(anotherBlock);
  348. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  349. }, function($ctx1) {$ctx1.fill(self,"then:on:do:catch:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock})});
  350. //>>excludeEnd("ctx");
  351. },
  352. //>>excludeStart("ide", pragmas.excludeIdeData);
  353. args: ["aBlockOrArray", "aClass", "aBlock", "anotherBlock"],
  354. source: "then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock\x0a\x09^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock",
  355. referencedClasses: [],
  356. //>>excludeEnd("ide");
  357. pragmas: [],
  358. messageSends: ["catch:", "on:do:", "then:"]
  359. }),
  360. $globals.TThenable);
  361. $core.setTraitComposition([{trait: $globals.TThenable}], $globals.Promise);
  362. });