Class: GraphQL::Dataloader::AsyncDataloader

Inherits:
GraphQL::Dataloader show all
Defined in:
lib/graphql/dataloader/async_dataloader.rb

Constant Summary

Constants inherited from GraphQL::Dataloader

NonblockingDataloader

Instance Method Summary collapse

Methods inherited from GraphQL::Dataloader

#append_job, #clear_cache, #get_fiber_variables, #initialize, #nonblocking?, #run_fiber, #run_isolated, #set_fiber_variables, #spawn_fiber, use, #with, with_dataloading

Constructor Details

This class inherits a constructor from GraphQL::Dataloader

Instance Method Details

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/graphql/dataloader/async_dataloader.rb', line 10

def run
  job_tasks = []
  next_job_tasks = []
  source_tasks = []
  next_source_tasks = []
  first_pass = true
  jobs_condition = Async::Condition.new
  sources_condition = Async::Condition.new
  Sync do |root_task|
    while first_pass || job_tasks.any?
      first_pass = false

      root_task.async do |jobs_task|
        while (task = job_tasks.shift || spawn_job_task(jobs_task, jobs_condition))
          if task.alive?
            next_job_tasks << task
          elsif task.failed?
            # re-raise a raised error -
            # this also covers errors from sources since
            # these jobs wait for sources as needed.
            task.wait
          end
        end
      end.wait
      job_tasks.concat(next_job_tasks)
      next_job_tasks.clear

      while source_tasks.any? || @source_cache.each_value.any? { |group_sources| group_sources.each_value.any?(&:pending?) }
        root_task.async do |sources_loop_task|
          while (task = source_tasks.shift || spawn_source_task(sources_loop_task, sources_condition))
            if task.alive?
              next_source_tasks << task
            end
          end
        end.wait
        sources_condition.signal
        source_tasks.concat(next_source_tasks)
        next_source_tasks.clear
      end
      jobs_condition.signal
    end
  end
rescue UncaughtThrowError => e
  throw e.tag, e.value
end

#yieldObject



5
6
7
8
# File 'lib/graphql/dataloader/async_dataloader.rb', line 5

def yield
  Thread.current[:graphql_dataloader_next_tick].wait
  nil
end