Kernel-Promises.js 16 KB

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