Class: Pipeloader::Batch::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/pipeloader/batch/context.rb

Overview

A sibling group: the records loaded together by one query. It lives ON the records (each carries a reference, see Model#_pipeloader_batch_context), not in any thread/fiber/request storage. That is what makes batching correct under GraphQL::Dataloader fibers, fiber-per-request servers, and plain threads alike: the context travels with the data instead of being looked up ambiently.

Constant Summary collapse

PRELOADING_KEY =

Re-entrancy guard for the singular interceptor: while a Preloader fills an association, its own load_target calls must not re-enter. Fiber-local, since a preload runs synchronously in one fiber and sibling fibers preload on their own.

:pipeloader_batch_preloading

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



29
30
31
# File 'lib/pipeloader/batch/context.rb', line 29

def initialize
  @objs = Hash.new { |hash, key| hash[key] = [] }
end

Class Method Details

.preloading?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/pipeloader/batch/context.rb', line 17

def preloading?
  Thread.current[PRELOADING_KEY] || false
end

.while_preloadingObject



21
22
23
24
25
26
# File 'lib/pipeloader/batch/context.rb', line 21

def while_preloading
  Thread.current[PRELOADING_KEY] = true
  yield
ensure
  Thread.current[PRELOADING_KEY] = false
end

Instance Method Details

#add(instance) ⇒ Object



33
34
35
# File 'lib/pipeloader/batch/context.rb', line 33

def add(instance)
  @objs[instance.class] << instance
end

#all(cls) ⇒ Object

The records of class ‘cls` in this group (an empty array if none).



38
39
40
# File 'lib/pipeloader/batch/context.rb', line 38

def all(cls)
  @objs[cls]
end