rpromise 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b10d619743dc3c21bbeadabdabfe44644e9f1d1
4
- data.tar.gz: ead44b4ddf6558d9cc86ae1845319c890803b0ad
3
+ metadata.gz: 1262691501de1334a31235ac093581f219d4cc44
4
+ data.tar.gz: d9557239fef9c4d00dd102b48b9f4cc0cb02de26
5
5
  SHA512:
6
- metadata.gz: 37f7b63b48e48bd8fb678d3822341055a526f330eb615a2055964efce61461c6b41ccb3ed0f8e0d9e7906a878d93a05b0df6feb1676a658a0831b8062859801a
7
- data.tar.gz: a64adabdb624c26505386d2241f7f2b89f86e7e6f06130c0587adb85b9e385f2a15a609d2f7b87a1b34f212ae971abd94b7ad43a437eb8157b3be076c1b7382f
6
+ metadata.gz: 3b1e40b5e717eaaf25c8baf8f22c627f75d24e8651a6192a892ac8e39378101e8db0e77c4fc3bc1b37164aec58512f3c03cfa4e455fe0311553fb5150b11241c
7
+ data.tar.gz: 612ef5ff492f18d43591b95aaaddd81c9b1e5bf9e60546dd5659da0cb3040adab5b9da1a4fde069cff66cbe6230e6c32e456fc1b2ff75d359d04af327d3b7e12
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Rpromise
2
2
 
