class Eventbox::ExternalObject
Wrapper for objects created external or in the action scope of some Eventbox
instance.
External objects can be called from event scope by {ExternalObject#send}.
External objects can also be passed to action or to external scope. In this case a {ExternalObject} is unwrapped back to the ordinary object.
@see ExternalProc
Public Class Methods
@private
Eventbox::WrappedObject::new
# File lib/eventbox/sanitizer.rb, line 305 def initialize(object, event_loop, target_event_loop, name=nil) super(object, event_loop, name) @target_event_loop = target_event_loop end
Public Instance Methods
Invoke the external objects within the event scope.
It can be called within {Eventbox::Boxable#sync_call sync_call} and {Eventbox::Boxable#yield_call yield_call} methods and from {Eventbox#sync_proc} and {Eventbox#yield_proc} closures. The method then runs in the background on the thread that called the event scope method in execution.
It's also possible to invoke the external object with an explicit {Eventbox::CallContext} instead of the implicit call context of a sync or yield call. The explicit {Eventbox::CallContext} must be given as the very first parameter and it is not passed to the object call. The object call is then done in the given context of an arbitrary event scope method or closure that didn't return yet. In this case the method runs in the background on the thread that is waiting for the call to return.
If the call to the external object doesn't return immediately, it blocks the calling thread or the thread of the {Eventbox::CallContext}. If this is not desired, an {Eventbox::Boxable#action action} can be used instead, to invoke the method of the object on a dedicated thread. However in any case calling the external object doesn't block the Eventbox
instance itself. It still keeps responsive to calls from other threads.
Optionally a proc can be provided as the last argument which acts as a completion callback. This proc is invoked, when the call has finished, with the result value as argument.
class Sender < Eventbox sync_call def init(€obj) # €-variables are passed as reference instead of copy # invoke the object given to Sender.new # and when completed, print the result of strip €obj.send :strip, ->(res){ p res } end end Sender.new(" a b c ") # Output: "a b c"
# File lib/eventbox/sanitizer.rb, line 336 def send(method, *args, **kwargs, &block) if @target_event_loop&.event_scope? # called in the event scope if CallContext === method call_context = method method = args.shift end if block && !(WrappedProc === block) raise InvalidAccess, "calling `#{method}' with block argument #{block.inspect} is not allowed - use async_proc, sync_proc, yield_proc or an external proc instead" end cbblock = args.pop if Proc === args.last @target_event_loop._external_object_call(@object, method, @name, args, kwargs, block, cbblock, @event_loop, call_context) else # called externally raise InvalidAccess, "external object #{self.inspect} #{"wrapped by #{name} " if name} can not be called in a different eventbox instance" end end