2020-06-29 12:09:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
class Lazy
|
2020-10-30 15:08:59 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
def initialize(&block)
|
|
|
|
@proc = block
|
|
|
|
end
|
|
|
|
|
|
|
|
def force
|
|
|
|
strong_memoize(:force) { self.class.force(@proc.call) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def then(&block)
|
|
|
|
self.class.new { yield force }
|
|
|
|
end
|
|
|
|
|
2020-12-23 00:10:04 +00:00
|
|
|
def catch(error_class = StandardError, &block)
|
|
|
|
self.class.new do
|
|
|
|
force
|
|
|
|
rescue error_class => e
|
|
|
|
yield e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-29 12:09:20 +00:00
|
|
|
# Force evaluation of a (possibly) lazy value
|
|
|
|
def self.force(value)
|
|
|
|
case value
|
2020-10-30 15:08:59 +00:00
|
|
|
when ::Gitlab::Graphql::Lazy
|
|
|
|
value.force
|
2020-06-29 12:09:20 +00:00
|
|
|
when ::BatchLoader::GraphQL
|
|
|
|
value.sync
|
2020-12-14 03:10:06 +00:00
|
|
|
when ::Gitlab::Graphql::Deferred
|
|
|
|
value.execute
|
2020-10-30 15:08:59 +00:00
|
|
|
when ::GraphQL::Execution::Lazy
|
|
|
|
value.value # part of the private api, but we can force this as well
|
2020-06-29 12:09:20 +00:00
|
|
|
when ::Concurrent::Promise
|
2020-10-30 15:08:59 +00:00
|
|
|
value.execute if value.state == :unscheduled
|
|
|
|
|
|
|
|
value.value # value.value(10.seconds)
|
2020-06-29 12:09:20 +00:00
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
2020-10-30 15:08:59 +00:00
|
|
|
|
|
|
|
def self.with_value(unforced, &block)
|
2020-11-05 12:09:05 +00:00
|
|
|
self.new { unforced }.then(&block)
|
2020-10-30 15:08:59 +00:00
|
|
|
end
|
2020-06-29 12:09:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|