when 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/Gemfile.lock +18 -0
- data/Rakefile +10 -0
- data/Readme.md +119 -0
- data/lib/when.rb +135 -0
- data/lib/when/version.rb +28 -0
- data/test/test_helper.rb +28 -0
- data/test/when_test.rb +249 -0
- data/when.gemspec +46 -0
- metadata +105 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# When.rb
|
2
|
+
|
3
|
+
This library is a Ruby port of the wonderful JavaScript promise library
|
4
|
+
[when.js](https://github.com/cujojs/when). The API is kept as close to the
|
5
|
+
original as possible. In cases where there's a trade-off between 1:1 API
|
6
|
+
compatibility and "being Ruby-like", the latter is preferred. An example is
|
7
|
+
`When.defer`, which yields the newly created deferred to a block in Ruby.
|
8
|
+
|
9
|
+
This is currently a minimal implementation and the port is being built on a
|
10
|
+
what-I-need basis.
|
11
|
+
|
12
|
+
## API
|
13
|
+
|
14
|
+
### `When(value_or_promise)`
|
15
|
+
|
16
|
+
Returns a promise. If the value is already a promise, it is returned. Otherwise,
|
17
|
+
a new promise is created and immediately resolved with the provided value.
|
18
|
+
|
19
|
+
### `When.defer`
|
20
|
+
|
21
|
+
Create a deferred, equivalent to `When::Deferred.new`. The deferred can be
|
22
|
+
"split" in its resolver and promise parts for better encapsulation:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require "when"
|
26
|
+
require "eventmachine"
|
27
|
+
|
28
|
+
def async
|
29
|
+
deferred = When.defer
|
30
|
+
EventMachine.defer { deferred.resolver.resolve(42) }
|
31
|
+
deferred.promise
|
32
|
+
end
|
33
|
+
|
34
|
+
EventMachine.run do
|
35
|
+
async.then do |num
|
36
|
+
puts "Got number #{num}"
|
37
|
+
EventMachine.stop
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
In this example, the returned promise can only register success and failure
|
43
|
+
callbacks, it can not be used to resolve the deferred.
|
44
|
+
|
45
|
+
### `When.resolve(value)`
|
46
|
+
|
47
|
+
Creates a deferred and immediately resolves it with `value`, then returns the
|
48
|
+
promise.
|
49
|
+
|
50
|
+
### `When.reject(value)`
|
51
|
+
|
52
|
+
Creates a deferred and immediately rejects it with `value`, then returns the
|
53
|
+
promise.
|
54
|
+
|
55
|
+
### `When.all(promises)`
|
56
|
+
|
57
|
+
Takes an array of promises/deferreds and returns a promise that will either
|
58
|
+
reject when the first promise rejects, or resolve when all promises have
|
59
|
+
resolved.
|
60
|
+
|
61
|
+
## Why?
|
62
|
+
|
63
|
+
Working with EventMachine, I found that I didn't care to much for its deferrable
|
64
|
+
implementation. My main objections was:
|
65
|
+
|
66
|
+
1. The way it encourages you to extend deferrable types with custom logic to
|
67
|
+
create deferrable business objects.
|
68
|
+
2. (More importantly) How it allows deferrables to be resolved multiple times.
|
69
|
+
|
70
|
+
|
71
|
+
## Installing
|
72
|
+
|
73
|
+
`when` ships as a gem:
|
74
|
+
|
75
|
+
$ gem install when
|
76
|
+
|
77
|
+
Or in your Gemfile:
|
78
|
+
|
79
|
+
gem "when", "~> 0"
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
Contributions are welcome. To get started:
|
84
|
+
|
85
|
+
$ git clone git://gitorious.org/gitorious/when.git
|
86
|
+
$ cd when
|
87
|
+
$ bundle install
|
88
|
+
$ rake
|
89
|
+
|
90
|
+
When you have fixed a bug/added a feature/done your thing, create a
|
91
|
+
[clone on Gitorious](http://gitorious.org/gitorious/when-rb) or a
|
92
|
+
[fork on GitHub](http://github.com/cjohansen/when-rb) and send a
|
93
|
+
merge request/pull request, whichever you prefer.
|
94
|
+
|
95
|
+
Please add tests when adding/altering code, and always make sure all the tests
|
96
|
+
pass before submitting your contribution.
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
### The MIT License (MIT)
|
101
|
+
|
102
|
+
**Copyright (C) 2012 Gitorious AS**
|
103
|
+
|
104
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
105
|
+
this software and associated documentation files (the "Software"), to deal in
|
106
|
+
the Software without restriction, including without limitation the rights to
|
107
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
108
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
109
|
+
subject to the following conditions:
|
110
|
+
|
111
|
+
The above copyright notice and this permission notice shall be included in all
|
112
|
+
copies or substantial portions of the Software.
|
113
|
+
|
114
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
115
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
116
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
117
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
118
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
119
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/when.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# --
|
3
|
+
# The MIT License (MIT)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2012 Gitorious AS
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
#++
|
25
|
+
|
26
|
+
module When
|
27
|
+
class Promise
|
28
|
+
def initialize(deferred); @deferred = deferred; end
|
29
|
+
def then(&block); @deferred.then(&block); end
|
30
|
+
def callback(&block); @deferred.callback(&block); end
|
31
|
+
def errback(&block); @deferred.errback(&block); end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Resolver
|
35
|
+
def initialize(deferred); @deferred = deferred; end
|
36
|
+
def resolve(*args); @deferred.resolve(*args); end
|
37
|
+
def reject(*args); @deferred.reject(*args); end
|
38
|
+
def resolved?; @deferred.resolved; end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Deferred
|
42
|
+
attr_reader :resolver, :promise
|
43
|
+
|
44
|
+
def initialize
|
45
|
+
@resolution = nil
|
46
|
+
@callbacks = { :resolved => [], :rejected => [] }
|
47
|
+
@resolver = Resolver.new(self)
|
48
|
+
@promise = Promise.new(self)
|
49
|
+
end
|
50
|
+
|
51
|
+
def resolve(*args); mark_resolved(:resolved, args); end
|
52
|
+
def reject(*args); mark_resolved(:rejected, args); end
|
53
|
+
def callback(&block); add_callback(:resolved, block); end
|
54
|
+
def then(&block); add_callback(:resolved, block); end
|
55
|
+
def errback(&block); add_callback(:rejected, block); end
|
56
|
+
def resolved?; !@resolution.nil?; end
|
57
|
+
|
58
|
+
private
|
59
|
+
def add_callback(type, block)
|
60
|
+
return notify_callbacks({ type => [block] }) if resolved?
|
61
|
+
@callbacks[type] << block
|
62
|
+
end
|
63
|
+
|
64
|
+
def mark_resolved(state, args)
|
65
|
+
raise AlreadyResolvedError.new("Already resolved") if resolved?
|
66
|
+
@resolution = [state, args]
|
67
|
+
notify_callbacks(@callbacks)
|
68
|
+
end
|
69
|
+
|
70
|
+
def notify_callbacks(callbacks)
|
71
|
+
blocks = callbacks[@resolution.first] || []
|
72
|
+
blocks.each { |cb| cb.call(*@resolution.last) }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.defer
|
77
|
+
deferred = Deferred.new
|
78
|
+
yield deferred if block_given?
|
79
|
+
deferred
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.resolve(val)
|
83
|
+
deferred = Deferred.new
|
84
|
+
deferred.resolve(val)
|
85
|
+
deferred.promise
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.reject(val)
|
89
|
+
deferred = Deferred.new
|
90
|
+
deferred.reject(val)
|
91
|
+
deferred.promise
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.all(promises)
|
95
|
+
raise(ArgumentError, "expected enumerable promises") if !promises.is_a?(Enumerable)
|
96
|
+
resolved = 0
|
97
|
+
results = []
|
98
|
+
d = Deferred.new
|
99
|
+
|
100
|
+
attempt_resolution = lambda do |err, res|
|
101
|
+
break if d.resolved?
|
102
|
+
if err.nil?
|
103
|
+
d.resolve(res) if promises.length == resolved
|
104
|
+
else
|
105
|
+
d.reject(err)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
wait_for_all(promises) do |err, result, index|
|
110
|
+
resolved += 1
|
111
|
+
results[index] = result
|
112
|
+
attempt_resolution.call(err, results)
|
113
|
+
end
|
114
|
+
|
115
|
+
attempt_resolution.call(nil, results) if promises.length == 0
|
116
|
+
d.promise
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
def self.wait_for_all(promises, &block)
|
121
|
+
promises.each_with_index do |p, i|
|
122
|
+
p.callback do |result|
|
123
|
+
block.call(nil, result, i)
|
124
|
+
end
|
125
|
+
p.errback { |e| block.call(e, nil, i) }
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
class AlreadyResolvedError < Exception; end
|
130
|
+
end
|
131
|
+
|
132
|
+
def When(val)
|
133
|
+
return val if val.respond_to?(:callback) && val.respond_to?(:errback)
|
134
|
+
When.resolve(val)
|
135
|
+
end
|
data/lib/when/version.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# --
|
3
|
+
# The MIT License (MIT)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2012 Gitorious AS
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
#++
|
25
|
+
|
26
|
+
module When
|
27
|
+
VERSION = "0.1.0"
|
28
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# --
|
3
|
+
# The MIT License (MIT)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2012 Gitorious AS
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
#++
|
25
|
+
require "bundler/setup"
|
26
|
+
require "minitest/autorun"
|
27
|
+
|
28
|
+
Bundler.require(:default, :test)
|
data/test/when_test.rb
ADDED
@@ -0,0 +1,249 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# --
|
3
|
+
# The MIT License (MIT)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2012 Gitorious AS
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
#++
|
25
|
+
require "test_helper"
|
26
|
+
require "when"
|
27
|
+
|
28
|
+
describe When do
|
29
|
+
describe "When" do
|
30
|
+
it "returns promise" do
|
31
|
+
promise = When(42)
|
32
|
+
assert promise.respond_to?(:callback)
|
33
|
+
assert promise.respond_to?(:errback)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "resolves promise with value" do
|
37
|
+
promise = When(42)
|
38
|
+
called_back = false
|
39
|
+
promise.callback do |value|
|
40
|
+
assert_equal 42, value
|
41
|
+
called_back = true
|
42
|
+
end
|
43
|
+
|
44
|
+
assert called_back
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "resolve" do
|
49
|
+
it "returns resolved promise" do
|
50
|
+
called_back = false
|
51
|
+
When.resolve(42).callback do |num|
|
52
|
+
assert_equal 42, num
|
53
|
+
called_back = true
|
54
|
+
end
|
55
|
+
|
56
|
+
assert called_back
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "reject" do
|
61
|
+
it "returns rejected promise" do
|
62
|
+
called_back = false
|
63
|
+
When.reject(42).errback do |num|
|
64
|
+
assert_equal 42, num
|
65
|
+
called_back = true
|
66
|
+
end
|
67
|
+
|
68
|
+
assert called_back
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "defer" do
|
73
|
+
it "creates deferred" do
|
74
|
+
deferred = When.defer
|
75
|
+
assert deferred.respond_to?(:callback)
|
76
|
+
assert deferred.respond_to?(:errback)
|
77
|
+
assert deferred.respond_to?(:resolve)
|
78
|
+
assert deferred.respond_to?(:reject)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "resolves promise through resolver" do
|
82
|
+
deferred = When.defer
|
83
|
+
called_back = false
|
84
|
+
deferred.promise.callback { called_back = true }
|
85
|
+
assert !called_back
|
86
|
+
deferred.resolver.resolve
|
87
|
+
assert called_back
|
88
|
+
end
|
89
|
+
|
90
|
+
it "aliases then to callback" do
|
91
|
+
deferred = When.defer
|
92
|
+
called_back = false
|
93
|
+
deferred.promise.then { called_back = true }
|
94
|
+
deferred.resolver.resolve
|
95
|
+
assert called_back
|
96
|
+
end
|
97
|
+
|
98
|
+
it "resolves promise through deferred" do
|
99
|
+
deferred = When.defer
|
100
|
+
called_back = false
|
101
|
+
deferred.promise.callback { called_back = true }
|
102
|
+
assert !called_back
|
103
|
+
deferred.resolve
|
104
|
+
assert called_back
|
105
|
+
end
|
106
|
+
|
107
|
+
it "resolves deferred through deferred" do
|
108
|
+
deferred = When.defer
|
109
|
+
called_back = false
|
110
|
+
deferred.callback { called_back = true }
|
111
|
+
assert !called_back
|
112
|
+
deferred.resolve
|
113
|
+
assert called_back
|
114
|
+
end
|
115
|
+
|
116
|
+
it "raises if already resolved" do
|
117
|
+
deferred = When.defer
|
118
|
+
deferred.resolve
|
119
|
+
assert_raises(When::AlreadyResolvedError) do
|
120
|
+
deferred.resolve
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it "rejects promise through reject" do
|
125
|
+
deferred = When.defer
|
126
|
+
called_back = false
|
127
|
+
deferred.promise.errback { called_back = true }
|
128
|
+
assert !called_back
|
129
|
+
deferred.resolver.reject
|
130
|
+
assert called_back
|
131
|
+
end
|
132
|
+
|
133
|
+
it "rejects promise through deferred" do
|
134
|
+
deferred = When.defer
|
135
|
+
called_back = false
|
136
|
+
deferred.promise.errback { called_back = true }
|
137
|
+
assert !called_back
|
138
|
+
deferred.reject
|
139
|
+
assert called_back
|
140
|
+
end
|
141
|
+
|
142
|
+
it "rejects deferred through deferred" do
|
143
|
+
deferred = When.defer
|
144
|
+
called_back = false
|
145
|
+
deferred.errback { called_back = true }
|
146
|
+
assert !called_back
|
147
|
+
deferred.reject
|
148
|
+
assert called_back
|
149
|
+
end
|
150
|
+
|
151
|
+
it "raises if already rejectd" do
|
152
|
+
deferred = When.defer
|
153
|
+
deferred.reject
|
154
|
+
assert_raises(When::AlreadyResolvedError) do
|
155
|
+
deferred.reject
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
describe ".all" do
|
161
|
+
it "returns deferrable" do
|
162
|
+
d = When.all([When(42)])
|
163
|
+
assert d.respond_to?(:callback)
|
164
|
+
assert d.respond_to?(:errback)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "resolves immediately if no promises" do
|
168
|
+
d = When.all([])
|
169
|
+
called_back = false
|
170
|
+
d.callback do |results|
|
171
|
+
assert_equal [], results
|
172
|
+
called_back = true
|
173
|
+
end
|
174
|
+
assert called_back
|
175
|
+
end
|
176
|
+
|
177
|
+
it "resolves when single deferrable resolves" do
|
178
|
+
deferred = When::Deferred.new
|
179
|
+
d = When.all([deferred.promise])
|
180
|
+
resolved = false
|
181
|
+
d.callback { |results| resolved = true }
|
182
|
+
|
183
|
+
assert !resolved
|
184
|
+
deferred.resolve(42)
|
185
|
+
assert resolved
|
186
|
+
end
|
187
|
+
|
188
|
+
it "resolves when all deferrables are resolved" do
|
189
|
+
deferreds = [When::Deferred.new, When::Deferred.new, When::Deferred.new]
|
190
|
+
d = When.all(deferreds.map(&:promise))
|
191
|
+
resolved = false
|
192
|
+
d.callback { |results| resolved = true }
|
193
|
+
|
194
|
+
assert !resolved
|
195
|
+
deferreds[0].resolve(42)
|
196
|
+
assert !resolved
|
197
|
+
deferreds[1].resolve(13)
|
198
|
+
assert !resolved
|
199
|
+
deferreds[2].resolve(3)
|
200
|
+
assert resolved
|
201
|
+
end
|
202
|
+
|
203
|
+
it "rejects when single deferrable rejects" do
|
204
|
+
deferred = When::Deferred.new
|
205
|
+
d = When.all([deferred.promise])
|
206
|
+
rejected = false
|
207
|
+
d.errback { |results| rejected = true }
|
208
|
+
|
209
|
+
assert !rejected
|
210
|
+
deferred.reject(StandardError.new)
|
211
|
+
assert rejected
|
212
|
+
end
|
213
|
+
|
214
|
+
it "rejects on first rejection" do
|
215
|
+
deferreds = [When::Deferred.new, When::Deferred.new, When::Deferred.new]
|
216
|
+
d = When.all(deferreds.map(&:promise))
|
217
|
+
rejected = false
|
218
|
+
d.errback { |results| rejected = true }
|
219
|
+
|
220
|
+
deferreds[0].resolve(42)
|
221
|
+
deferreds[2].reject(StandardError.new)
|
222
|
+
deferreds[1].resolve(13)
|
223
|
+
|
224
|
+
assert rejected
|
225
|
+
end
|
226
|
+
|
227
|
+
it "proxies resolution vaule in array" do
|
228
|
+
deferred = When::Deferred.new
|
229
|
+
d = When.all([deferred.promise])
|
230
|
+
results = nil
|
231
|
+
d.callback { |res| results = res }
|
232
|
+
|
233
|
+
deferred.resolve(42)
|
234
|
+
assert_equal [42], results
|
235
|
+
end
|
236
|
+
|
237
|
+
it "orders results like input" do
|
238
|
+
deferred1 = When::Deferred.new
|
239
|
+
deferred2 = When::Deferred.new
|
240
|
+
d = When.all([deferred1.promise, deferred2.promise])
|
241
|
+
results = nil
|
242
|
+
d.callback { |res| results = res }
|
243
|
+
|
244
|
+
deferred2.resolve(42)
|
245
|
+
deferred1.resolve(13)
|
246
|
+
assert_equal [13, 42], results
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
data/when.gemspec
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# --
|
3
|
+
# The MIT License (MIT)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2012 Gitorious AS
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
#++
|
25
|
+
dir = File.expand_path(File.dirname(__FILE__))
|
26
|
+
require File.join(dir, "lib/when/version")
|
27
|
+
|
28
|
+
Gem::Specification.new do |s|
|
29
|
+
s.name = "when"
|
30
|
+
s.version = When::VERSION
|
31
|
+
s.authors = ["Christian Johansen"]
|
32
|
+
s.email = ["christian@gitorious.org"]
|
33
|
+
s.homepage = "http://gitorious.org/gitorious/seuss"
|
34
|
+
s.summary = %q{A Ruby implementation of the wonderful when.js promise library}
|
35
|
+
s.description = %q{Currently very incomplete}
|
36
|
+
|
37
|
+
s.rubyforge_project = "when"
|
38
|
+
|
39
|
+
s.add_development_dependency "minitest", "~> 2.0"
|
40
|
+
s.add_development_dependency "rake", "~> 0.9"
|
41
|
+
|
42
|
+
s.files = `git ls-files`.split("\n")
|
43
|
+
s.test_files = `git ls-files -- {test}/*`.split("\n")
|
44
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
45
|
+
s.require_paths = ["lib"]
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: when
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Christian Johansen
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-10-08 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: minitest
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
version: "2.0"
|
34
|
+
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 25
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
- 9
|
48
|
+
version: "0.9"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description: Currently very incomplete
|
52
|
+
email:
|
53
|
+
- christian@gitorious.org
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- Gemfile
|
62
|
+
- Gemfile.lock
|
63
|
+
- Rakefile
|
64
|
+
- Readme.md
|
65
|
+
- lib/when.rb
|
66
|
+
- lib/when/version.rb
|
67
|
+
- test/test_helper.rb
|
68
|
+
- test/when_test.rb
|
69
|
+
- when.gemspec
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://gitorious.org/gitorious/seuss
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 3
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
97
|
+
requirements: []
|
98
|
+
|
99
|
+
rubyforge_project: when
|
100
|
+
rubygems_version: 1.4.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: A Ruby implementation of the wonderful when.js promise library
|
104
|
+
test_files: []
|
105
|
+
|