rpromise 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rpromise/version.rb +1 -1
- data/lib/rpromise.rb +2 -3
- data/spec/lib/rpromise_spec.rb +26 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71a0304c401834de12b087ed0616ad637b7ad3a6
|
4
|
+
data.tar.gz: b38e756b3ccb4dcc60dbdffe866987f29ad34314
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b09464f4d17c111bafae20fcbdcff7d000b8edcf715e1bbb320ebb07e84c4bb14b0e2d1b53b5000bb079aadb00c8e3aa2ab14ded7f89a2d4e45b23a4f7f8486
|
7
|
+
data.tar.gz: f2f7a02741c54b6e7452131b9fc4d54bb5d1b652f1a29f1fa9a483e894aa60baffa061c450f55d13ed0a96ba2499ef34bd9d94aa19993ba55c0d711e5e9f66bb
|
data/lib/rpromise/version.rb
CHANGED
data/lib/rpromise.rb
CHANGED
@@ -17,12 +17,11 @@ class Rpromise
|
|
17
17
|
@thread = Thread.new do
|
18
18
|
begin
|
19
19
|
yield(method(:resolve!), method(:reject!))
|
20
|
-
rescue
|
20
|
+
rescue => e
|
21
21
|
reject!(e)
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
25
|
-
reject!(e)
|
24
|
+
@thread.abort_on_exception = true
|
26
25
|
end
|
27
26
|
|
28
27
|
def then(on_resolved = nil, on_rejected = nil)
|
data/spec/lib/rpromise_spec.rb
CHANGED
@@ -5,6 +5,7 @@ describe ::Rpromise do
|
|
5
5
|
let(:n) { 0 }
|
6
6
|
subject(:promise) do
|
7
7
|
described_class.new do |resolve, reject|
|
8
|
+
sleep(0.1)
|
8
9
|
end
|
9
10
|
end
|
10
11
|
|
@@ -14,6 +15,7 @@ describe ::Rpromise do
|
|
14
15
|
it 'uses the resolve! method as callback' do
|
15
16
|
lock = true
|
16
17
|
p = described_class.new do |resolve, reject|
|
18
|
+
sleep(0.1)
|
17
19
|
resolve.call('hi')
|
18
20
|
lock = false
|
19
21
|
end
|
@@ -24,6 +26,7 @@ describe ::Rpromise do
|
|
24
26
|
it 'uses the reject! method as callback' do
|
25
27
|
lock = true
|
26
28
|
p = described_class.new do |resolve, reject|
|
29
|
+
sleep(0.1)
|
27
30
|
reject.call('hi')
|
28
31
|
lock = false
|
29
32
|
end
|
@@ -57,6 +60,7 @@ describe ::Rpromise do
|
|
57
60
|
|
58
61
|
before(:each) do
|
59
62
|
@promise = described_class.new do |resolve, reject|
|
63
|
+
sleep(0.1)
|
60
64
|
resolve.call(value)
|
61
65
|
end
|
62
66
|
end
|
@@ -104,6 +108,7 @@ describe ::Rpromise do
|
|
104
108
|
@promise.then(lambda do |v|
|
105
109
|
return described_class.new do |resolve, reject|
|
106
110
|
thread2 = Thread.new do
|
111
|
+
sleep(0.1)
|
107
112
|
resolve.call('Hello world!')
|
108
113
|
end
|
109
114
|
end
|
@@ -136,6 +141,7 @@ describe ::Rpromise do
|
|
136
141
|
lambda_error = nil
|
137
142
|
lock = true
|
138
143
|
p = described_class.new do |resolve, reject|
|
144
|
+
sleep(0.1)
|
139
145
|
reject.call(error)
|
140
146
|
end
|
141
147
|
p.then(nil, lambda do |e|
|
@@ -145,10 +151,12 @@ describe ::Rpromise do
|
|
145
151
|
loop { break unless lock }
|
146
152
|
expect(lambda_error).to eq error
|
147
153
|
end
|
154
|
+
|
148
155
|
it 'handles raised exceptions within the promise' do
|
149
156
|
lambda_error = nil
|
150
157
|
lock = true
|
151
158
|
p = described_class.new do |resolve, reject|
|
159
|
+
sleep(0.1)
|
152
160
|
raise 'Oops'
|
153
161
|
end
|
154
162
|
p.then(nil, lambda do |e|
|
@@ -159,8 +167,24 @@ describe ::Rpromise do
|
|
159
167
|
expect(lambda_error).to be_kind_of RuntimeError
|
160
168
|
expect(lambda_error.message).to eq 'Oops'
|
161
169
|
end
|
162
|
-
|
163
|
-
|
170
|
+
|
171
|
+
it 'handles raised exceptions within resolve!' do
|
172
|
+
lambda_error = nil
|
173
|
+
lock = true
|
174
|
+
p = described_class.new do |resolve, reject|
|
175
|
+
sleep(0.1)
|
176
|
+
resolve.call(true)
|
177
|
+
end
|
178
|
+
# Skip the lambda argument to generate an error
|
179
|
+
p.then(lambda do
|
180
|
+
lock = false
|
181
|
+
end, lambda do |err|
|
182
|
+
lambda_error = err
|
183
|
+
lock = false
|
184
|
+
end)
|
185
|
+
loop { break unless lock }
|
186
|
+
expect(lambda_error).to be_kind_of ArgumentError
|
187
|
+
end
|
164
188
|
end
|
165
189
|
end
|
166
190
|
|