Kernel-Promises.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 localReturn = 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) localReturn = ex;\x0a\x09\x09\x09\x09else reject(ex);\x0a\x09\x09\x09}\x0a\x09\x09});\x0a\x09if (localReturn) throw localReturn; else return promise;\x0a'>",
  142. referencedClasses: [],
  143. //>>excludeEnd("ide");
  144. pragmas: [["inlineJS:", ["\x0a\x09var localReturn = 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) localReturn = ex;\x0a\x09\x09\x09\x09else reject(ex);\x0a\x09\x09\x09}\x0a\x09\x09});\x0a\x09if (localReturn) throw localReturn; 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 localReturn = 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) localReturn = ex;
  157. else reject(ex);
  158. }
  159. });
  160. if (localReturn) throw localReturn; 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\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<inlineJS: '\x0a\x09\x09try {\x0a\x09\x09\x09return aBlock._value();\x0a\x09\x09} catch(error) {\x0a\x09\x09\x09// pass non-local returns undetected\x0a\x09\x09\x09if (Array.isArray(error) && error.length === 1) throw error;\x0a\x09\x09\x09self._signal_(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// pass non-local returns undetected\x0a\x09\x09\x09if (Array.isArray(error) && error.length === 1) throw error;\x0a\x09\x09\x09self._signal_(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. // pass non-local returns undetected
  299. if (Array.isArray(error) && error.length === 1) throw error;
  300. self._signal_(error);
  301. }
  302. ;
  303. return self;
  304. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  305. }, function($ctx1) {$ctx1.fill(self,"try:",{aBlock:aBlock})});
  306. //>>excludeEnd("ctx");
  307. }; }),
  308. $globals.PromiseExecution);
  309. $core.addMethod(
  310. $core.method({
  311. selector: "value:",
  312. protocol: "settling",
  313. //>>excludeStart("ide", pragmas.excludeIdeData);
  314. args: ["anObject"],
  315. source: "value: anObject\x0a\x09resolveBlock value: anObject",
  316. referencedClasses: [],
  317. //>>excludeEnd("ide");
  318. pragmas: [],
  319. messageSends: ["value:"]
  320. }, function ($methodClass){ return function (anObject){
  321. var self=this,$self=this;
  322. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  323. return $core.withContext(function($ctx1) {
  324. //>>excludeEnd("ctx");
  325. $recv($self.resolveBlock)._value_(anObject);
  326. return self;
  327. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  328. }, function($ctx1) {$ctx1.fill(self,"value:",{anObject:anObject})});
  329. //>>excludeEnd("ctx");
  330. }; }),
  331. $globals.PromiseExecution);
  332. $core.addMethod(
  333. $core.method({
  334. selector: "resolveBlock:rejectBlock:",
  335. protocol: "instance creation",
  336. //>>excludeStart("ide", pragmas.excludeIdeData);
  337. args: ["aBlock", "anotherBlock"],
  338. source: "resolveBlock: aBlock rejectBlock: anotherBlock\x0a\x09^ super new\x0a\x09\x09resolveBlock: aBlock rejectBlock: anotherBlock;\x0a\x09\x09yourself",
  339. referencedClasses: [],
  340. //>>excludeEnd("ide");
  341. pragmas: [],
  342. messageSends: ["resolveBlock:rejectBlock:", "new", "yourself"]
  343. }, function ($methodClass){ return function (aBlock,anotherBlock){
  344. var self=this,$self=this;
  345. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  346. return $core.withContext(function($ctx1) {
  347. //>>excludeEnd("ctx");
  348. var $1;
  349. $1=[(
  350. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  351. $ctx1.supercall = true,
  352. //>>excludeEnd("ctx");
  353. ($methodClass.superclass||$boot.nilAsClass).fn.prototype._new.call($self))
  354. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  355. ,$ctx1.supercall = false
  356. //>>excludeEnd("ctx");
  357. ][0];
  358. $recv($1)._resolveBlock_rejectBlock_(aBlock,anotherBlock);
  359. return $recv($1)._yourself();
  360. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  361. }, function($ctx1) {$ctx1.fill(self,"resolveBlock:rejectBlock:",{aBlock:aBlock,anotherBlock:anotherBlock})});
  362. //>>excludeEnd("ctx");
  363. }; }),
  364. $globals.PromiseExecution.a$cls);
  365. $core.addTrait("TPromiseModel", "Kernel-Promises");
  366. $core.addMethod(
  367. $core.method({
  368. selector: "signal",
  369. protocol: "settling",
  370. //>>excludeStart("ide", pragmas.excludeIdeData);
  371. args: [],
  372. source: "signal\x0a\x09^ self signal: Error new",
  373. referencedClasses: ["Error"],
  374. //>>excludeEnd("ide");
  375. pragmas: [],
  376. messageSends: ["signal:", "new"]
  377. }, function ($methodClass){ return function (){
  378. var self=this,$self=this;
  379. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  380. return $core.withContext(function($ctx1) {
  381. //>>excludeEnd("ctx");
  382. return $self._signal_($recv($globals.Error)._new());
  383. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  384. }, function($ctx1) {$ctx1.fill(self,"signal",{})});
  385. //>>excludeEnd("ctx");
  386. }; }),
  387. $globals.TPromiseModel);
  388. $core.addMethod(
  389. $core.method({
  390. selector: "signal:",
  391. protocol: "settling",
  392. //>>excludeStart("ide", pragmas.excludeIdeData);
  393. args: ["anErrorObject"],
  394. source: "signal: anErrorObject\x0a\x09self subclassResponsibility",
  395. referencedClasses: [],
  396. //>>excludeEnd("ide");
  397. pragmas: [],
  398. messageSends: ["subclassResponsibility"]
  399. }, function ($methodClass){ return function (anErrorObject){
  400. var self=this,$self=this;
  401. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  402. return $core.withContext(function($ctx1) {
  403. //>>excludeEnd("ctx");
  404. $self._subclassResponsibility();
  405. return self;
  406. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  407. }, function($ctx1) {$ctx1.fill(self,"signal:",{anErrorObject:anErrorObject})});
  408. //>>excludeEnd("ctx");
  409. }; }),
  410. $globals.TPromiseModel);
  411. $core.addMethod(
  412. $core.method({
  413. selector: "value",
  414. protocol: "settling",
  415. //>>excludeStart("ide", pragmas.excludeIdeData);
  416. args: [],
  417. source: "value\x0a\x09^ self value: nil",
  418. referencedClasses: [],
  419. //>>excludeEnd("ide");
  420. pragmas: [],
  421. messageSends: ["value:"]
  422. }, function ($methodClass){ return function (){
  423. var self=this,$self=this;
  424. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  425. return $core.withContext(function($ctx1) {
  426. //>>excludeEnd("ctx");
  427. return $self._value_(nil);
  428. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  429. }, function($ctx1) {$ctx1.fill(self,"value",{})});
  430. //>>excludeEnd("ctx");
  431. }; }),
  432. $globals.TPromiseModel);
  433. $core.addMethod(
  434. $core.method({
  435. selector: "value:",
  436. protocol: "settling",
  437. //>>excludeStart("ide", pragmas.excludeIdeData);
  438. args: ["anObject"],
  439. source: "value: anObject\x0a\x09self subclassResponsibility",
  440. referencedClasses: [],
  441. //>>excludeEnd("ide");
  442. pragmas: [],
  443. messageSends: ["subclassResponsibility"]
  444. }, function ($methodClass){ return function (anObject){
  445. var self=this,$self=this;
  446. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  447. return $core.withContext(function($ctx1) {
  448. //>>excludeEnd("ctx");
  449. $self._subclassResponsibility();
  450. return self;
  451. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  452. }, function($ctx1) {$ctx1.fill(self,"value:",{anObject:anObject})});
  453. //>>excludeEnd("ctx");
  454. }; }),
  455. $globals.TPromiseModel);
  456. $core.addTrait("TThenable", "Kernel-Promises");
  457. $core.addMethod(
  458. $core.method({
  459. selector: "catch:",
  460. protocol: "promises",
  461. //>>excludeStart("ide", pragmas.excludeIdeData);
  462. args: ["aBlock"],
  463. source: "catch: aBlock\x0a<inlineJS: 'return self.then(null, function (err) { return aBlock._value_(err); })'>",
  464. referencedClasses: [],
  465. //>>excludeEnd("ide");
  466. pragmas: [["inlineJS:", ["return self.then(null, function (err) { return aBlock._value_(err); })"]]],
  467. messageSends: []
  468. }, function ($methodClass){ return function (aBlock){
  469. var self=this,$self=this;
  470. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  471. return $core.withContext(function($ctx1) {
  472. //>>excludeEnd("ctx");
  473. return self.then(null, function (err) { return aBlock._value_(err); });
  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 if (err._isKindOf_(aClass)) return aBlock._value_(err);\x0a else throw err;\x0a})'>",
  503. referencedClasses: [],
  504. //>>excludeEnd("ide");
  505. pragmas: [["inlineJS:", ["return self.then(null, function (err) {\x0a if (err._isKindOf_(aClass)) return aBlock._value_(err);\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. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  514. else throw err;
  515. });
  516. return self;
  517. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  518. }, function($ctx1) {$ctx1.fill(self,"on:do:",{aClass:aClass,aBlock:aBlock})});
  519. //>>excludeEnd("ctx");
  520. }; }),
  521. $globals.TThenable);
  522. $core.addMethod(
  523. $core.method({
  524. selector: "on:do:catch:",
  525. protocol: "promises",
  526. //>>excludeStart("ide", pragmas.excludeIdeData);
  527. args: ["aClass", "aBlock", "anotherBlock"],
  528. source: "on: aClass do: aBlock catch: anotherBlock\x0a\x09^ (self on: aClass do: aBlock) catch: anotherBlock",
  529. referencedClasses: [],
  530. //>>excludeEnd("ide");
  531. pragmas: [],
  532. messageSends: ["catch:", "on:do:"]
  533. }, function ($methodClass){ return function (aClass,aBlock,anotherBlock){
  534. var self=this,$self=this;
  535. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  536. return $core.withContext(function($ctx1) {
  537. //>>excludeEnd("ctx");
  538. return $recv($self._on_do_(aClass,aBlock))._catch_(anotherBlock);
  539. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  540. }, function($ctx1) {$ctx1.fill(self,"on:do:catch:",{aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock})});
  541. //>>excludeEnd("ctx");
  542. }; }),
  543. $globals.TThenable);
  544. $core.addMethod(
  545. $core.method({
  546. selector: "then:",
  547. protocol: "promises",
  548. //>>excludeStart("ide", pragmas.excludeIdeData);
  549. args: ["aBlockOrArray"],
  550. 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)'>",
  551. referencedClasses: [],
  552. //>>excludeEnd("ide");
  553. 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)"]]],
  554. messageSends: []
  555. }, function ($methodClass){ return function (aBlockOrArray){
  556. var self=this,$self=this;
  557. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  558. return $core.withContext(function($ctx1) {
  559. //>>excludeEnd("ctx");
  560. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  561. return array.reduce(function (soFar, aBlock) {
  562. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  563. function (result) {
  564. if (Array.isArray(result)) {
  565. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  566. } else {
  567. return aBlock._value_(result);
  568. }
  569. } :
  570. function (result) {
  571. return aBlock._value_(result);
  572. }
  573. );
  574. }, self);
  575. return self;
  576. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  577. }, function($ctx1) {$ctx1.fill(self,"then:",{aBlockOrArray:aBlockOrArray})});
  578. //>>excludeEnd("ctx");
  579. }; }),
  580. $globals.TThenable);
  581. $core.addMethod(
  582. $core.method({
  583. selector: "then:catch:",
  584. protocol: "promises",
  585. //>>excludeStart("ide", pragmas.excludeIdeData);
  586. args: ["aBlockOrArray", "anotherBlock"],
  587. source: "then: aBlockOrArray catch: anotherBlock\x0a\x09^ (self then: aBlockOrArray) catch: anotherBlock",
  588. referencedClasses: [],
  589. //>>excludeEnd("ide");
  590. pragmas: [],
  591. messageSends: ["catch:", "then:"]
  592. }, function ($methodClass){ return function (aBlockOrArray,anotherBlock){
  593. var self=this,$self=this;
  594. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  595. return $core.withContext(function($ctx1) {
  596. //>>excludeEnd("ctx");
  597. return $recv($self._then_(aBlockOrArray))._catch_(anotherBlock);
  598. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  599. }, function($ctx1) {$ctx1.fill(self,"then:catch:",{aBlockOrArray:aBlockOrArray,anotherBlock:anotherBlock})});
  600. //>>excludeEnd("ctx");
  601. }; }),
  602. $globals.TThenable);
  603. $core.addMethod(
  604. $core.method({
  605. selector: "then:on:do:",
  606. protocol: "promises",
  607. //>>excludeStart("ide", pragmas.excludeIdeData);
  608. args: ["aBlockOrArray", "aClass", "aBlock"],
  609. source: "then: aBlockOrArray on: aClass do: aBlock\x0a\x09^ (self then: aBlockOrArray) on: aClass do: aBlock",
  610. referencedClasses: [],
  611. //>>excludeEnd("ide");
  612. pragmas: [],
  613. messageSends: ["on:do:", "then:"]
  614. }, function ($methodClass){ return function (aBlockOrArray,aClass,aBlock){
  615. var self=this,$self=this;
  616. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  617. return $core.withContext(function($ctx1) {
  618. //>>excludeEnd("ctx");
  619. return $recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock);
  620. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  621. }, function($ctx1) {$ctx1.fill(self,"then:on:do:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock})});
  622. //>>excludeEnd("ctx");
  623. }; }),
  624. $globals.TThenable);
  625. $core.addMethod(
  626. $core.method({
  627. selector: "then:on:do:catch:",
  628. protocol: "promises",
  629. //>>excludeStart("ide", pragmas.excludeIdeData);
  630. args: ["aBlockOrArray", "aClass", "aBlock", "anotherBlock"],
  631. source: "then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock\x0a\x09^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock",
  632. referencedClasses: [],
  633. //>>excludeEnd("ide");
  634. pragmas: [],
  635. messageSends: ["catch:", "on:do:", "then:"]
  636. }, function ($methodClass){ return function (aBlockOrArray,aClass,aBlock,anotherBlock){
  637. var self=this,$self=this;
  638. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  639. return $core.withContext(function($ctx1) {
  640. //>>excludeEnd("ctx");
  641. return $recv($recv($self._then_(aBlockOrArray))._on_do_(aClass,aBlock))._catch_(anotherBlock);
  642. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  643. }, function($ctx1) {$ctx1.fill(self,"then:on:do:catch:",{aBlockOrArray:aBlockOrArray,aClass:aClass,aBlock:aBlock,anotherBlock:anotherBlock})});
  644. //>>excludeEnd("ctx");
  645. }; }),
  646. $globals.TThenable);
  647. $core.setTraitComposition([{trait: $globals.TThenable}], $globals.Promise);
  648. $core.setTraitComposition([{trait: $globals.TPromiseModel}], $globals.Promise.a$cls);
  649. $core.setTraitComposition([{trait: $globals.TPromiseModel}], $globals.PromiseExecution);
  650. });