Kernel-Promises.js 25 KB

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