Class: Eventbox::ExternalObject

Inherits:
WrappedObject show all
Defined in:
lib/eventbox/sanitizer.rb

Overview

Wrapper for objects created external or in the action scope of some Eventbox instance.

External objects can be called from event scope by #send_async.

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 Also:

Instance Attribute Summary

Attributes inherited from WrappedObject

#name

Instance Method Summary collapse

Methods inherited from WrappedObject

#inspect

Instance Method Details

#send_async(method, *args, &block) ⇒ Object

Invoke the external objects within the event scope.

It can be called within sync_call and 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 it within a async_call or Eventbox#async_proc, when the method or proc that brought the external object into the event scope, is a yield call that didn't return yet. In this case the method runs in the background on the thread that is waiting for the yield call to return.

If the call to the external object doesn't return immediately, it blocks the calling thread. If this is not desired, an action can be used instead, to invoke the method. 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_async :strip, ->(res){ p res }
  end
end
Sender.new(" a b c ")    # Output: "a b c"

326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/eventbox/sanitizer.rb', line 326

def send_async(method, *args, &block)
  if @target_event_loop&.event_scope?    # called in the event scope

    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, block, cbblock, @event_loop, @creation_answer_queue)
  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