ract 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.editorconfig +12 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +18 -0
- data/LICENSE +21 -0
- data/README.md +213 -0
- data/Rakefile +19 -0
- data/images/little-kernel-no-bg.png +0 -0
- data/images/ract-flow.png +0 -0
- data/images/ract-no-bg.png +0 -0
- data/lib/ract/async.rb +37 -0
- data/lib/ract/batch.rb +50 -0
- data/lib/ract/ract.rb +139 -0
- data/lib/ract/result.rb +37 -0
- data/lib/ract/settled.rb +43 -0
- data/lib/ract/single_methods.rb +34 -0
- data/lib/ract/version.rb +5 -0
- data/lib/ract.rb +74 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 48ebc9d97681afc2240e5001650e74d7250e76d20eb181955b402192377c8ca8
|
4
|
+
data.tar.gz: 8c121a49a43a61beff3a8f5f068997585951fbb4acd6e4432b89e8cad571ba0d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4b82bcf08d12aa28ffd6ac36f66572cb9b40a6a82e5f364d09eb37adbf0e79a7d7ac9f32a608dbd04dd62439487193ddd32ac3bca5d5a07f5d31de1306f8633b
|
7
|
+
data.tar.gz: b6cf72683bd4a7bdf7b7fd7756af98726627c56321ca65938663d47bd8469d8982031d97b2786f347ad4894f27eb255a43ef6766e2870a71cb1f4c42738e4284
|
data/.editorconfig
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.4.2
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
gem 'rake'
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem 'pry', '~> 0.15.2'
|
11
|
+
gem 'minitest', '~> 5.16'
|
12
|
+
gem 'minitest-reporters', '~> 1.6'
|
13
|
+
gem 'minitest-hooks'
|
14
|
+
gem 'rspec'
|
15
|
+
gem 'simplecov', '~> 0.22.0', require: false
|
16
|
+
gem 'simplecov-console', '~> 0.9.3', require: false
|
17
|
+
gem 'standard', '~> 1.49'
|
18
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Thadeu Esteves Jr
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
<h1 style="z-index: 999;">Ract</h1>
|
2
|
+
|
3
|
+
<center>
|
4
|
+
<img src="./images/ract-no-bg.png" alt="Ract Logo" width="65%" style="position: relative; margin-top: -50px; z-index: -1; mix-blend-mode: overlay;">
|
5
|
+
</center>
|
6
|
+
|
7
|
+
Ract is a lightweight Promise implementation for Ruby, providing a clean and intuitive way to handle asynchronous operations. It follows a similar pattern to JavaScript Promises, making it easy to write non-blocking code with proper error handling.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'ract'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
$ bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
or using add
|
24
|
+
|
25
|
+
```bash
|
26
|
+
$ bundle add ract
|
27
|
+
```
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
```bash
|
32
|
+
$ gem install ract
|
33
|
+
```
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'ract'
|
39
|
+
```
|
40
|
+
|
41
|
+
### Basic Usage
|
42
|
+
|
43
|
+
Create a new promise with a block that will be executed asynchronously:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# Create a promise that resolves with a value
|
47
|
+
promise = Ract.new { 42 } # or just Ract { 42 }
|
48
|
+
|
49
|
+
# Create a promise that might reject with an error
|
50
|
+
promise = Ract.new do
|
51
|
+
if success_condition
|
52
|
+
"Success result"
|
53
|
+
else
|
54
|
+
raise "Something went wrong"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
### Handling Promise Resolution
|
60
|
+
|
61
|
+
Use `.then` to handle successful resolution:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
promise = Ract.new { 42 }
|
65
|
+
|
66
|
+
promise.then do |value|
|
67
|
+
puts "The answer is #{value}"
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
### Error Handling
|
72
|
+
|
73
|
+
Use `.rescue` (or its alias `.catch`) to handle rejections:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
promise = Ract.new { raise "Something went wrong" }
|
77
|
+
|
78
|
+
promise
|
79
|
+
.then { |value| puts "This won't be called" }
|
80
|
+
.rescue { |error| puts "Error: #{error.message}" }
|
81
|
+
```
|
82
|
+
|
83
|
+
### Chaining Promises
|
84
|
+
|
85
|
+
Promises can be chained for sequential asynchronous operations:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
Ract.new { fetch_user(user_id) }
|
89
|
+
.then { |user| fetch_posts(user.id) }
|
90
|
+
.and_then { |posts| render_posts(posts) }
|
91
|
+
.rescue { |error| handle_error(error) }
|
92
|
+
.catch { |error| handle_error(error) }
|
93
|
+
```
|
94
|
+
|
95
|
+
### Waiting for Resolution
|
96
|
+
|
97
|
+
If you need to wait for a promise to resolve, use `.await`:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
promise = Ract.new { time_consuming_operation }
|
101
|
+
result = promise.await # Blocks until the promise resolves
|
102
|
+
```
|
103
|
+
|
104
|
+
### Creating Pre-resolved Promises
|
105
|
+
|
106
|
+
Create already resolved or rejected promises:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
# Already resolved promise
|
110
|
+
promise = Ract.resolve(42)
|
111
|
+
|
112
|
+
# Already rejected promise
|
113
|
+
promise = Ract.reject("Something went wrong")
|
114
|
+
```
|
115
|
+
|
116
|
+
### Combining Multiple Promises
|
117
|
+
|
118
|
+
Wait for multiple promises to complete:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
# Wait for all promises to resolve (will raise an error if any promise rejects)
|
122
|
+
promises = [Ract.new { task1 }, Ract.new { task2 }]
|
123
|
+
combined = Ract.all(promises)
|
124
|
+
|
125
|
+
# Wait for all promises to resolve (will not raise errors, returns results with status)
|
126
|
+
combined = Ract.all(promises, raise_on_error: false)
|
127
|
+
|
128
|
+
# Get results when all are settled (resolved or rejected)
|
129
|
+
results = Ract.all_settled(promises)
|
130
|
+
```
|
131
|
+
|
132
|
+
### Using block
|
133
|
+
|
134
|
+
You can use a block to receive result
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
tasks = [ ract { "mylogs" } ]
|
138
|
+
|
139
|
+
Ract.take(tasks) { p it }
|
140
|
+
# ["mylogs"]
|
141
|
+
```
|
142
|
+
|
143
|
+
This update properly explains that:
|
144
|
+
|
145
|
+
1. By default, `Ract.all` will raise an error if any of the promises are rejected
|
146
|
+
2. You can set `raise_on_error: false` to get all results regardless of whether they resolved or rejected
|
147
|
+
3. Alternatively, you can use `Ract.all_settled` to get results for all promises whether they resolved or rejected
|
148
|
+
|
149
|
+
### Immediate Execution
|
150
|
+
|
151
|
+
If you need to execute a block immediately with the current value, regardless of the promise state:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
promise = Ract.new { 42 }
|
155
|
+
promise.then { |value| puts "Current value: #{value}" }
|
156
|
+
```
|
157
|
+
|
158
|
+
### Async/Await Pattern
|
159
|
+
|
160
|
+
Ract supports an async/await pattern similar to JavaScript:
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
# Define an async method
|
164
|
+
async def fetch_data
|
165
|
+
user = fetch_user(user_id)
|
166
|
+
posts = fetch_posts(user.id)
|
167
|
+
comments = fetch_comments(posts.first.id)
|
168
|
+
|
169
|
+
return { user: user, posts: posts, comments: comments }
|
170
|
+
end
|
171
|
+
|
172
|
+
# Use the async method
|
173
|
+
result = fetch_data.await
|
174
|
+
```
|
175
|
+
|
176
|
+
### Examples using many callable promises
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
class Dynamo
|
180
|
+
async def self.get_item(table, key)
|
181
|
+
{ Item: {} }
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
tasks = [
|
186
|
+
Dynamo.get_item_async('users', 1),
|
187
|
+
Dynamo.get_item_async('posts', 1),
|
188
|
+
Dynamo.get_item_async('comments', 1)
|
189
|
+
]
|
190
|
+
|
191
|
+
result_all = Ract.all(tasks, raise_on_error: false)
|
192
|
+
result_taken = Ract.take(tasks, raise_on_error: false)
|
193
|
+
|
194
|
+
p result_all
|
195
|
+
# [{ Item: {} }, { Item: {} }, { Item: {} }]
|
196
|
+
|
197
|
+
p result_taken
|
198
|
+
# [{ Item: {} }, { Item: {} }, { Item: {} }]
|
199
|
+
```
|
200
|
+
|
201
|
+
## Development
|
202
|
+
|
203
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
204
|
+
|
205
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
206
|
+
|
207
|
+
## Contributing
|
208
|
+
|
209
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thadeu/ract.
|
210
|
+
|
211
|
+
## License
|
212
|
+
|
213
|
+
The gem is available as open source under the terms of the MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
require 'rubocop/rake_task'
|
9
|
+
RuboCop::RakeTask.new
|
10
|
+
|
11
|
+
require 'rake/testtask'
|
12
|
+
|
13
|
+
Rake::TestTask.new(:test) do |t|
|
14
|
+
t.libs << 'spec'
|
15
|
+
t.libs << 'lib'
|
16
|
+
t.test_files = FileList['spec/**/*_spec.rb']
|
17
|
+
end
|
18
|
+
|
19
|
+
task default: %i[spec test rubocop]
|
Binary file
|
Binary file
|
Binary file
|
data/lib/ract/async.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true # :rdoc
|
2
|
+
|
3
|
+
class Ract
|
4
|
+
module Async
|
5
|
+
def self.included(base)
|
6
|
+
base.include(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.extended(base)
|
10
|
+
base.extend(ClassMethods)
|
11
|
+
end
|
12
|
+
|
13
|
+
def Ract
|
14
|
+
Ract.new { yield }
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def ract
|
19
|
+
Ract { yield }
|
20
|
+
end
|
21
|
+
|
22
|
+
def async(method_name)
|
23
|
+
if method_defined?(method_name)
|
24
|
+
original_method = instance_method(method_name)
|
25
|
+
|
26
|
+
define_method("#{method_name}_async") do |*args, **kwargs, &block|
|
27
|
+
Ract { original_method.bind(self).call(*args, **kwargs, &block) }
|
28
|
+
end
|
29
|
+
elsif singleton_methods.include?(method_name)
|
30
|
+
define_singleton_method("#{method_name}_async") do |*args, **kwargs, &block|
|
31
|
+
Ract { send(method_name, *args, **kwargs, &block) }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/ract/batch.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true # :rdoc
|
2
|
+
|
3
|
+
class Ract
|
4
|
+
class Batch
|
5
|
+
attr_reader :promises, :remaining
|
6
|
+
|
7
|
+
def initialize(promises)
|
8
|
+
@promises = promises.keep_if { |promise| promise.is_a?(Ract) }
|
9
|
+
@queue = Thread::Queue.new
|
10
|
+
@remaining = promises.size
|
11
|
+
end
|
12
|
+
|
13
|
+
def run!(&block)
|
14
|
+
return if @promises.empty?
|
15
|
+
|
16
|
+
create_threads
|
17
|
+
process_results(&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_threads
|
21
|
+
@promises.each_with_index do |promise, index|
|
22
|
+
Thread.new do
|
23
|
+
try_block!(promise)
|
24
|
+
|
25
|
+
promise.then do |value|
|
26
|
+
@queue << [:success, index, value]
|
27
|
+
end&.rescue do |reason|
|
28
|
+
@queue << [:error, index, reason]
|
29
|
+
end
|
30
|
+
rescue StandardError => e
|
31
|
+
@queue << [:error, index, e]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def process_results(&block)
|
37
|
+
while @remaining.positive?
|
38
|
+
block.call(@queue.pop)
|
39
|
+
@remaining -= 1
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def try_block!(promise)
|
44
|
+
return unless promise.respond_to?(:execute_block)
|
45
|
+
return unless promise.state == Ract::PENDING
|
46
|
+
|
47
|
+
promise.execute_block
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/ract/ract.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Ract
|
4
|
+
class Error < StandardError; end
|
5
|
+
class Rejected < StandardError; end
|
6
|
+
|
7
|
+
PENDING = :pending
|
8
|
+
FULFILLED = :fulfilled
|
9
|
+
REJECTED = :rejected
|
10
|
+
|
11
|
+
attr_reader :state, :value, :reason
|
12
|
+
|
13
|
+
def initialize(value = nil, auto_execute: false, &block)
|
14
|
+
@state = PENDING
|
15
|
+
@value = value
|
16
|
+
@reason = nil
|
17
|
+
@mutex = Mutex.new
|
18
|
+
@condition = ConditionVariable.new
|
19
|
+
|
20
|
+
@callbacks = []
|
21
|
+
@error_callbacks = []
|
22
|
+
@block = block
|
23
|
+
|
24
|
+
if block_given? && auto_execute
|
25
|
+
execute_block
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def execute_block
|
30
|
+
return unless @block
|
31
|
+
|
32
|
+
begin
|
33
|
+
resolve(@block.call)
|
34
|
+
rescue StandardError => e
|
35
|
+
reject(e)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def fulfilled?
|
40
|
+
@state == FULFILLED
|
41
|
+
end
|
42
|
+
|
43
|
+
def rejected?
|
44
|
+
@state == REJECTED
|
45
|
+
end
|
46
|
+
|
47
|
+
def await
|
48
|
+
raise Rejected, @reason.to_s if @state == REJECTED
|
49
|
+
|
50
|
+
resolve
|
51
|
+
|
52
|
+
@value
|
53
|
+
end
|
54
|
+
|
55
|
+
def resolve(value = nil)
|
56
|
+
synchronize do
|
57
|
+
return if @state != PENDING
|
58
|
+
|
59
|
+
@state = FULFILLED
|
60
|
+
@value = value.nil? && @block ? @block.call : value
|
61
|
+
@condition&.broadcast
|
62
|
+
execute_callbacks
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def reject(reason = nil)
|
67
|
+
synchronize do
|
68
|
+
return if @state != PENDING
|
69
|
+
|
70
|
+
@state = REJECTED
|
71
|
+
@reason = reason
|
72
|
+
@condition&.broadcast
|
73
|
+
execute_error_callbacks
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def then(&block)
|
78
|
+
return self unless block_given?
|
79
|
+
|
80
|
+
if @state == PENDING
|
81
|
+
execute_block
|
82
|
+
@callbacks << block
|
83
|
+
end
|
84
|
+
|
85
|
+
return self if @state == REJECTED
|
86
|
+
|
87
|
+
begin
|
88
|
+
block.call(@value)
|
89
|
+
rescue StandardError => e
|
90
|
+
@state = REJECTED
|
91
|
+
@reason = e
|
92
|
+
reject(e)
|
93
|
+
end
|
94
|
+
|
95
|
+
self
|
96
|
+
end
|
97
|
+
alias and_then then
|
98
|
+
|
99
|
+
def rescue(&block)
|
100
|
+
return self unless block_given?
|
101
|
+
|
102
|
+
if @state == PENDING
|
103
|
+
execute_block
|
104
|
+
@error_callbacks << block
|
105
|
+
end
|
106
|
+
|
107
|
+
if @state == REJECTED
|
108
|
+
block.call(@reason)
|
109
|
+
end
|
110
|
+
|
111
|
+
self
|
112
|
+
end
|
113
|
+
alias catch rescue
|
114
|
+
|
115
|
+
class << self
|
116
|
+
include SingleMethods
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def execute_callbacks
|
122
|
+
callbacks = @callbacks.dup
|
123
|
+
@callbacks.clear
|
124
|
+
callbacks.each { |callback| callback.call(@value) }
|
125
|
+
end
|
126
|
+
|
127
|
+
def execute_error_callbacks
|
128
|
+
callbacks = @error_callbacks.dup
|
129
|
+
@error_callbacks.clear
|
130
|
+
callbacks.each { |callback| callback.call(@reason) }
|
131
|
+
end
|
132
|
+
|
133
|
+
def synchronize(&block)
|
134
|
+
@mutex.synchronize(&block)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
Object.include Ract::Async
|
139
|
+
Module.extend Ract::Async
|
data/lib/ract/result.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true # :rdoc
|
2
|
+
|
3
|
+
class Ract
|
4
|
+
class Result
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
attr_reader :value
|
8
|
+
|
9
|
+
def initialize(value)
|
10
|
+
@value = value
|
11
|
+
end
|
12
|
+
|
13
|
+
def deconstruct
|
14
|
+
[@value]
|
15
|
+
end
|
16
|
+
|
17
|
+
def deconstruct_keys(_keys)
|
18
|
+
{ value: @value, count: @value.size }
|
19
|
+
end
|
20
|
+
|
21
|
+
def and_then(&block)
|
22
|
+
return self unless block_given?
|
23
|
+
|
24
|
+
block.call(@value)
|
25
|
+
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def each(&block)
|
30
|
+
return enum_for(:each) unless block_given?
|
31
|
+
|
32
|
+
@value.each(&block)
|
33
|
+
|
34
|
+
self
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/ract/settled.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true # :rdoc
|
2
|
+
|
3
|
+
class Ract
|
4
|
+
class Settled
|
5
|
+
attr_reader :type, :raise_on_error
|
6
|
+
|
7
|
+
def initialize(promises, type: :all, raise_on_error: true)
|
8
|
+
@results = Array.new(promises.size)
|
9
|
+
@batch = Batch.new(promises)
|
10
|
+
@type = type
|
11
|
+
@raise_on_error = raise_on_error
|
12
|
+
end
|
13
|
+
|
14
|
+
def run!
|
15
|
+
return Result.new([]) if @batch.promises.empty?
|
16
|
+
|
17
|
+
@batch.run! do |type, index, value|
|
18
|
+
case type
|
19
|
+
when :success
|
20
|
+
@results[index] = success_row(value)
|
21
|
+
when :error
|
22
|
+
raise Rejected, value if @raise_on_error && @type == :all
|
23
|
+
|
24
|
+
@results[index] = rejected_row(value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Result.new(@results)
|
29
|
+
end
|
30
|
+
|
31
|
+
def success_row(value)
|
32
|
+
return value if @type == :all
|
33
|
+
|
34
|
+
{ status: Ract::FULFILLED, value: value }
|
35
|
+
end
|
36
|
+
|
37
|
+
def rejected_row(reason)
|
38
|
+
return reason if @type == :all
|
39
|
+
|
40
|
+
{ status: Ract::REJECTED, reason: reason }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true # :rdoc
|
2
|
+
|
3
|
+
class Ract
|
4
|
+
module SingleMethods
|
5
|
+
def resolve(value = nil, auto_execute: false)
|
6
|
+
new(auto_execute: true) { value }.await
|
7
|
+
end
|
8
|
+
|
9
|
+
def reject(reason = nil, auto_execute: false)
|
10
|
+
new(auto_execute: true) { raise Rejected, reason }.await
|
11
|
+
end
|
12
|
+
|
13
|
+
def all(promises, raise_on_error: true, auto_execute: false, &block)
|
14
|
+
return [] if promises.empty?
|
15
|
+
|
16
|
+
result = Settled.new(promises, raise_on_error: raise_on_error).run!
|
17
|
+
|
18
|
+
block.call(result.value) if block_given?
|
19
|
+
|
20
|
+
result.value
|
21
|
+
end
|
22
|
+
alias take all
|
23
|
+
|
24
|
+
def all_settled(promises, auto_execute: false, &block)
|
25
|
+
return [] if promises.empty?
|
26
|
+
|
27
|
+
result = Settled.new(promises, type: :all_settled).run!
|
28
|
+
|
29
|
+
block.call(result.value) if block_given?
|
30
|
+
|
31
|
+
result.value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/ract/version.rb
ADDED
data/lib/ract.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ract/version'
|
4
|
+
require 'ract/batch'
|
5
|
+
require 'ract/result'
|
6
|
+
require 'ract/settled'
|
7
|
+
require 'ract/async'
|
8
|
+
require 'ract/single_methods'
|
9
|
+
require 'ract/ract'
|
10
|
+
|
11
|
+
# A lightweight Promise implementation for Ruby
|
12
|
+
#
|
13
|
+
# == Usage
|
14
|
+
#
|
15
|
+
# You can use `Ract.new` or `Ract` to create a new promise
|
16
|
+
#
|
17
|
+
# Ract.new { 1 }
|
18
|
+
#
|
19
|
+
# Ract { 1 }
|
20
|
+
#
|
21
|
+
# You can use `async` to define an async method
|
22
|
+
# class ComplexTask
|
23
|
+
# async def self.execute(...)
|
24
|
+
# new(...).execute
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# def initialize(...)
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# def execute
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# async def call
|
34
|
+
# # your complex logic
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# If you use with color async method, you must be use with suffix _async, for example:
|
39
|
+
# This will create another method with _async suffix, to encapsulate the logic in a Thread
|
40
|
+
#
|
41
|
+
# ComplexTask.call_async
|
42
|
+
#
|
43
|
+
# ComplexTask.execute_async
|
44
|
+
#
|
45
|
+
# You can use `ract` or `go` to define a method that returns a promise
|
46
|
+
#
|
47
|
+
# def call
|
48
|
+
# ract { 1 }
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# def call
|
52
|
+
# go { 1 }
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# def call
|
56
|
+
# Ract { 1 }
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# == Multiple Racts
|
60
|
+
#
|
61
|
+
# You can use `Ract.take` to perform multiple promises together
|
62
|
+
#
|
63
|
+
# tasks = [ Ract { 1 }, Ract.new { 2 } ]
|
64
|
+
#
|
65
|
+
# Running your tasks using .take
|
66
|
+
#
|
67
|
+
# result = Ract.take(tasks)
|
68
|
+
#
|
69
|
+
# after that you will have an array with results
|
70
|
+
#
|
71
|
+
# p result -> [1, 2]
|
72
|
+
#
|
73
|
+
class Ract
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ract
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thadeu Esteves Jr
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-04-17 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: A simple and lightweight gem to wrapper Threads(Promises) like JavaScript
|
13
|
+
in Ruby, adding a color to Ruby Threads
|
14
|
+
email:
|
15
|
+
- tadeuu@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".editorconfig"
|
21
|
+
- ".rspec"
|
22
|
+
- ".ruby-version"
|
23
|
+
- CHANGELOG.md
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- images/little-kernel-no-bg.png
|
29
|
+
- images/ract-flow.png
|
30
|
+
- images/ract-no-bg.png
|
31
|
+
- lib/ract.rb
|
32
|
+
- lib/ract/async.rb
|
33
|
+
- lib/ract/batch.rb
|
34
|
+
- lib/ract/ract.rb
|
35
|
+
- lib/ract/result.rb
|
36
|
+
- lib/ract/settled.rb
|
37
|
+
- lib/ract/single_methods.rb
|
38
|
+
- lib/ract/version.rb
|
39
|
+
homepage: https://github.com/thadeu/ract-rb
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata:
|
43
|
+
homepage_uri: https://github.com/thadeu/ract-rb
|
44
|
+
source_code_uri: https://github.com/thadeu/ract-rb
|
45
|
+
changelog_uri: https://github.com/thadeu/ract-rb/blob/main/CHANGELOG.md
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.7.0
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubygems_version: 3.6.2
|
61
|
+
specification_version: 4
|
62
|
+
summary: A simple and lightweight gem to wrapper Threads(Promises) like JavaScript
|
63
|
+
in Ruby
|
64
|
+
test_files: []
|