Browse Source

Move Thenable class to TThenable trait.

Herbert Vojčík 1 year ago
parent
commit
61f19cfe38
4 changed files with 210 additions and 217 deletions
  1. 5 0
      API-CHANGES.txt
  2. 1 0
      CHANGELOG
  3. 151 157
      src/Kernel-Promises.js
  4. 53 60
      src/Kernel-Promises.st

+ 5 - 0
API-CHANGES.txt

@@ -1,3 +1,8 @@
+0.19.0:
+
+* Remove class Thenable, add trait TThenable.
+
+
 0.18.3:
 
 * Move {add,remove}Element from Array.prototype to kernel API.

+ 1 - 0
CHANGELOG

@@ -2,6 +2,7 @@
 ===================================
 
 * Removed BehaviorBody in favour of traits TBehaviorDefaults and TBehaviorProvider.
+* Move Thenable class to TThenable trait.
 
 Commits: https://lolg.it/amber/amber/commits/0.19.0.
 

File diff suppressed because it is too large
+ 151 - 157
src/Kernel-Promises.js


+ 53 - 60
src/Kernel-Promises.st

@@ -1,15 +1,60 @@
 Smalltalk createPackage: 'Kernel-Promises'!
-Object subclass: #Thenable
+Object subclass: #Promise
 	instanceVariableNames: ''
 	package: 'Kernel-Promises'!
-!Thenable commentStamp!
-I am the abstract base class for Promises.
 
-My subclasses should wrap existing JS implementations.
+!Promise class methodsFor: 'composites'!
+
+all: aCollection
+"Returns a Promise resolved with results of sub-promises."
+<inlineJS: 'return Promise.all($recv(aCollection)._asArray())'>
+!
+
+any: aCollection
+"Returns a Promise resolved with first result of sub-promises."
+<inlineJS: 'return Promise.race($recv(aCollection)._asArray())'>
+! !
+
+!Promise class methodsFor: 'instance creation'!
+
+forBlock: aBlock
+"Returns a Promise that is resolved with the value of aBlock,
+and rejected if error happens while evaluating aBlock."
+	^ self new then: aBlock
+!
+
+new
+"Returns a dumb Promise resolved with nil."
+<inlineJS: 'return Promise.resolve()'>
+!
+
+new: aBlock
+"Returns a Promise that is eventually resolved or rejected.
+Pass a block that is called with one argument, model.
+You should call model value: ... to resolve the promise
+and model signal: ... to reject the promise.
+If error happens during run of the block,
+promise is rejected with that error as well."
+<inlineJS: 'return new Promise(function (resolve, reject) {
+    var model = {value: resolve, signal: reject}; // TODO make faster
+    aBlock._value_(model);
+})'>
+!
 
-I contain methods that wrap Promises/A+ `.then` behaviour.!
+signal: anObject
+"Returns a Promise rejected with anObject."
+<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>
+!
+
+value: anObject
+"Returns a Promise resolved with anObject."
+<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>
+! !
+
+Trait named: #TThenable
+	package: 'Kernel-Promises'!
 
-!Thenable methodsFor: 'promises'!
+!TThenable methodsFor: 'promises'!
 
 catch: aBlock
 <inlineJS: 'return self.then(null, function (err) {return $core.seamless(function () {
@@ -25,10 +70,7 @@ on: aClass do: aBlock
 !
 
 on: aClass do: aBlock catch: anotherBlock
-<inlineJS: 'return self.then(null, function (err) {return $core.seamless(function () {
-    try { if (err._isKindOf_(aClass)) return aBlock._value_(err); } catch (e) { err = e; }
-    return anotherBlock._value_(err);
-})})'>
+	^ (self on: aClass do: aBlock) catch: anotherBlock
 !
 
 then: aBlockOrArray
@@ -70,56 +112,7 @@ then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
 	^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock
 ! !
 
-Thenable subclass: #Promise
-	instanceVariableNames: ''
-	package: 'Kernel-Promises'!
-
-!Promise class methodsFor: 'composites'!
-
-all: aCollection
-"Returns a Promise resolved with results of sub-promises."
-<inlineJS: 'return Promise.all($recv(aCollection)._asArray())'>
-!
-
-any: aCollection
-"Returns a Promise resolved with first result of sub-promises."
-<inlineJS: 'return Promise.race($recv(aCollection)._asArray())'>
-! !
-
-!Promise class methodsFor: 'instance creation'!
-
-forBlock: aBlock
-"Returns a Promise that is resolved with the value of aBlock,
-and rejected if error happens while evaluating aBlock."
-	^ self new then: aBlock
-!
-
-new
-"Returns a dumb Promise resolved with nil."
-<inlineJS: 'return Promise.resolve()'>
-!
-
-new: aBlock
-"Returns a Promise that is eventually resolved or rejected.
-Pass a block that is called with one argument, model.
-You should call model value: ... to resolve the promise
-and model signal: ... to reject the promise.
-If error happens during run of the block,
-promise is rejected with that error as well."
-<inlineJS: 'return new Promise(function (resolve, reject) {
-    var model = {value: resolve, signal: reject}; // TODO make faster
-    aBlock._value_(model);
-})'>
-!
-
-signal: anObject
-"Returns a Promise rejected with anObject."
-<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>
-!
-
-value: anObject
-"Returns a Promise resolved with anObject."
-<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>
+Promise setTraitComposition: {TThenable} asTraitComposition!
 ! !
 
 !JSObjectProxy methodsFor: '*Kernel-Promises'!