Class: FocusActor::AsyncProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/focus_actor/async_processor.rb

Overview

This is a Proxy for instance. It can response instance methods on instance.

Start a new thread, and then loop to call methods in mailbox.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, mailbox: Queue.new) ⇒ AsyncProcessor

mailbox should be thread safe



12
13
14
15
16
17
# File 'lib/focus_actor/async_processor.rb', line 12

def initialize(instance, mailbox: Queue.new)
  @instance = instance
  @mailbox = mailbox

  run!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

To call a method, push it to mailbox.



20
21
22
23
24
25
# File 'lib/focus_actor/async_processor.rb', line 20

def method_missing(method, *args, &block)
  return super unless instance.respond_to?(method)

  mailbox.push CellContext.new(method, args, block)
  nil
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



9
10
11
# File 'lib/focus_actor/async_processor.rb', line 9

def instance
  @instance
end

#mailboxObject (readonly)

Returns the value of attribute mailbox.



9
10
11
# File 'lib/focus_actor/async_processor.rb', line 9

def mailbox
  @mailbox
end

Instance Method Details

#respond_to_missing?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/focus_actor/async_processor.rb', line 27

def respond_to_missing?(method, include_all = false)
  instance.respond_to?(method) || super
end