Kernel-Promises.st 6.2 KB

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