Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add in default callbacks, currently in RPCDeferred. May want to promo…

…te to Deferred.
  • Loading branch information...
commit 0c7ccd2cd3ccd96642d35438d30ddee966f1808a 1 parent 1be4136
Cyrus Hall authored
Showing with 56 additions and 4 deletions.
  1. +10 −3 lib/gosim/defer.rb
  2. +46 −1 lib/gosim/network.rb
View
13 lib/gosim/defer.rb
@@ -80,9 +80,15 @@ def callback(result)
def errback(result)
raise NotFailureError unless is_failure?(result)
- @called = true
- @result = result
- run_callbacks
+ callback(result)
+ end
+
+ def has_callbacks?
+ @callbacks.any? { | cb | cb != PASSTHRU }
+ end
+
+ def has_errbacks?
+ @errbacks.any? { | eb | eb != PASSTHRU }
end
# Execute the callbacks in sequence, passing the result of each callback to
@@ -92,6 +98,7 @@ def errback(result)
def run_callbacks
return if @paused > 0
+ # Transverse chains
while @callbacks.any?
begin
cb = @callbacks.shift
View
47 lib/gosim/network.rb
@@ -45,6 +45,36 @@ def initialize(uid = nil)
super()
end
+ def default_callback(callback = nil, &block)
+ callback = callback || block
+
+ if callback
+ raise NotCallableError unless is_callable?(callback)
+ @default_cb = callback
+ end
+ end
+
+ def default_errback(errback = nil, &block)
+ errback = errback || block
+
+ if errback
+ raise NoterrableError unless is_callable?(errback)
+ @default_eb = errback
+ end
+ end
+
+ def run_callbacks
+ # Check for defaults. Call the appropriate one only if no calls have
+ # been provided
+ if is_failure?(@result)
+ @default_eb.call(@result) if !has_errbacks? && @default_eb
+ elsif !has_callbacks? && @default_cb
+ @default_cb.call(@result)
+ end
+
+ super()
+ end
+
def no_return
Topology.instance.remove_deferred(@uid)
end
@@ -165,12 +195,27 @@ def initialize(local_node, remote_addr)
else
@addr = nil
end
+
+ @default_cb = nil
+ @default_eb = nil
end
def method_missing(method, *args)
raise RPCInvalidMethodError.new("#{method} not available on target node!") unless @remote_node.respond_to?(method)
- @topo.rpc_request(@local_node.addr, @remote_node.addr, method, args)
+ deferred = @topo.rpc_request(@local_node.addr, @remote_node.addr, method, args)
+ deferred.default_callback(@default_cb) if @default_cb
+ deferred.default_errback(@default_eb) if @default_eb
+
+ return deferred
+ end
+
+ def add_default_callback(callback = nil, &block)
+ @default_cb = callback || block
+ end
+
+ def add_default_errback(errback = nil, &block)
+ @default_eb = errback || block
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.