promise_em 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ab3dd65a964c6537b0f2009496466c035b60076eebb9efe3496171a0e842f84d
4
+ data.tar.gz: 1d3f23cec2ce2388ae595ccc3decc7747af6b2db95369d95e9d1f05e7ab4feb2
5
+ SHA512:
6
+ metadata.gz: a8d0b2e7abc8db0c1f5d379701c2a8deb7c43784a63c37977b7585eeeaa7a2827aebac304fce2b65830815f96474cd07ade7e5f067171e930446e81847d481ba
7
+ data.tar.gz: a1db71f2a6c5bd9ed7d5ce10ee9b10f17e992bcb106e038f299bc0471078e923578640158709b73dbb9f0bf7491b47d4ea7b43f7719b23007a87c8b18323f324
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.3.0
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 1.17.3
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ Style/StringLiterals:
2
+ Enabled: true
3
+ EnforcedStyle: double_quotes
4
+
5
+ Style/StringLiteralsInInterpolation:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Layout/LineLength:
10
+ Max: 120
11
+
12
+ Gemspec/RequiredRubyVersion:
13
+ Enabled: false
14
+
15
+ Style/MultilineBlockChain:
16
+ Enabled: false
17
+
18
+ Metrics/MethodLength:
19
+ Max: 25
20
+
21
+ Metrics/BlockLength:
22
+ Exclude:
23
+ - 'Rakefile'
24
+ - '**/*.rake'
25
+ - 'spec/**/*.rb'
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ em_promise
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.3.0
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in promise_em.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ group :development do
11
+ gem "rspec", "~> 3.0"
12
+ gem "rubocop", "~> 0.80"
13
+ end
14
+
15
+ gem "eventmachine"
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ promise_em (0.1.1)
5
+ eventmachine (>= 1.0.0.beta.4)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ diff-lcs (1.4.4)
12
+ eventmachine (1.2.7)
13
+ jaro_winkler (1.5.4)
14
+ parallel (1.19.2)
15
+ parser (3.0.3.2)
16
+ ast (~> 2.4.1)
17
+ rainbow (3.0.0)
18
+ rake (13.0.6)
19
+ rexml (3.2.5)
20
+ rspec (3.10.0)
21
+ rspec-core (~> 3.10.0)
22
+ rspec-expectations (~> 3.10.0)
23
+ rspec-mocks (~> 3.10.0)
24
+ rspec-core (3.10.1)
25
+ rspec-support (~> 3.10.0)
26
+ rspec-expectations (3.10.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.10.0)
29
+ rspec-mocks (3.10.2)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.10.0)
32
+ rspec-support (3.10.3)
33
+ rubocop (0.81.0)
34
+ jaro_winkler (~> 1.5.1)
35
+ parallel (~> 1.10)
36
+ parser (>= 2.7.0.1)
37
+ rainbow (>= 2.2.2, < 4.0)
38
+ rexml
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 1.4.0, < 2.0)
41
+ ruby-progressbar (1.11.0)
42
+ unicode-display_width (1.8.0)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ eventmachine
49
+ promise_em!
50
+ rake (~> 13.0)
51
+ rspec (~> 3.0)
52
+ rubocop (~> 0.80)
53
+
54
+ BUNDLED WITH
55
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 user1622
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,85 @@
1
+ # EmPromise
2
+ [![Gem Version](https://badge.fury.io/rb/promise_em.svg)](https://badge.fury.io/rb/promise_em)
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'promise_em'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install promise_em
18
+
19
+ ## Usage
20
+
21
+ Simple example with one deferrable
22
+
23
+ ```ruby
24
+ EM.run do
25
+ PromiseEm::Promise.new do |resolve, _reject|
26
+ puts 'new promise'
27
+ EM::Timer.new(0.1) { resolve.call('hello') }
28
+ end.then do |arg|
29
+ puts arg
30
+ end.catch do |*error|
31
+ puts error
32
+ end
33
+
34
+ EM::Timer.new(0.5) { EM.stop }
35
+ end
36
+
37
+ # new promise
38
+ # hello
39
+ ```
40
+
41
+ Example with few deferrable
42
+
43
+ ```ruby
44
+ EM.run do
45
+ PromiseEm::Promise.new do |resolve, _reject|
46
+ puts 'new promise'
47
+ EM::Timer.new(0.1) { resolve.call('hello') }
48
+ end.then do |arg|
49
+ defer = EM::DefaultDeferrable.new
50
+ puts arg
51
+ EM::Timer.new(0.1) { defer.succeed('hello2') }
52
+ defer
53
+ end.then do |arg|
54
+ puts arg
55
+ raise "error with arg #{arg}"
56
+ end.catch do |*error|
57
+ puts error
58
+ end
59
+
60
+ EM::Timer.new(0.5) { EM.stop }
61
+ end
62
+
63
+ # new promise
64
+ # hello
65
+ # hello2
66
+ # error with arg hello2
67
+ ```
68
+
69
+ ## Development
70
+
71
+ Run `rake` to run the tests and rubocop. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
72
+
73
+ 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).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/user1622/em_promise.
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
82
+
83
+ ## Code of Conduct
84
+
85
+ Everyone interacting in the EmPromise project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/em_promise/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "promise_em"
6
+
7
+ require "irb"
8
+ IRB.start(__FILE__)
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PromiseEm
4
+ # JavaScript like promise for EventMachine library
5
+ class Promise < ::EventMachine::DefaultDeferrable
6
+ def initialize
7
+ @then_callbacks = []
8
+ @catch_callbacks = []
9
+ context = self
10
+ @resolve = create_resolve_proc(context)
11
+ @reject = create_reject_proc(context)
12
+ ::EventMachine.next_tick { yield(@resolve, @reject) } if block_given?
13
+ end
14
+
15
+ def then(&block)
16
+ @then_callbacks << block
17
+ self
18
+ end
19
+
20
+ def catch(&block)
21
+ @catch_callbacks << block
22
+ self
23
+ end
24
+
25
+ private
26
+
27
+ def create_resolve_proc(context)
28
+ proc do |*args|
29
+ then_callback = @then_callbacks.shift
30
+ unless then_callback
31
+ context.succeed(*args)
32
+ next
33
+ end
34
+
35
+ begin
36
+ result = then_callback.call(*args)
37
+ if result.is_a?(::EventMachine::Deferrable)
38
+ result.callback { |*callback_args| @resolve.call(*callback_args) }.errback { |*error| @reject.call(*error) }
39
+ else
40
+ ::EventMachine.next_tick { @resolve.call(*result) }
41
+ end
42
+ rescue StandardError => e
43
+ ::EventMachine.next_tick { @reject.call(*e) }
44
+ end
45
+ end
46
+ end
47
+
48
+ def create_reject_proc(context)
49
+ proc do |*args|
50
+ reject_callback = @catch_callbacks.shift
51
+ unless reject_callback
52
+ context.fail(*args)
53
+ next
54
+ end
55
+
56
+ result = reject_callback.call(*args)
57
+ if result.is_a?(::EventMachine::Deferrable)
58
+ result.callback { |*cb_args| @reject.call(*cb_args) }.errback { |*cb_args| @reject.call(*cb_args) }
59
+ else
60
+ ::EventMachine.next_tick { @reject.call(result) }
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PromiseEm
4
+ VERSION = "0.1.1"
5
+ end
data/lib/promise_em.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "promise_em/version"
4
+ require "bundler/setup"
5
+
6
+ Bundler.require
7
+
8
+ # JavaScript like promise for EventMachine library
9
+ module PromiseEm
10
+ class Error < StandardError; end
11
+
12
+ autoload(:Promise, "promise_em/promise")
13
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/promise_em/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "promise_em"
7
+ spec.version = PromiseEm::VERSION
8
+ spec.authors = ["user1622"]
9
+ spec.email = ["pastushuk.denis@gmail.com"]
10
+
11
+ spec.summary = "JavaScript like promise for EventMachine"
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/user1622/promise_em"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/user1622/promise_em"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "eventmachine", ">= 1.0.0.beta.4"
30
+ spec.add_development_dependency "rspec"
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: promise_em
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - user1622
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eventmachine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0.beta.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0.beta.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: JavaScript like promise for EventMachine
42
+ email:
43
+ - pastushuk.denis@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".github/workflows/main.yml"
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".rubocop.yml"
52
+ - ".ruby-gemset"
53
+ - ".ruby-version"
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - bin/console
60
+ - lib/promise_em.rb
61
+ - lib/promise_em/promise.rb
62
+ - lib/promise_em/version.rb
63
+ - promise_em.gemspec
64
+ homepage: https://github.com/user1622/promise_em
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ homepage_uri: https://github.com/user1622/promise_em
69
+ source_code_uri: https://github.com/user1622/promise_em
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.3.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.0.9
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: JavaScript like promise for EventMachine
89
+ test_files: []