3
+ [![Build Status](https://travis-ci.org/samuelmolinari/rpromise.svg?branch=master)](https://travis-ci.org/samuelmolinari/rpromise)
4
+
3
5
  ## Installation
4
6
 
5
7
  Add this line to your application's Gemfile:
@@ -21,7 +23,7 @@ Or install it yourself as:
21
23
  class Task
22
24
 
23
25
  def async
24
- return ::Rpromise::Promise.new do |resolve, reject|
26
+ return ::Rpromise.new do |resolve, reject|
25
27
  Thread.new do
26
28
  sleep(1)
27
29
  value = Random.rand * 10
@@ -41,7 +43,7 @@ on_resolve = lambda do |value|
41
43
 
42
44
  puts value
43
45
 
44
- return ::Rpromise::Promise.new do |resolve, reject|
46
+ return ::Rpromise.new do |resolve, reject|
45
47
  Thread.new do
46
48
  sleep(1)
47
49
  # Do an async task
@@ -1,3 +1,3 @@
1
- module Rpromise
2
- VERSION = "0.0.4"
1
+ class Rpromise
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/rpromise.rb CHANGED
@@ -1,5 +1,98 @@
1
- require "rpromise/version"
2
- require "rpromise/promise"
1
+ require 'rpromise/version'
3
2
 
4
- module Rpromise
3
+ class Rpromise
4
+ module State
5
+ PENDING = :pending
6
+ RESOLVED = :resolved
7
+ REJECTED = :rejected
8
+ end
9
+
10
+ Handler = Struct.new(:on_resolved, :on_rejected, :resolve, :reject)
11
+
12
+ attr_reader :state
13
+
14
+ def initialize()
15
+ @state = State::PENDING
16
+ @defered = nil
17
+ yield(method(:resolve!), method(:reject!),self)
18
+ rescue Exception => e
19
+ reject!(e)
20
+ end
21
+
22
+ def then(on_resolved = nil, on_rejected = nil)
23
+ raise ArgumentError unless is_valid_block?(on_resolved)
24
+ raise ArgumentError unless is_valid_block?(on_rejected)
25
+ return self if on_resolved.nil? && on_rejected.nil?
26
+ return ::Rpromise.new do |resolve, reject, promise|
27
+ handler = Handler.new(on_resolved, on_rejected, resolve, reject)
28
+ self.handle(handler)
29
+ end
30
+ end
31
+
32
+ def pending?
33
+ return @state == State::PENDING
34
+ end
35
+
36
+ def resolved?
37
+ return @state == State::RESOLVED
38
+ end
39
+
40
+ def rejected?
41
+ return @state == State::REJECTED
42
+ end
43
+
44
+ def resolve!(value = nil)
45
+ if value.is_a?(::Rpromise)
46
+ value.then(method(:resolve!), method(:reject!))
47
+ return
48
+ end
49
+
50
+ @state = State::RESOLVED
51
+ @value = value
52
+
53
+ unless @defered.nil?
54
+ handle(@defered)
55
+ end
56
+
57
+ rescue
58
+ reject!(nil)
59
+ end
60
+
61
+ def reject!(value = nil)
62
+ @state = State::REJECTED
63
+ @value = value
64
+
65
+ unless @defered.nil?
66
+ handle(@defered)
67
+ end
68
+ end
69
+
70
+ protected
71
+
72
+ def is_valid_block?(arg)
73
+ return arg.nil? || arg.is_a?(Proc) || arg.is_a?(Method)
74
+ end
75
+
76
+ def handle(handler)
77
+ if pending?
78
+ @defered = handler
79
+ return nil
80
+ end
81
+
82
+ callback = nil
83
+
84
+ if resolved?
85
+ callback = handler.on_resolved
86
+ else
87
+ callback = handler.on_rejected
88
+ end
89
+
90
+ unless callback.nil?
91
+ output = nil
92
+ output = callback.call(@value)
93
+ handler.resolve.call(output) unless handler.resolve.nil?
94
+ end
95
+ rescue
96
+ handler.reject.call(nil) unless handler.reject.nil?
97
+ end
5
98
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ::Rpromise::Promise do
3
+ describe ::Rpromise do
4
4
 
5
5
  let(:n) { 0 }
6
6
  subject(:promise) do
@@ -13,7 +13,7 @@ describe ::Rpromise::Promise do
13
13
  describe '#initialize' do
14
14
  it 'uses the resolve! method as callback' do
15
15
  t = nil
16
- p = ::Rpromise::Promise.new do |resolve, reject|
16
+ p = described_class.new do |resolve, reject|
17
17
  t = Thread.new do
18
18
  resolve.call('hi')
19
19
  end
@@ -24,7 +24,7 @@ describe ::Rpromise::Promise do
24
24
 
25
25
  it 'uses the reject! method as callback' do
26
26
  t = nil
27
- p = ::Rpromise::Promise.new do |resolve, reject|
27
+ p = described_class.new do |resolve, reject|
28
28
  t = Thread.new do
29
29
  reject.call('hi')
30
30
  end
@@ -37,7 +37,7 @@ describe ::Rpromise::Promise do
37
37
  describe '#then' do
38
38
 
39
39
  it 'returns a new promise' do
40
- expect(promise.then).to be_kind_of ::Rpromise::Promise
40
+ expect(promise.then).to be_kind_of described_class
41
41
  end
42
42
 
43
43
  it 'takes an optional block as first argument' do
@@ -61,7 +61,7 @@ describe ::Rpromise::Promise do
61
61
  context 'with async task' do
62
62
  before(:each) do
63
63
  @thread = nil
64
- @promise = ::Rpromise::Promise.new do |resolve, reject|
64
+ @promise = described_class.new do |resolve, reject|
65
65
  @thread = Thread.new do
66
66
  sleep(0.5)
67
67
  resolve.call(value)
@@ -106,7 +106,7 @@ describe ::Rpromise::Promise do
106
106
  lambda_value = nil
107
107
  lock = true
108
108
  @promise.then(lambda do |v|
109
- return ::Rpromise::Promise.new do |resolve, reject|
109
+ return described_class.new do |resolve, reject|
110
110
  thread2 = Thread.new do
111
111
  sleep(0)
112
112
  resolve.call('Hello world!')
@@ -134,7 +134,7 @@ describe ::Rpromise::Promise do
134
134
 
135
135
  context 'with non-async task' do
136
136
  let(:promise) do
137
- ::Rpromise::Promise.new do |resolve, reject|
137
+ described_class.new do |resolve, reject|
138
138
  resolve.call(value)
139
139
  end
140
140
  end
@@ -187,7 +187,7 @@ describe ::Rpromise::Promise do
187
187
  context 'with non-async task' do
188
188
  let(:error) { Random.rand }
189
189
  let(:promise) do
190
- ::Rpromise::Promise.new do |resolve, reject|
190
+ described_class.new do |resolve, reject|
191
191
  reject.call(error)
192
192
  end
193
193
  end
@@ -200,7 +200,7 @@ describe ::Rpromise::Promise do
200
200
  end
201
201
  it 'handles raised exceptions within the promise' do
202
202
  lambda_error = nil
203
- promise = ::Rpromise::Promise.new do |resolve, reject|
203
+ promise = described_class.new do |resolve, reject|
204
204
  raise 'Oops'
205
205
  end
206
206
  promise.then(nil, lambda do |e|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rpromise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Molinari
@@ -96,10 +96,9 @@ files:
96
96
  - README.md
97
97
  - Rakefile
98
98
  - lib/rpromise.rb
99
- - lib/rpromise/promise.rb
100
99
  - lib/rpromise/version.rb
101
100
  - rpromise.gemspec
102
- - spec/lib/rpromise/promise_spec.rb
101
+ - spec/lib/rpromise_spec.rb
103
102
  - spec/spec_helper.rb
104
103
  homepage: ''
105
104
  licenses:
@@ -126,5 +125,5 @@ signing_key:
126
125
  specification_version: 4
127
126
  summary: Handle async method easily
128
127
  test_files:
129
- - spec/lib/rpromise/promise_spec.rb
128
+ - spec/lib/rpromise_spec.rb
130
129
  - spec/spec_helper.rb
@@ -1,101 +0,0 @@
1
- require 'rpromise'
2
- require 'pry'
3
-
4
- class ::Rpromise::Promise
5
-
6
- module State
7
- PENDING = :pending
8
- RESOLVED = :resolved
9
- REJECTED = :rejected
10
- end
11
-
12
- Handler = Struct.new(:on_resolved, :on_rejected, :resolve, :reject)
13
-
14
- attr_reader :state
15
-
16
- def initialize()
17
- @state = State::PENDING
18
- @defered = nil
19
- yield(method(:resolve!), method(:reject!),self)
20
- rescue Exception => e
21
- reject!(e)
22
- end
23
-
24
- def then(on_resolved = nil, on_rejected = nil)
25
- raise ArgumentError unless is_valid_block?(on_resolved)
26
- raise ArgumentError unless is_valid_block?(on_rejected)
27
- return self if on_resolved.nil? && on_rejected.nil?
28
- return ::Rpromise::Promise.new do |resolve, reject, promise|
29
- handler = Handler.new(on_resolved, on_rejected, resolve, reject)
30
- self.handle(handler)
31
- end
32
- end
33
-
34
- def pending?
35
- return @state == State::PENDING
36
- end
37
-
38
- def resolved?
39
- return @state == State::RESOLVED
40
- end
41
-
42
- def rejected?
43
- return @state == State::REJECTED
44
- end
45
-
46
- def resolve!(value = nil)
47
- if value.is_a?(::Rpromise::Promise)
48
- value.then(method(:resolve!), method(:reject!))
49
- return
50
- end
51
-
52
- @state = State::RESOLVED
53
- @value = value
54
-
55
- unless @defered.nil?
56
- handle(@defered)
57
- end
58
-
59
- rescue
60
- reject!(nil)
61
- end
62
-
63
- def reject!(value = nil)
64
- @state = State::REJECTED
65
- @value = value
66
-
67
- unless @defered.nil?
68
- handle(@defered)
69
- end
70
- end
71
-
72
- protected
73
-
74
- def is_valid_block?(arg)
75
- return arg.nil? || arg.is_a?(Proc) || arg.is_a?(Method)
76
- end
77
-
78
- def handle(handler)
79
- if pending?
80
- @defered = handler
81
- return nil
82
- end
83
-
84
- callback = nil
85
-
86
- if resolved?
87
- callback = handler.on_resolved
88
- else
89
- callback = handler.on_rejected
90
- end
91
-
92
- unless callback.nil?
93
- output = nil
94
- output = callback.call(@value)
95
- handler.resolve.call(output) unless handler.resolve.nil?
96
- end
97
- rescue
98
- handler.reject.call(nil) unless handler.reject.nil?
99
- end
100
-
101
- end