Kernel-Promises.js 17 KB

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