Kernel-Promises.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 = {value: resolve, signal: reject};\x0a aBlock._value_(model);\x0a})'>",
  142. referencedClasses: [],
  143. //>>excludeEnd("ide");
  144. pragmas: [["inlineJS:", ["return new Promise(function (resolve, reject) {\x0a var model = {value: resolve, signal: reject};\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 = {value: resolve, signal: reject};
  153. aBlock._value_(model);
  154. });
  155. return self;
  156. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  157. }, function($ctx1) {$ctx1.fill(self,"new:",{aBlock:aBlock})});
  158. //>>excludeEnd("ctx");
  159. }; }),
  160. $globals.Promise.a$cls);
  161. $core.addMethod(
  162. $core.method({
  163. selector: "signal:",
  164. protocol: "instance creation",
  165. //>>excludeStart("ide", pragmas.excludeIdeData);
  166. args: ["anObject"],
  167. source: "signal: anObject\x0a\x22Returns a Promise rejected with anObject.\x22\x0a<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>",
  168. referencedClasses: [],
  169. //>>excludeEnd("ide");
  170. pragmas: [["inlineJS:", ["return $recv(anObject)._in_(function (x) {return Promise.reject(x)})"]]],
  171. messageSends: []
  172. }, function ($methodClass){ return function (anObject){
  173. var self=this,$self=this;
  174. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  175. return $core.withContext(function($ctx1) {
  176. //>>excludeEnd("ctx");
  177. return $recv(anObject)._in_(function (x) {return Promise.reject(x)});
  178. return self;
  179. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  180. }, function($ctx1) {$ctx1.fill(self,"signal:",{anObject:anObject})});
  181. //>>excludeEnd("ctx");
  182. }; }),
  183. $globals.Promise.a$cls);
  184. $core.addMethod(
  185. $core.method({
  186. selector: "value:",
  187. protocol: "instance creation",
  188. //>>excludeStart("ide", pragmas.excludeIdeData);
  189. args: ["anObject"],
  190. source: "value: anObject\x0a\x22Returns a Promise resolved with anObject.\x22\x0a<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>",
  191. referencedClasses: [],
  192. //>>excludeEnd("ide");
  193. pragmas: [["inlineJS:", ["return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})"]]],
  194. messageSends: []
  195. }, function ($methodClass){ return function (anObject){
  196. var self=this,$self=this;
  197. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  198. return $core.withContext(function($ctx1) {
  199. //>>excludeEnd("ctx");
  200. return $recv(anObject)._in_(function (x) {return Promise.resolve(x)});
  201. return self;
  202. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  203. }, function($ctx1) {$ctx1.fill(self,"value:",{anObject:anObject})});
  204. //>>excludeEnd("ctx");
  205. }; }),
  206. $globals.Promise.a$cls);
  207. $core.addTrait("TThenable", "Kernel-Promises");
  208. $core.addMethod(
  209. $core.method({
  210. selector: "catch:",
  211. protocol: "promises",
  212. //>>excludeStart("ide", pragmas.excludeIdeData);
  213. args: ["aBlock"],
  214. source: "catch: aBlock\x0a<inlineJS: 'return self.then(null, function (err) { return aBlock._value_(err); })'>",
  215. referencedClasses: [],
  216. //>>excludeEnd("ide");
  217. pragmas: [["inlineJS:", ["return self.then(null, function (err) { return aBlock._value_(err); })"]]],
  218. messageSends: []
  219. }, function ($methodClass){ return function (aBlock){
  220. var self=this,$self=this;
  221. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  222. return $core.withContext(function($ctx1) {
  223. //>>excludeEnd("ctx");
  224. return self.then(null, function (err) { return aBlock._value_(err); });
  225. return self;
  226. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  227. }, function($ctx1) {$ctx1.fill(self,"catch:",{aBlock:aBlock})});
  228. //>>excludeEnd("ctx");
  229. }; }),
  230. $globals.TThenable);
  231. $core.addMethod(
  232. $core.method({
  233. selector: "isThenable",
  234. protocol: "testing",
  235. //>>excludeStart("ide", pragmas.excludeIdeData);
  236. args: [],
  237. source: "isThenable\x0a\x09^ true",
  238. referencedClasses: [],
  239. //>>excludeEnd("ide");
  240. pragmas: [],
  241. messageSends: []
  242. }, function ($methodClass){ return function (){
  243. var self=this,$self=this;
  244. return true;
  245. }; }),
  246. $globals.TThenable);
  247. $core.addMethod(
  248. $core.method({
  249. selector: "on:do:",
  250. protocol: "promises",
  251. //>>excludeStart("ide", pragmas.excludeIdeData);
  252. args: ["aClass", "aBlock"],
  253. 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})'>",
  254. referencedClasses: [],
  255. //>>excludeEnd("ide");
  256. pragmas: [["inlineJS:", ["return self.then(null, function (err) {\x0a if (err._isKindOf_(aClass)) return aBlock._value_(err);\x0a else throw err;\x0a})"]]],
  257. messageSends: []
  258. }, function ($methodClass){ return function (aClass,aBlock){
  259. var self=this,$self=this;
  260. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  261. return $core.withContext(function($ctx1) {
  262. //>>excludeEnd("ctx");
  263. return self.then(null, function (err) {
  264. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  265. else throw err;
  266. });
  267. return self;
  268. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  269. }, function($ctx1) {$ctx1.fill(self,"on:do:",{aClass:aClass,aBlock:aBlock})});
  270. //>>excludeEnd("ctx");
  271. }; }),
  272. $globals.TThenable);
  273. $core.addMethod(
  274. $core.method({
  275. selector: "on:do:catch:",
  276. protocol: "promises",
  277. //>>excludeStart("ide", pragmas.excludeIdeData);
  278. args: ["aClass", "aBlock", "anotherBlock"],
  279. source: "on: aClass do: aBlock catch: anotherBlock\x0a\x09^ (self on: aClass do: aBlock) catch: anotherBlock",
  280. referencedClasses: [],
  281. //>>excludeEnd("ide");
  282. pragmas: [],
  283. messageSends: ["catch:", "on:do:"]
  284. }, function ($methodClass){ return function (aClass,aBlock,anotherBlock){
  285. var self=this,$self=this;
  286. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  287. return $core.withContext(function($ctx1) {
  288. //>>excludeEnd("ctx");
  289. return $recv($self._on_do_(aClass,aBlock))._catch_(anotherBlock);
  290. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  291. }, function($ctx1) {$ctx1.fill(self,"on:do:catch:",{aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock})});
  292. //>>excludeEnd("ctx");
  293. }; }),
  294. $globals.TThenable);
  295. $core.addMethod(
  296. $core.method({
  297. selector: "then:",
  298. protocol: "promises",
  299. //>>excludeStart("ide", pragmas.excludeIdeData);
  300. args: ["aBlockOrArray"],
  301. 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)'>",
  302. referencedClasses: [],
  303. //>>excludeEnd("ide");
  304. 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)"]]],
  305. messageSends: []
  306. }, function ($methodClass){ return function (aBlockOrArray){
  307. var self=this,$self=this;
  308. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  309. return $core.withContext(function($ctx1) {
  310. //>>excludeEnd("ctx");
  311. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  312. return array.reduce(function (soFar, aBlock) {
  313. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  314. function (result) {
  315. if (Array.isArray(result)) {
  316. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  317. } else {
  318. return aBlock._value_(result);
  319. }
  320. } :
  321. function (result) {
  322. return aBlock._value_(result);
  323. }
  324. );
  325. }, self);
  326. return self;
  327. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  328. }, function($ctx1) {$ctx1.fill(self,"then:",{aBlockOrArray:aBlockOrArray})});
  329. //>>excludeEnd("ctx");
  330. }; }),
  331. $globals.TThenable);
  332. $core.addMethod(
  333. $core.method({
  334. selector: "then:catch:",
  335. protocol: "promises",
  336. //>>excludeStart("ide", pragmas.excludeIdeData);
  337. args: ["aBlockOrArray", "anotherBlock"],
  338. source: "then: aBlockOrArray catch: anotherBlock\x0a\x09^ (self then: aBlockOrArray) catch: anotherBlock",
  339. referencedClasses: [],
  340. //>>excludeEnd("ide");
  341. pragmas: [],
  342. messageSends: ["catch:", "then:"]
  343. }, function ($methodClass){ return function (aBlockOrArray,anotherBlock){
  344. var self=this,$self=this;
  345. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  346. return $core.withContext(function($ctx1) {
  347. //>>excludeEnd("ctx");
  348. return $recv($self._then_(aBlockOrArray))._catch_(anotherBlock);
  349. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  350. }, function($ctx1) {$ctx1.fill(self,"then:catch:",{aBlockOrArray:aBlockOrArray,anotherBlock:anotherBlock})});
  351. //>>excludeEnd("ctx");
  352. }; }),
  353. $globals.TThenable);
  354. $core.addMethod(
  355. $core.method({
  356. selector: "then:on:do:",
  357. protocol: "promises",
  358. //>>excludeStart("ide", pragmas.excludeIdeData);
  359. args: ["aBlockOrArray", "aClass", "aBlock"],
  360. source: "then: aBlockOrArray on: aClass do: aBlock\x0a\x09^ (self then: aBlockOrArray) on: aClass do: aBlock",
  361. referencedClasses: [],
  362. //>>excludeEnd("ide");
  363. pragmas: [],
  364. messageSends: ["on:do:", "then:"]
  365. }, function ($methodClass){ return function (aBlockOrArray,aClass,aBlock){
  366. var self=this,$self=this;
  367. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  368. return $core.withContext(function($ctx1) {
  369. //>>excludeEnd("ctx");
  370. return $recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock);
  371. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  372. }, function($ctx1) {$ctx1.fill(self,"then:on:do:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock})});
  373. //>>excludeEnd("ctx");
  374. }; }),
  375. $globals.TThenable);
  376. $core.addMethod(
  377. $core.method({
  378. selector: "then:on:do:catch:",
  379. protocol: "promises",
  380. //>>excludeStart("ide", pragmas.excludeIdeData);
  381. args: ["aBlockOrArray", "aClass", "aBlock", "anotherBlock"],
  382. source: "then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock\x0a\x09^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock",
  383. referencedClasses: [],
  384. //>>excludeEnd("ide");
  385. pragmas: [],
  386. messageSends: ["catch:", "on:do:", "then:"]
  387. }, function ($methodClass){ return function (aBlockOrArray,aClass,aBlock,anotherBlock){
  388. var self=this,$self=this;
  389. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  390. return $core.withContext(function($ctx1) {
  391. //>>excludeEnd("ctx");
  392. return $recv($recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock))._catch_(anotherBlock);
  393. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  394. }, function($ctx1) {$ctx1.fill(self,"then:on:do:catch:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock})});
  395. //>>excludeEnd("ctx");
  396. }; }),
  397. $globals.TThenable);
  398. $core.setTraitComposition([{trait: $globals.TThenable}], $globals.Promise);
  399. });