Browse Source

Move Thenable class to TThenable trait.

Herbert Vojčík 7 months 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 @@
1
+0.19.0:
2
+
3
+* Remove class Thenable, add trait TThenable.
4
+
5
+
1 6
 0.18.3:
2 7
 
3 8
 * Move {add,remove}Element from Array.prototype to kernel API.

+ 1 - 0
CHANGELOG

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

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


+ 53 - 60
src/Kernel-Promises.st

@@ -1,15 +1,60 @@
1 1
 Smalltalk createPackage: 'Kernel-Promises'!
2
-Object subclass: #Thenable
2
+Object subclass: #Promise
3 3
 	instanceVariableNames: ''
4 4
 	package: 'Kernel-Promises'!
5
-!Thenable commentStamp!
6
-I am the abstract base class for Promises.
7 5
 
8
-My subclasses should wrap existing JS implementations.
6
+!Promise class methodsFor: 'composites'!
7
+
8
+all: aCollection
9
+"Returns a Promise resolved with results of sub-promises."
10
+<inlineJS: 'return Promise.all($recv(aCollection)._asArray())'>
11
+!
12
+
13
+any: aCollection
14
+"Returns a Promise resolved with first result of sub-promises."
15
+<inlineJS: 'return Promise.race($recv(aCollection)._asArray())'>
16
+! !
17
+
18
+!Promise class methodsFor: 'instance creation'!
19
+
20
+forBlock: aBlock
21
+"Returns a Promise that is resolved with the value of aBlock,
22
+and rejected if error happens while evaluating aBlock."
23
+	^ self new then: aBlock
24
+!
25
+
26
+new
27
+"Returns a dumb Promise resolved with nil."
28
+<inlineJS: 'return Promise.resolve()'>
29
+!
30
+
31
+new: aBlock
32
+"Returns a Promise that is eventually resolved or rejected.
33
+Pass a block that is called with one argument, model.
34
+You should call model value: ... to resolve the promise
35
+and model signal: ... to reject the promise.
36
+If error happens during run of the block,
37
+promise is rejected with that error as well."
38
+<inlineJS: 'return new Promise(function (resolve, reject) {
39
+    var model = {value: resolve, signal: reject}; // TODO make faster
40
+    aBlock._value_(model);
41
+})'>
42
+!
9 43
 
10
-I contain methods that wrap Promises/A+ `.then` behaviour.!
44
+signal: anObject
45
+"Returns a Promise rejected with anObject."
46
+<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>
47
+!
48
+
49
+value: anObject
50
+"Returns a Promise resolved with anObject."
51
+<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>
52
+! !
53
+
54
+Trait named: #TThenable
55
+	package: 'Kernel-Promises'!
11 56
 
12
-!Thenable methodsFor: 'promises'!
57
+!TThenable methodsFor: 'promises'!
13 58
 
14 59
 catch: aBlock
15 60
 <inlineJS: 'return self.then(null, function (err) {return $core.seamless(function () {
@@ -25,10 +70,7 @@ on: aClass do: aBlock
25 70
 !
26 71
 
27 72
 on: aClass do: aBlock catch: anotherBlock
28
-<inlineJS: 'return self.then(null, function (err) {return $core.seamless(function () {
29
-    try { if (err._isKindOf_(aClass)) return aBlock._value_(err); } catch (e) { err = e; }
30
-    return anotherBlock._value_(err);
31
-})})'>
73
+	^ (self on: aClass do: aBlock) catch: anotherBlock
32 74
 !
33 75
 
34 76
 then: aBlockOrArray
@@ -70,56 +112,7 @@ then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
70 112
 	^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock
71 113
 ! !
72 114
 
73
-Thenable subclass: #Promise
74
-	instanceVariableNames: ''
75
-	package: 'Kernel-Promises'!
76
-
77
-!Promise class methodsFor: 'composites'!
78
-
79
-all: aCollection
80
-"Returns a Promise resolved with results of sub-promises."
81
-<inlineJS: 'return Promise.all($recv(aCollection)._asArray())'>
82
-!
83
-
84
-any: aCollection
85
-"Returns a Promise resolved with first result of sub-promises."
86
-<inlineJS: 'return Promise.race($recv(aCollection)._asArray())'>
87
-! !
88
-
89
-!Promise class methodsFor: 'instance creation'!
90
-
91
-forBlock: aBlock
92
-"Returns a Promise that is resolved with the value of aBlock,
93
-and rejected if error happens while evaluating aBlock."
94
-	^ self new then: aBlock
95
-!
96
-
97
-new
98
-"Returns a dumb Promise resolved with nil."
99
-<inlineJS: 'return Promise.resolve()'>
100
-!
101
-
102
-new: aBlock
103
-"Returns a Promise that is eventually resolved or rejected.
104
-Pass a block that is called with one argument, model.
105
-You should call model value: ... to resolve the promise
106
-and model signal: ... to reject the promise.
107
-If error happens during run of the block,
108
-promise is rejected with that error as well."
109
-<inlineJS: 'return new Promise(function (resolve, reject) {
110
-    var model = {value: resolve, signal: reject}; // TODO make faster
111
-    aBlock._value_(model);
112
-})'>
113
-!
114
-
115
-signal: anObject
116
-"Returns a Promise rejected with anObject."
117
-<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>
118
-!
119
-
120
-value: anObject
121
-"Returns a Promise resolved with anObject."
122
-<inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>
115
+Promise setTraitComposition: {TThenable} asTraitComposition!
123 116
 ! !
124 117
 
125 118
 !JSObjectProxy methodsFor: '*Kernel-Promises'!