Kernel-Promises.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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: '\x0a\x09var nonLocalReturn = null,\x0a\x09\x09promise = new Promise(function (resolve, reject) {\x0a\x09\x09 var model = $globals.PromiseExecution._resolveBlock_rejectBlock_(resolve, reject);\x0a\x09\x09 try { aBlock._value_(model); }\x0a\x09\x09\x09catch (ex) {\x0a\x09\x09\x09\x09if (Array.isArray(ex) && ex.length === 1) nonLocalReturn = ex;\x0a\x09\x09\x09\x09else reject(ex);\x0a\x09\x09\x09}\x0a\x09\x09});\x0a\x09if (nonLocalReturn) throw nonLocalReturn; else return promise;\x0a'>",
  142. referencedClasses: [],
  143. //>>excludeEnd("ide");
  144. pragmas: [["inlineJS:", ["\x0a\x09var nonLocalReturn = null,\x0a\x09\x09promise = new Promise(function (resolve, reject) {\x0a\x09\x09 var model = $globals.PromiseExecution._resolveBlock_rejectBlock_(resolve, reject);\x0a\x09\x09 try { aBlock._value_(model); }\x0a\x09\x09\x09catch (ex) {\x0a\x09\x09\x09\x09if (Array.isArray(ex) && ex.length === 1) nonLocalReturn = ex;\x0a\x09\x09\x09\x09else reject(ex);\x0a\x09\x09\x09}\x0a\x09\x09});\x0a\x09if (nonLocalReturn) throw nonLocalReturn; else return promise;"]]],
  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. var nonLocalReturn = null,
  152. promise = new Promise(function (resolve, reject) {
  153. var model = $globals.PromiseExecution._resolveBlock_rejectBlock_(resolve, reject);
  154. try { aBlock._value_(model); }
  155. catch (ex) {
  156. if (Array.isArray(ex) && ex.length === 1) nonLocalReturn = ex;
  157. else reject(ex);
  158. }
  159. });
  160. if (nonLocalReturn) throw nonLocalReturn; else return promise;;
  161. return self;
  162. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  163. }, function($ctx1) {$ctx1.fill(self,"new:",{aBlock:aBlock})});
  164. //>>excludeEnd("ctx");
  165. }; }),
  166. $globals.Promise.a$cls);
  167. $core.addMethod(
  168. $core.method({
  169. selector: "signal:",
  170. protocol: "instance creation",
  171. //>>excludeStart("ide", pragmas.excludeIdeData);
  172. args: ["anObject"],
  173. source: "signal: anObject\x0a\x22Returns a Promise rejected with anObject.\x22\x0a<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>",
  174. referencedClasses: [],
  175. //>>excludeEnd("ide");
  176. pragmas: [["inlineJS:", ["return $recv(anObject)._in_(function (x) {return Promise.reject(x)})"]]],
  177. messageSends: []
  178. }, function ($methodClass){ return function (anObject){
  179. var self=this,$self=this;
  180. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  181. return $core.withContext(function($ctx1) {
  182. //>>excludeEnd("ctx");
  183. return $recv(anObject)._in_(function (x) {return Promise.reject(x)});
  184. return self;
  185. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  186. }, function($ctx1) {$ctx1.fill(self,"signal:",{anObject:anObject})});
  187. //>>excludeEnd("ctx");
  188. }; }),
  189. $globals.Promise.a$cls);
  190. $core.addMethod(
  191. $core.method({
  192. selector: "value:",
  193. protocol: "instance creation",
  194. //>>excludeStart("ide", pragmas.excludeIdeData);
  195. args: ["anObject"],
  196. source: "value: anObject\x0a\x22Returns a Promise resolved with anObject.\x22\x0a<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>",
  197. referencedClasses: [],
  198. //>>excludeEnd("ide");
  199. pragmas: [["inlineJS:", ["return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})"]]],
  200. messageSends: []
  201. }, function ($methodClass){ return function (anObject){
  202. var self=this,$self=this;
  203. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  204. return $core.withContext(function($ctx1) {
  205. //>>excludeEnd("ctx");
  206. return $recv(anObject)._in_(function (x) {return Promise.resolve(x)});
  207. return self;
  208. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  209. }, function($ctx1) {$ctx1.fill(self,"value:",{anObject:anObject})});
  210. //>>excludeEnd("ctx");
  211. }; }),
  212. $globals.Promise.a$cls);
  213. $core.addClass("PromiseExecution", $globals.Object, "Kernel-Promises");
  214. $core.setSlots($globals.PromiseExecution, ["resolveBlock", "rejectBlock"]);
  215. $core.addMethod(
  216. $core.method({
  217. selector: "do:",
  218. protocol: "evaluating",
  219. //>>excludeStart("ide", pragmas.excludeIdeData);
  220. args: ["aBlock"],
  221. source: "do: aBlock\x0a\x09\x22Executes a block 'in the context of a promise' and resolves.\x0a\x09That is, if it ends with an error, promise is rejected.\x0a\x09If a block succeeds, promise is resolved with its return value.\x0a\x09Non-local returns are also treated as an error and reified as rejections.\x22\x0a\x09self value: (self try: aBlock)",
  222. referencedClasses: [],
  223. //>>excludeEnd("ide");
  224. pragmas: [],
  225. messageSends: ["value:", "try:"]
  226. }, function ($methodClass){ return function (aBlock){
  227. var self=this,$self=this;
  228. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  229. return $core.withContext(function($ctx1) {
  230. //>>excludeEnd("ctx");
  231. $self._value_($self._try_(aBlock));
  232. return self;
  233. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  234. }, function($ctx1) {$ctx1.fill(self,"do:",{aBlock:aBlock})});
  235. //>>excludeEnd("ctx");
  236. }; }),
  237. $globals.PromiseExecution);
  238. $core.addMethod(
  239. $core.method({
  240. selector: "resolveBlock:rejectBlock:",
  241. protocol: "accessing",
  242. //>>excludeStart("ide", pragmas.excludeIdeData);
  243. args: ["aBlock", "anotherBlock"],
  244. source: "resolveBlock: aBlock rejectBlock: anotherBlock\x0a\x09resolveBlock := aBlock.\x0a\x09rejectBlock := anotherBlock",
  245. referencedClasses: [],
  246. //>>excludeEnd("ide");
  247. pragmas: [],
  248. messageSends: []
  249. }, function ($methodClass){ return function (aBlock,anotherBlock){
  250. var self=this,$self=this;
  251. $self.resolveBlock=aBlock;
  252. $self.rejectBlock=anotherBlock;
  253. return self;
  254. }; }),
  255. $globals.PromiseExecution);
  256. $core.addMethod(
  257. $core.method({
  258. selector: "signal:",
  259. protocol: "settling",
  260. //>>excludeStart("ide", pragmas.excludeIdeData);
  261. args: ["anErrorObject"],
  262. source: "signal: anErrorObject\x0a\x09rejectBlock value: anErrorObject",
  263. referencedClasses: [],
  264. //>>excludeEnd("ide");
  265. pragmas: [],
  266. messageSends: ["value:"]
  267. }, function ($methodClass){ return function (anErrorObject){
  268. var self=this,$self=this;
  269. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  270. return $core.withContext(function($ctx1) {
  271. //>>excludeEnd("ctx");
  272. $recv($self.rejectBlock)._value_(anErrorObject);
  273. return self;
  274. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  275. }, function($ctx1) {$ctx1.fill(self,"signal:",{anErrorObject:anErrorObject})});
  276. //>>excludeEnd("ctx");
  277. }; }),
  278. $globals.PromiseExecution);
  279. $core.addMethod(
  280. $core.method({
  281. selector: "try:",
  282. protocol: "evaluating",
  283. //>>excludeStart("ide", pragmas.excludeIdeData);
  284. args: ["aBlock"],
  285. source: "try: aBlock\x0a\x09\x22Executes a block 'in the context of a promise'.\x0a\x09That is, if it ends with an error, promise is rejected.\x0a\x09Non-local returns are also treated as an error and reified as rejections.\x22\x0a\x09<inlineJS: '\x0a\x09\x09try {\x0a\x09\x09\x09return aBlock._value();\x0a\x09\x09} catch(error) {\x0a\x09\x09\x09$self._signal_($globals.NonLifoReturn._reifyIfFeasible_(error));\x0a\x09\x09}\x0a\x09'>",
  286. referencedClasses: [],
  287. //>>excludeEnd("ide");
  288. pragmas: [["inlineJS:", ["\x0a\x09\x09try {\x0a\x09\x09\x09return aBlock._value();\x0a\x09\x09} catch(error) {\x0a\x09\x09\x09$self._signal_($globals.NonLifoReturn._reifyIfFeasible_(error));\x0a\x09\x09}\x0a\x09"]]],
  289. messageSends: []
  290. }, function ($methodClass){ return function (aBlock){
  291. var self=this,$self=this;
  292. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  293. return $core.withContext(function($ctx1) {
  294. //>>excludeEnd("ctx");
  295. try {
  296. return aBlock._value();
  297. } catch(error) {
  298. $self._signal_($globals.NonLifoReturn._reifyIfFeasible_(error));
  299. }
  300. ;
  301. return self;
  302. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  303. }, function($ctx1) {$ctx1.fill(self,"try:",{aBlock:aBlock})});
  304. //>>excludeEnd("ctx");
  305. }; }),
  306. $globals.PromiseExecution);
  307. $core.addMethod(
  308. $core.method({
  309. selector: "value:",
  310. protocol: "settling",
  311. //>>excludeStart("ide", pragmas.excludeIdeData);
  312. args: ["anObject"],
  313. source: "value: anObject\x0a\x09resolveBlock value: anObject",
  314. referencedClasses: [],
  315. //>>excludeEnd("ide");
  316. pragmas: [],
  317. messageSends: ["value:"]
  318. }, function ($methodClass){ return function (anObject){
  319. var self=this,$self=this;
  320. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  321. return $core.withContext(function($ctx1) {
  322. //>>excludeEnd("ctx");
  323. $recv($self.resolveBlock)._value_(anObject);
  324. return self;
  325. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  326. }, function($ctx1) {$ctx1.fill(self,"value:",{anObject:anObject})});
  327. //>>excludeEnd("ctx");
  328. }; }),
  329. $globals.PromiseExecution);
  330. $core.addMethod(
  331. $core.method({
  332. selector: "resolveBlock:rejectBlock:",
  333. protocol: "instance creation",
  334. //>>excludeStart("ide", pragmas.excludeIdeData);
  335. args: ["aBlock", "anotherBlock"],
  336. source: "resolveBlock: aBlock rejectBlock: anotherBlock\x0a\x09^ super new\x0a\x09\x09resolveBlock: aBlock rejectBlock: anotherBlock;\x0a\x09\x09yourself",
  337. referencedClasses: [],
  338. //>>excludeEnd("ide");
  339. pragmas: [],
  340. messageSends: ["resolveBlock:rejectBlock:", "new", "yourself"]
  341. }, function ($methodClass){ return function (aBlock,anotherBlock){
  342. var self=this,$self=this;
  343. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  344. return $core.withContext(function($ctx1) {
  345. //>>excludeEnd("ctx");
  346. var $1;
  347. $1=[(
  348. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  349. $ctx1.supercall = true,
  350. //>>excludeEnd("ctx");
  351. ($methodClass.superclass||$boot.nilAsClass).fn.prototype._new.call($self))
  352. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  353. ,$ctx1.supercall = false
  354. //>>excludeEnd("ctx");
  355. ][0];
  356. $recv($1)._resolveBlock_rejectBlock_(aBlock,anotherBlock);
  357. return $recv($1)._yourself();
  358. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  359. }, function($ctx1) {$ctx1.fill(self,"resolveBlock:rejectBlock:",{aBlock:aBlock,anotherBlock:anotherBlock})});
  360. //>>excludeEnd("ctx");
  361. }; }),
  362. $globals.PromiseExecution.a$cls);
  363. $core.addTrait("TPromiseModel", "Kernel-Promises");
  364. $core.addMethod(
  365. $core.method({
  366. selector: "signal",
  367. protocol: "settling",
  368. //>>excludeStart("ide", pragmas.excludeIdeData);
  369. args: [],
  370. source: "signal\x0a\x09^ self signal: Error new",
  371. referencedClasses: ["Error"],
  372. //>>excludeEnd("ide");
  373. pragmas: [],
  374. messageSends: ["signal:", "new"]
  375. }, function ($methodClass){ return function (){
  376. var self=this,$self=this;
  377. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  378. return $core.withContext(function($ctx1) {
  379. //>>excludeEnd("ctx");
  380. return $self._signal_($recv($globals.Error)._new());
  381. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  382. }, function($ctx1) {$ctx1.fill(self,"signal",{})});
  383. //>>excludeEnd("ctx");
  384. }; }),
  385. $globals.TPromiseModel);
  386. $core.addMethod(
  387. $core.method({
  388. selector: "signal:",
  389. protocol: "settling",
  390. //>>excludeStart("ide", pragmas.excludeIdeData);
  391. args: ["anErrorObject"],
  392. source: "signal: anErrorObject\x0a\x09self subclassResponsibility",
  393. referencedClasses: [],
  394. //>>excludeEnd("ide");
  395. pragmas: [],
  396. messageSends: ["subclassResponsibility"]
  397. }, function ($methodClass){ return function (anErrorObject){
  398. var self=this,$self=this;
  399. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  400. return $core.withContext(function($ctx1) {
  401. //>>excludeEnd("ctx");
  402. $self._subclassResponsibility();
  403. return self;
  404. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  405. }, function($ctx1) {$ctx1.fill(self,"signal:",{anErrorObject:anErrorObject})});
  406. //>>excludeEnd("ctx");
  407. }; }),
  408. $globals.TPromiseModel);
  409. $core.addMethod(
  410. $core.method({
  411. selector: "value",
  412. protocol: "settling",
  413. //>>excludeStart("ide", pragmas.excludeIdeData);
  414. args: [],
  415. source: "value\x0a\x09^ self value: nil",
  416. referencedClasses: [],
  417. //>>excludeEnd("ide");
  418. pragmas: [],
  419. messageSends: ["value:"]
  420. }, function ($methodClass){ return function (){
  421. var self=this,$self=this;
  422. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  423. return $core.withContext(function($ctx1) {
  424. //>>excludeEnd("ctx");
  425. return $self._value_(nil);
  426. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  427. }, function($ctx1) {$ctx1.fill(self,"value",{})});
  428. //>>excludeEnd("ctx");
  429. }; }),
  430. $globals.TPromiseModel);
  431. $core.addMethod(
  432. $core.method({
  433. selector: "value:",
  434. protocol: "settling",
  435. //>>excludeStart("ide", pragmas.excludeIdeData);
  436. args: ["anObject"],
  437. source: "value: anObject\x0a\x09self subclassResponsibility",
  438. referencedClasses: [],
  439. //>>excludeEnd("ide");
  440. pragmas: [],
  441. messageSends: ["subclassResponsibility"]
  442. }, function ($methodClass){ return function (anObject){
  443. var self=this,$self=this;
  444. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  445. return $core.withContext(function($ctx1) {
  446. //>>excludeEnd("ctx");
  447. $self._subclassResponsibility();
  448. return self;
  449. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  450. }, function($ctx1) {$ctx1.fill(self,"value:",{anObject:anObject})});
  451. //>>excludeEnd("ctx");
  452. }; }),
  453. $globals.TPromiseModel);
  454. $core.addTrait("TThenable", "Kernel-Promises");
  455. $core.addMethod(
  456. $core.method({
  457. selector: "catch:",
  458. protocol: "promises",
  459. //>>excludeStart("ide", pragmas.excludeIdeData);
  460. args: ["aBlock"],
  461. source: "catch: aBlock\x0a<inlineJS: 'return self.then(null, function (err) {\x0a\x09return aBlock._value_($globals.NonLifoReturn._reifyIfFeasible_(err));\x0a})'>",
  462. referencedClasses: [],
  463. //>>excludeEnd("ide");
  464. pragmas: [["inlineJS:", ["return self.then(null, function (err) {\x0a\x09return aBlock._value_($globals.NonLifoReturn._reifyIfFeasible_(err));\x0a})"]]],
  465. messageSends: []
  466. }, function ($methodClass){ return function (aBlock){
  467. var self=this,$self=this;
  468. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  469. return $core.withContext(function($ctx1) {
  470. //>>excludeEnd("ctx");
  471. return self.then(null, function (err) {
  472. return aBlock._value_($globals.NonLifoReturn._reifyIfFeasible_(err));
  473. });
  474. return self;
  475. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  476. }, function($ctx1) {$ctx1.fill(self,"catch:",{aBlock:aBlock})});
  477. //>>excludeEnd("ctx");
  478. }; }),
  479. $globals.TThenable);
  480. $core.addMethod(
  481. $core.method({
  482. selector: "isThenable",
  483. protocol: "testing",
  484. //>>excludeStart("ide", pragmas.excludeIdeData);
  485. args: [],
  486. source: "isThenable\x0a\x09^ true",
  487. referencedClasses: [],
  488. //>>excludeEnd("ide");
  489. pragmas: [],
  490. messageSends: []
  491. }, function ($methodClass){ return function (){
  492. var self=this,$self=this;
  493. return true;
  494. }; }),
  495. $globals.TThenable);
  496. $core.addMethod(
  497. $core.method({
  498. selector: "on:do:",
  499. protocol: "promises",
  500. //>>excludeStart("ide", pragmas.excludeIdeData);
  501. args: ["aClass", "aBlock"],
  502. source: "on: aClass do: aBlock\x0a<inlineJS: 'return self.then(null, function (err) {\x0a var reified = $globals.NonLifoReturn._reifyIfFeasible_(err);\x0a if (reified._isKindOf_(aClass)) return aBlock._value_(reified);\x0a else throw err;\x0a})'>",
  503. referencedClasses: [],
  504. //>>excludeEnd("ide");
  505. pragmas: [["inlineJS:", ["return self.then(null, function (err) {\x0a var reified = $globals.NonLifoReturn._reifyIfFeasible_(err);\x0a if (reified._isKindOf_(aClass)) return aBlock._value_(reified);\x0a else throw err;\x0a})"]]],
  506. messageSends: []
  507. }, function ($methodClass){ return function (aClass,aBlock){
  508. var self=this,$self=this;
  509. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  510. return $core.withContext(function($ctx1) {
  511. //>>excludeEnd("ctx");
  512. return self.then(null, function (err) {
  513. var reified = $globals.NonLifoReturn._reifyIfFeasible_(err);
  514. if (reified._isKindOf_(aClass)) return aBlock._value_(reified);
  515. else throw err;
  516. });
  517. return self;
  518. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  519. }, function($ctx1) {$ctx1.fill(self,"on:do:",{aClass:aClass,aBlock:aBlock})});
  520. //>>excludeEnd("ctx");
  521. }; }),
  522. $globals.TThenable);
  523. $core.addMethod(
  524. $core.method({
  525. selector: "on:do:catch:",
  526. protocol: "promises",
  527. //>>excludeStart("ide", pragmas.excludeIdeData);
  528. args: ["aClass", "aBlock", "anotherBlock"],
  529. source: "on: aClass do: aBlock catch: anotherBlock\x0a\x09^ (self on: aClass do: aBlock) catch: anotherBlock",
  530. referencedClasses: [],
  531. //>>excludeEnd("ide");
  532. pragmas: [],
  533. messageSends: ["catch:", "on:do:"]
  534. }, function ($methodClass){ return function (aClass,aBlock,anotherBlock){
  535. var self=this,$self=this;
  536. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  537. return $core.withContext(function($ctx1) {
  538. //>>excludeEnd("ctx");
  539. return $recv($self._on_do_(aClass,aBlock))._catch_(anotherBlock);
  540. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  541. }, function($ctx1) {$ctx1.fill(self,"on:do:catch:",{aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock})});
  542. //>>excludeEnd("ctx");
  543. }; }),
  544. $globals.TThenable);
  545. $core.addMethod(
  546. $core.method({
  547. selector: "then:",
  548. protocol: "promises",
  549. //>>excludeStart("ide", pragmas.excludeIdeData);
  550. args: ["aBlockOrArray"],
  551. 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)'>",
  552. referencedClasses: [],
  553. //>>excludeEnd("ide");
  554. 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)"]]],
  555. messageSends: []
  556. }, function ($methodClass){ return function (aBlockOrArray){
  557. var self=this,$self=this;
  558. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  559. return $core.withContext(function($ctx1) {
  560. //>>excludeEnd("ctx");
  561. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  562. return array.reduce(function (soFar, aBlock) {
  563. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  564. function (result) {
  565. if (Array.isArray(result)) {
  566. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  567. } else {
  568. return aBlock._value_(result);
  569. }
  570. } :
  571. function (result) {
  572. return aBlock._value_(result);
  573. }
  574. );
  575. }, self);
  576. return self;
  577. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  578. }, function($ctx1) {$ctx1.fill(self,"then:",{aBlockOrArray:aBlockOrArray})});
  579. //>>excludeEnd("ctx");
  580. }; }),
  581. $globals.TThenable);
  582. $core.addMethod(
  583. $core.method({
  584. selector: "then:catch:",
  585. protocol: "promises",
  586. //>>excludeStart("ide", pragmas.excludeIdeData);
  587. args: ["aBlockOrArray", "anotherBlock"],
  588. source: "then: aBlockOrArray catch: anotherBlock\x0a\x09^ (self then: aBlockOrArray) catch: anotherBlock",
  589. referencedClasses: [],
  590. //>>excludeEnd("ide");
  591. pragmas: [],
  592. messageSends: ["catch:", "then:"]
  593. }, function ($methodClass){ return function (aBlockOrArray,anotherBlock){
  594. var self=this,$self=this;
  595. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  596. return $core.withContext(function($ctx1) {
  597. //>>excludeEnd("ctx");
  598. return $recv($self._then_(aBlockOrArray))._catch_(anotherBlock);
  599. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  600. }, function($ctx1) {$ctx1.fill(self,"then:catch:",{aBlockOrArray:aBlockOrArray,anotherBlock:anotherBlock})});
  601. //>>excludeEnd("ctx");
  602. }; }),
  603. $globals.TThenable);
  604. $core.addMethod(
  605. $core.method({
  606. selector: "then:on:do:",
  607. protocol: "promises",
  608. //>>excludeStart("ide", pragmas.excludeIdeData);
  609. args: ["aBlockOrArray", "aClass", "aBlock"],
  610. source: "then: aBlockOrArray on: aClass do: aBlock\x0a\x09^ (self then: aBlockOrArray) on: aClass do: aBlock",
  611. referencedClasses: [],
  612. //>>excludeEnd("ide");
  613. pragmas: [],
  614. messageSends: ["on:do:", "then:"]
  615. }, function ($methodClass){ return function (aBlockOrArray,aClass,aBlock){
  616. var self=this,$self=this;
  617. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  618. return $core.withContext(function($ctx1) {
  619. //>>excludeEnd("ctx");
  620. return $recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock);
  621. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  622. }, function($ctx1) {$ctx1.fill(self,"then:on:do:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock})});
  623. //>>excludeEnd("ctx");
  624. }; }),
  625. $globals.TThenable);
  626. $core.addMethod(
  627. $core.method({
  628. selector: "then:on:do:catch:",
  629. protocol: "promises",
  630. //>>excludeStart("ide", pragmas.excludeIdeData);
  631. args: ["aBlockOrArray", "aClass", "aBlock", "anotherBlock"],
  632. source: "then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock\x0a\x09^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock",
  633. referencedClasses: [],
  634. //>>excludeEnd("ide");
  635. pragmas: [],
  636. messageSends: ["catch:", "on:do:", "then:"]
  637. }, function ($methodClass){ return function (aBlockOrArray,aClass,aBlock,anotherBlock){
  638. var self=this,$self=this;
  639. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  640. return $core.withContext(function($ctx1) {
  641. //>>excludeEnd("ctx");
  642. return $recv($recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock))._catch_(anotherBlock);
  643. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  644. }, function($ctx1) {$ctx1.fill(self,"then:on:do:catch:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock})});
  645. //>>excludeEnd("ctx");
  646. }; }),
  647. $globals.TThenable);
  648. $core.setTraitComposition([{trait: $globals.TThenable}], $globals.Promise);
  649. $core.setTraitComposition([{trait: $globals.TPromiseModel}], $globals.Promise.a$cls);
  650. $core.setTraitComposition([{trait: $globals.TPromiseModel}], $globals.PromiseExecution);
  651. });