Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use Deferrable#fail instead of re-inventing fail_all

  • Loading branch information...
commit 58626c0def78b576125dc9755e5a6ed549b242d7 1 parent e95773c
@ConradIrwin ConradIrwin authored
View
20 lib/em-imap/connection.rb
@@ -75,6 +75,7 @@ def post_init
@listeners = Set.new
super
listen_for_greeting
+ listen_for_failure
end
# Listen for the first response from the server and succeed or fail
@@ -97,21 +98,24 @@ def listen_for_greeting
end
def hello_listener
- @hello_listener ||= Listener.new.errback{ |e| fail_all e }.bothback{ hello_listener.stop }
+ @hello_listener ||= Listener.new.errback{ |e| fail e }.bothback{ hello_listener.stop }
end
# Called when the connection is closed.
# TODO: Figure out how to send a useful error...
def unbind
- fail_all EOFError.new("Connection to IMAP server was unbound"), true
+ @unbound = true
+ fail EOFError.new("Connection to IMAP server was unbound")
end
- def fail_all(error, closed=false)
- # NOTE: Take a shallow clone of the listeners here so that we get guaranteed
- # behaviour. We want to fail any listeners that may be added by the errbacks
- # of other listeners.
- @listeners.clone.each{ |listener| listener.fail error } while @listeners.size > 0
- close_connection unless closed
+ def listen_for_failure
+ errback do |error|
+ # NOTE: Take a shallow clone of the listeners here so that we get guaranteed
+ # behaviour. We want to fail any listeners that may be added by the errbacks
+ # of other listeners.
+ @listeners.clone.each{ |listener| listener.fail error } while @listeners.size > 0
+ close_connection unless @unbound
+ end
end
def add_to_listener_pool(listener)
View
2  lib/em-imap/continuation_synchronisation.rb
@@ -63,7 +63,7 @@ def listen_for_continuation
if awaiting_continuation?
@awaiting_continuation.receive_event response
else
- fail_all Net::IMAP::ResponseError.new("Unexpected continuation response from server")
+ fail Net::IMAP::ResponseError.new("Unexpected continuation response from server")
end
end
end
View
5 lib/em-imap/response_parser.rb
@@ -39,15 +39,12 @@ def receive_data(data)
# Callback used by receive data.
def receive_response(response); end
- # Callback used if something goes wrong.
- def fail_all(error); end
-
private
def parse(line)
@parser.parse(line)
rescue Net::IMAP::ResponseParseError => e
- fail_all e
+ fail e
end
end
end
View
4 spec/client_spec.rb
@@ -175,7 +175,7 @@
@client.create("Encyclop\xc3\xa6dia").errback{ |e| a = true }
@client.create("Brittanica").errback{ |e| b = true }
@connection.should_receive(:close_connection).once
- @connection.fail_all EOFError.new("Testing error")
+ @connection.fail EOFError.new("Testing error")
a.should == true
b.should == true
end
@@ -187,7 +187,7 @@
a = true
end
end
- @connection.fail_all EOFError.new("Testing error")
+ @connection.fail EOFError.new("Testing error")
a.should == true
end
View
2  spec/continuation_synchronisation_spec.rb
@@ -88,7 +88,7 @@
end
it "should fail the connection when an unexpected continuation response is received" do
- @connection.should_receive(:fail_all).with(an_instance_of(Net::IMAP::ResponseError))
+ @connection.should_receive(:fail).with(an_instance_of(Net::IMAP::ResponseError))
@connection.receive_response Net::IMAP::ContinuationRequest.new("hehe")
end
end
View
2  spec/response_parser_spec.rb
@@ -48,7 +48,7 @@
end
it "should fail the connection when invalid data is received" do
- @response_parser.should_receive(:fail_all).with(an_instance_of(Net::IMAP::ResponseParseError))
+ @response_parser.should_receive(:fail).with(an_instance_of(Net::IMAP::ResponseParseError))
@response_parser.receive_data "lol ??\r\n"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.