Kernel-Promises.st 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. Smalltalk createPackage: 'Kernel-Promises'!
  2. Object subclass: #Promise
  3. instanceVariableNames: ''
  4. package: 'Kernel-Promises'!
  5. !Promise class methodsFor: 'composites'!
  6. all: aCollection
  7. "Returns a Promise resolved with results of sub-promises."
  8. <inlineJS: 'return Promise.all($recv(aCollection)._asArray())'>
  9. !
  10. any: aCollection
  11. "Returns a Promise resolved with first result of sub-promises."
  12. <inlineJS: 'return Promise.race($recv(aCollection)._asArray())'>
  13. ! !
  14. !Promise class methodsFor: 'instance creation'!
  15. forBlock: aBlock
  16. "Returns a Promise that is resolved with the value of aBlock,
  17. and rejected if error happens while evaluating aBlock."
  18. ^ self new then: aBlock
  19. !
  20. new
  21. "Returns a dumb Promise resolved with nil."
  22. <inlineJS: 'return Promise.resolve()'>
  23. !
  24. new: aBlock
  25. "Returns a Promise that is eventually resolved or rejected.
  26. Pass a block that is called with one argument, model.
  27. You should call model value: ... to resolve the promise
  28. and model signal: ... to reject the promise.
  29. If error happens during run of the block,
  30. promise is rejected with that error as well."
  31. <inlineJS: 'return new Promise(function (resolve, reject) {
  32. var model = {value: resolve, signal: reject}; // TODO make faster
  33. aBlock._value_(model);
  34. })'>
  35. !
  36. signal: anObject
  37. "Returns a Promise rejected with anObject."
  38. <inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>
  39. !
  40. value: anObject
  41. "Returns a Promise resolved with anObject."
  42. <inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>
  43. ! !
  44. Trait named: #TThenable
  45. package: 'Kernel-Promises'!
  46. !TThenable methodsFor: 'promises'!
  47. catch: aBlock
  48. <inlineJS: 'return self.then(null, function (err) {return $core.seamless(function () {
  49. return aBlock._value_(err);
  50. })})'>
  51. !
  52. on: aClass do: aBlock
  53. <inlineJS: 'return self.then(null, function (err) {return $core.seamless(function () {
  54. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  55. else throw err;
  56. })})'>
  57. !
  58. on: aClass do: aBlock catch: anotherBlock
  59. ^ (self on: aClass do: aBlock) catch: anotherBlock
  60. !
  61. then: aBlockOrArray
  62. "Accepts a block or array of blocks.
  63. Each of blocks in the array or the singleton one is
  64. used in .then call to a promise, to accept a result
  65. and transform it to the result for the next one.
  66. In case a block has more than one argument
  67. and result is an array, first n-1 elements of the array
  68. are put into additional arguments beyond the first.
  69. The first argument always contains the result as-is."
  70. <inlineJS: '
  71. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  72. return array.reduce(function (soFar, aBlock) {
  73. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  74. function (result) {return $core.seamless(function () {
  75. if (Array.isArray(result)) {
  76. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  77. } else {
  78. return aBlock._value_(result);
  79. }
  80. })} :
  81. function (result) {return $core.seamless(function () {
  82. return aBlock._value_(result);
  83. })}
  84. );
  85. }, self)'>
  86. !
  87. then: aBlockOrArray catch: anotherBlock
  88. ^ (self then: aBlockOrArray) catch: anotherBlock
  89. !
  90. then: aBlockOrArray on: aClass do: aBlock
  91. ^ (self then: aBlockOrArray) on: aClass do: aBlock
  92. !
  93. then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
  94. ^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock
  95. ! !
  96. Promise setTraitComposition: {TThenable} asTraitComposition!
  97. ! !
  98. !JSObjectProxy methodsFor: '*Kernel-Promises'!
  99. catch: aBlock
  100. <inlineJS: 'var js = self["@jsObject"];
  101. if (typeof js.then === "function")
  102. return $globals.Thenable.fn.prototype._catch_.call(js, aBlock);
  103. else
  104. return self._doesNotUnderstand_(
  105. $globals.Message._new()
  106. ._selector_("catch:")
  107. ._arguments_([aBlock])
  108. )'>
  109. !
  110. on: aClass do: aBlock
  111. <inlineJS: 'var js = self["@jsObject"];
  112. if (typeof js.then === "function")
  113. return $globals.Thenable.fn.prototype._on_do_.call(js, aClass, aBlock);
  114. else
  115. return self._doesNotUnderstand_(
  116. $globals.Message._new()
  117. ._selector_("on:do:")
  118. ._arguments_([aClass, aBlock])
  119. )'>
  120. !
  121. on: aClass do: aBlock catch: anotherBlock
  122. <inlineJS: 'var js = self["@jsObject"];
  123. if (typeof js.then === "function")
  124. return $globals.Thenable.fn.prototype._on_do_catch_.call(js, aClass, aBlock, anotherBlock);
  125. else
  126. return self._doesNotUnderstand_(
  127. $globals.Message._new()
  128. ._selector_("on:do:catch:")
  129. ._arguments_([aClass, aBlock, anotherBlock])
  130. )'>
  131. !
  132. then: aBlockOrArray
  133. <inlineJS: 'var js = self["@jsObject"];
  134. if (typeof js.then === "function")
  135. return $globals.Thenable.fn.prototype._then_.call(js, aBlockOrArray);
  136. else
  137. return self._doesNotUnderstand_(
  138. $globals.Message._new()
  139. ._selector_("then:")
  140. ._arguments_([aBlockOrArray])
  141. )'>
  142. !
  143. then: aBlockOrArray catch: anotherBlock
  144. <inlineJS: 'var js = self["@jsObject"];
  145. if (typeof js.then === "function")
  146. return $globals.Thenable.fn.prototype._then_catch_.call(js, aBlockOrArray, anotherBlock);
  147. else
  148. return self._doesNotUnderstand_(
  149. $globals.Message._new()
  150. ._selector_("then:catch:")
  151. ._arguments_([aBlockOrArray, anotherBlock])
  152. )'>
  153. !
  154. then: aBlockOrArray on: aClass do: aBlock
  155. <inlineJS: 'var js = self["@jsObject"];
  156. if (typeof js.then === "function")
  157. return $globals.Thenable.fn.prototype._then_on_do_.call(js, aBlockOrArray, aClass, aBlock);
  158. else
  159. return self._doesNotUnderstand_(
  160. $globals.Message._new()
  161. ._selector_("then:on:do:")
  162. ._arguments_([aBlockOrArray, aClass, aBlock])
  163. )'>
  164. !
  165. then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
  166. <inlineJS: 'var js = self["@jsObject"];
  167. if (typeof js.then === "function")
  168. return $globals.Thenable.fn.prototype._then_on_do_catch_.call(js, aBlockOrArray, aClass, aBlock, anotherBlock);
  169. else
  170. return self._doesNotUnderstand_(
  171. $globals.Message._new()
  172. ._selector_("then:on:do:catch:")
  173. ._arguments_([aBlockOrArray, aClass, aBlock, anotherBlock])
  174. )'>
  175. ! !