Kernel-Promises.js 27 KB

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