Kernel-Promises.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 aBlock._value_(err); })'>",
  178. referencedClasses: [],
  179. //>>excludeEnd("ide");
  180. pragmas: [["inlineJS:", ["return self.then(null, function (err) { return aBlock._value_(err); })"]]],
  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 aBlock._value_(err); });
  188. return self;
  189. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  190. }, function($ctx1) {$ctx1.fill(self,"catch:",{aBlock:aBlock})});
  191. //>>excludeEnd("ctx");
  192. }; }),
  193. $globals.TThenable);
  194. $core.addMethod(
  195. $core.method({
  196. selector: "on:do:",
  197. protocol: "promises",
  198. //>>excludeStart("ide", pragmas.excludeIdeData);
  199. args: ["aClass", "aBlock"],
  200. source: "on: aClass do: aBlock\x0a<inlineJS: 'return self.then(null, function (err) {\x0a if (err._isKindOf_(aClass)) return aBlock._value_(err);\x0a else throw err;\x0a})'>",
  201. referencedClasses: [],
  202. //>>excludeEnd("ide");
  203. pragmas: [["inlineJS:", ["return self.then(null, function (err) {\x0a if (err._isKindOf_(aClass)) return aBlock._value_(err);\x0a else throw err;\x0a})"]]],
  204. messageSends: []
  205. }, function ($methodClass){ return function (aClass,aBlock){
  206. var self=this,$self=this;
  207. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  208. return $core.withContext(function($ctx1) {
  209. //>>excludeEnd("ctx");
  210. return self.then(null, function (err) {
  211. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  212. else throw err;
  213. });
  214. return self;
  215. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  216. }, function($ctx1) {$ctx1.fill(self,"on:do:",{aClass:aClass,aBlock:aBlock})});
  217. //>>excludeEnd("ctx");
  218. }; }),
  219. $globals.TThenable);
  220. $core.addMethod(
  221. $core.method({
  222. selector: "on:do:catch:",
  223. protocol: "promises",
  224. //>>excludeStart("ide", pragmas.excludeIdeData);
  225. args: ["aClass", "aBlock", "anotherBlock"],
  226. source: "on: aClass do: aBlock catch: anotherBlock\x0a\x09^ (self on: aClass do: aBlock) catch: anotherBlock",
  227. referencedClasses: [],
  228. //>>excludeEnd("ide");
  229. pragmas: [],
  230. messageSends: ["catch:", "on:do:"]
  231. }, function ($methodClass){ return function (aClass,aBlock,anotherBlock){
  232. var self=this,$self=this;
  233. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  234. return $core.withContext(function($ctx1) {
  235. //>>excludeEnd("ctx");
  236. return $recv($self._on_do_(aClass,aBlock))._catch_(anotherBlock);
  237. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  238. }, function($ctx1) {$ctx1.fill(self,"on:do:catch:",{aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock})});
  239. //>>excludeEnd("ctx");
  240. }; }),
  241. $globals.TThenable);
  242. $core.addMethod(
  243. $core.method({
  244. selector: "then:",
  245. protocol: "promises",
  246. //>>excludeStart("ide", pragmas.excludeIdeData);
  247. args: ["aBlockOrArray"],
  248. 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) {\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) {\x0a return aBlock._value_(result);\x0a }\x0a );\x0a}, self)'>",
  249. referencedClasses: [],
  250. //>>excludeEnd("ide");
  251. 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) {\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) {\x0a return aBlock._value_(result);\x0a }\x0a );\x0a}, self)"]]],
  252. messageSends: []
  253. }, function ($methodClass){ return function (aBlockOrArray){
  254. var self=this,$self=this;
  255. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  256. return $core.withContext(function($ctx1) {
  257. //>>excludeEnd("ctx");
  258. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  259. return array.reduce(function (soFar, aBlock) {
  260. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  261. function (result) {
  262. if (Array.isArray(result)) {
  263. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  264. } else {
  265. return aBlock._value_(result);
  266. }
  267. } :
  268. function (result) {
  269. return aBlock._value_(result);
  270. }
  271. );
  272. }, self);
  273. return self;
  274. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  275. }, function($ctx1) {$ctx1.fill(self,"then:",{aBlockOrArray:aBlockOrArray})});
  276. //>>excludeEnd("ctx");
  277. }; }),
  278. $globals.TThenable);
  279. $core.addMethod(
  280. $core.method({
  281. selector: "then:catch:",
  282. protocol: "promises",
  283. //>>excludeStart("ide", pragmas.excludeIdeData);
  284. args: ["aBlockOrArray", "anotherBlock"],
  285. source: "then: aBlockOrArray catch: anotherBlock\x0a\x09^ (self then: aBlockOrArray) catch: anotherBlock",
  286. referencedClasses: [],
  287. //>>excludeEnd("ide");
  288. pragmas: [],
  289. messageSends: ["catch:", "then:"]
  290. }, function ($methodClass){ return function (aBlockOrArray,anotherBlock){
  291. var self=this,$self=this;
  292. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  293. return $core.withContext(function($ctx1) {
  294. //>>excludeEnd("ctx");
  295. return $recv($self._then_(aBlockOrArray))._catch_(anotherBlock);
  296. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  297. }, function($ctx1) {$ctx1.fill(self,"then:catch:",{aBlockOrArray:aBlockOrArray,anotherBlock:anotherBlock})});
  298. //>>excludeEnd("ctx");
  299. }; }),
  300. $globals.TThenable);
  301. $core.addMethod(
  302. $core.method({
  303. selector: "then:on:do:",
  304. protocol: "promises",
  305. //>>excludeStart("ide", pragmas.excludeIdeData);
  306. args: ["aBlockOrArray", "aClass", "aBlock"],
  307. source: "then: aBlockOrArray on: aClass do: aBlock\x0a\x09^ (self then: aBlockOrArray) on: aClass do: aBlock",
  308. referencedClasses: [],
  309. //>>excludeEnd("ide");
  310. pragmas: [],
  311. messageSends: ["on:do:", "then:"]
  312. }, function ($methodClass){ return function (aBlockOrArray,aClass,aBlock){
  313. var self=this,$self=this;
  314. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  315. return $core.withContext(function($ctx1) {
  316. //>>excludeEnd("ctx");
  317. return $recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock);
  318. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  319. }, function($ctx1) {$ctx1.fill(self,"then:on:do:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock})});
  320. //>>excludeEnd("ctx");
  321. }; }),
  322. $globals.TThenable);
  323. $core.addMethod(
  324. $core.method({
  325. selector: "then:on:do:catch:",
  326. protocol: "promises",
  327. //>>excludeStart("ide", pragmas.excludeIdeData);
  328. args: ["aBlockOrArray", "aClass", "aBlock", "anotherBlock"],
  329. source: "then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock\x0a\x09^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock",
  330. referencedClasses: [],
  331. //>>excludeEnd("ide");
  332. pragmas: [],
  333. messageSends: ["catch:", "on:do:", "then:"]
  334. }, function ($methodClass){ return function (aBlockOrArray,aClass,aBlock,anotherBlock){
  335. var self=this,$self=this;
  336. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  337. return $core.withContext(function($ctx1) {
  338. //>>excludeEnd("ctx");
  339. return $recv($recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock))._catch_(anotherBlock);
  340. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  341. }, function($ctx1) {$ctx1.fill(self,"then:on:do:catch:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock})});
  342. //>>excludeEnd("ctx");
  343. }; }),
  344. $globals.TThenable);
  345. $core.setTraitComposition([{trait: $globals.TThenable}], $globals.Promise);
  346. });