Class: Gitlab::Graphql::Lazy

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/graphql/lazy.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(&block) ⇒ Lazy

Returns a new instance of Lazy.


8
9
10
# File 'lib/gitlab/graphql/lazy.rb', line 8

def initialize(&block)
  @proc = block
end

Class Method Details

.force(value) ⇒ Object

Force evaluation of a (possibly) lazy value


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitlab/graphql/lazy.rb', line 29

def self.force(value)
  case value
  when ::Gitlab::Graphql::Lazy
    value.force
  when ::BatchLoader::GraphQL
    value.sync
  when ::Gitlab::Graphql::Deferred
    value.execute
  when ::GraphQL::Execution::Lazy
    value.value # part of the private api, but we can force this as well
  when ::Concurrent::Promise
    value.execute if value.state == :unscheduled

    value.value # value.value(10.seconds)
  else
    value
  end
end

.with_value(unforced, &block) ⇒ Object


48
49
50
# File 'lib/gitlab/graphql/lazy.rb', line 48

def self.with_value(unforced, &block)
  self.new { unforced }.then(&block)
end

Instance Method Details

#catch(error_class = StandardError, &block) ⇒ Object


20
21
22
23
24
25
26
# File 'lib/gitlab/graphql/lazy.rb', line 20

def catch(error_class = StandardError, &block)
  self.class.new do
    force
  rescue error_class => e
    yield e
  end
end

#forceObject


12
13
14
# File 'lib/gitlab/graphql/lazy.rb', line 12

def force
  strong_memoize(:force) { self.class.force(@proc.call) }
end

#then(&block) ⇒ Object


16
17
18
# File 'lib/gitlab/graphql/lazy.rb', line 16

def then(&block)
  self.class.new { yield force }
end