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.

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(method, *args, **kwargs, &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 the external object with an explicit CallContext instead of the implicit call context of a sync or yield call. The explicit 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 CallContext. If this is not desired, an 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"


336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# 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