class Realm::Dispatcher

Attributes

runtime[R]

Public Class Methods

new(runtime) click to toggle source
# File lib/realm/dispatcher.rb, line 8
def initialize(runtime)
  @runtime = runtime
  @threads = []
end

Public Instance Methods

query(identifier, params = {}) click to toggle source
# File lib/realm/dispatcher.rb, line 13
def query(identifier, params = {})
  callable, action = get_callable(QueryHandler, identifier)
  callable, action = get_repo_adapter(identifier) unless callable
  raise QueryHandlerMissing, identifier unless callable

  dispatch(callable, action, params)
end
run(identifier, params = {}) click to toggle source
# File lib/realm/dispatcher.rb, line 21
def run(identifier, params = {})
  callable, action = get_callable(CommandHandler, identifier)
  raise CommandHandlerMissing, identifier unless callable

  dispatch(callable, action, params)
end
run_as_job(identifier, params = {}) { |result| ... } click to toggle source
# File lib/realm/dispatcher.rb, line 28
def run_as_job(identifier, params = {})
  callable, action = get_callable(CommandHandler, identifier)
  raise CommandHandlerMissing, identifier unless callable

  @threads.delete_if(&:stop?)
  @threads << Thread.new do # TODO: back by SQS
    result = dispatch(callable, action, params)
    yield result if block_given?
  end
end
wait_for_jobs() click to toggle source

Blocks until all jobs are finished. Useful mainly in tests.

# File lib/realm/dispatcher.rb, line 40
def wait_for_jobs
  @threads.each(&:join)
end

Private Instance Methods

dispatch(callable, action, params) click to toggle source
# File lib/realm/dispatcher.rb, line 48
def dispatch(callable, action, params)
  arguments = { action: action, params: params, runtime: runtime }.compact
  callable.(**arguments)
end
get_callable(type, identifier) click to toggle source
# File lib/realm/dispatcher.rb, line 53
def get_callable(type, identifier)
  return [identifier, nil] if identifier.respond_to?(:call)

  domain_resolver.get_handler_with_action(type, identifier)
end
get_repo_adapter(identifier) click to toggle source
# File lib/realm/dispatcher.rb, line 59
def get_repo_adapter(identifier)
  parts = identifier.to_s.split('.')
  return [nil, nil] unless parts.size == 2 && runtime&.context&.key?("#{parts[0]}_repo")

  [Persistence::RepositoryQueryHandlerAdapter.new(runtime.context["#{parts[0]}_repo"]), parts[1].to_sym]
end