Kernel-Promises.st 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. Smalltalk createPackage: 'Kernel-Promises'!
  2. Object subclass: #Promise
  3. slots: {}
  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. delayMilliseconds: aNumber
  16. ^ self new: [ :model | [ model value: nil ] valueWithTimeout: aNumber ]
  17. !
  18. forBlock: aBlock
  19. "Returns a Promise that is resolved with the value of aBlock,
  20. and rejected if error happens while evaluating aBlock."
  21. ^ self new then: aBlock
  22. !
  23. new
  24. "Returns a dumb Promise resolved with nil."
  25. <inlineJS: 'return Promise.resolve()'>
  26. !
  27. new: aBlock
  28. "Returns a Promise that is eventually resolved or rejected.
  29. Pass a block that is called with one argument, model.
  30. You should call model value: ... to resolve the promise
  31. and model signal: ... to reject the promise.
  32. If error happens during run of the block,
  33. promise is rejected with that error as well."
  34. <inlineJS: '
  35. var nonLocalReturn = null,
  36. promise = new Promise(function (resolve, reject) {
  37. var model = $globals.PromiseExecution._resolveBlock_rejectBlock_(resolve, reject);
  38. try { aBlock._value_(model); }
  39. catch (ex) {
  40. if (Array.isArray(ex) && ex.length === 1) nonLocalReturn = ex;
  41. else reject(ex);
  42. }
  43. });
  44. if (nonLocalReturn) throw nonLocalReturn; else return promise;
  45. '>
  46. !
  47. signal: anObject
  48. "Returns a Promise rejected with anObject."
  49. <inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>
  50. !
  51. value: anObject
  52. "Returns a Promise resolved with anObject."
  53. <inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>
  54. ! !
  55. Object subclass: #PromiseExecution
  56. slots: {#resolveBlock. #rejectBlock}
  57. package: 'Kernel-Promises'!
  58. !PromiseExecution methodsFor: 'accessing'!
  59. resolveBlock: aBlock rejectBlock: anotherBlock
  60. resolveBlock := aBlock.
  61. rejectBlock := anotherBlock
  62. ! !
  63. !PromiseExecution methodsFor: 'evaluating'!
  64. do: aBlock
  65. "Executes a block 'in the context of a promise' and resolves.
  66. That is, if it ends with an error, promise is rejected.
  67. If a block succeeds, promise is resolved with its return value.
  68. Non-local returns are also treated as an error and reified as rejections."
  69. self value: (self try: aBlock)
  70. !
  71. try: aBlock
  72. "Executes a block 'in the context of a promise'.
  73. That is, if it ends with an error, promise is rejected.
  74. Non-local returns are also treated as an error and reified as rejections."
  75. <inlineJS: '
  76. try {
  77. return aBlock._value();
  78. } catch(error) {
  79. $self._signal_(
  80. Array.isArray(error) && error.length === 1 ?
  81. $globals.NonLifoReturn._value_(error[0]) :
  82. error
  83. );
  84. }
  85. '>
  86. ! !
  87. !PromiseExecution methodsFor: 'settling'!
  88. signal: anErrorObject
  89. rejectBlock value: anErrorObject
  90. !
  91. value: anObject
  92. resolveBlock value: anObject
  93. ! !
  94. !PromiseExecution class methodsFor: 'instance creation'!
  95. resolveBlock: aBlock rejectBlock: anotherBlock
  96. ^ super new
  97. resolveBlock: aBlock rejectBlock: anotherBlock;
  98. yourself
  99. ! !
  100. Trait named: #TPromiseModel
  101. package: 'Kernel-Promises'!
  102. !TPromiseModel methodsFor: 'settling'!
  103. signal
  104. ^ self signal: Error new
  105. !
  106. signal: anErrorObject
  107. self subclassResponsibility
  108. !
  109. value
  110. ^ self value: nil
  111. !
  112. value: anObject
  113. self subclassResponsibility
  114. ! !
  115. Trait named: #TThenable
  116. package: 'Kernel-Promises'!
  117. !TThenable methodsFor: 'promises'!
  118. catch: aBlock
  119. <inlineJS: 'return self.then(null, function (err) { return aBlock._value_(err); })'>
  120. !
  121. on: aClass do: aBlock
  122. <inlineJS: 'return self.then(null, function (err) {
  123. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  124. else throw err;
  125. })'>
  126. !
  127. on: aClass do: aBlock catch: anotherBlock
  128. ^ (self on: aClass do: aBlock) catch: anotherBlock
  129. !
  130. then: aBlockOrArray
  131. "Accepts a block or array of blocks.
  132. Each of blocks in the array or the singleton one is
  133. used in .then call to a promise, to accept a result
  134. and transform it to the result for the next one.
  135. In case a block has more than one argument
  136. and result is an array, first n-1 elements of the array
  137. are put into additional arguments beyond the first.
  138. The first argument always contains the result as-is."
  139. <inlineJS: '
  140. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  141. return array.reduce(function (soFar, aBlock) {
  142. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  143. function (result) {
  144. if (Array.isArray(result)) {
  145. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  146. } else {
  147. return aBlock._value_(result);
  148. }
  149. } :
  150. function (result) {
  151. return aBlock._value_(result);
  152. }
  153. );
  154. }, self)'>
  155. !
  156. then: aBlockOrArray catch: anotherBlock
  157. ^ (self then: aBlockOrArray) catch: anotherBlock
  158. !
  159. then: aBlockOrArray on: aClass do: aBlock
  160. ^ (self then: aBlockOrArray) on: aClass do: aBlock
  161. !
  162. then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
  163. ^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock
  164. ! !
  165. !TThenable methodsFor: 'testing'!
  166. isThenable
  167. ^ true
  168. ! !
  169. Promise setTraitComposition: {TThenable} asTraitComposition!
  170. Promise class setTraitComposition: {TPromiseModel} asTraitComposition!
  171. PromiseExecution setTraitComposition: {TPromiseModel} asTraitComposition!
  172. ! !