Kernel-Promises.js 15 KB

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