2020-07-28 23:09:34 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
class Lazy
|
2021-01-29 00:20:46 +05:30
|
|
|
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-07-28 23:09:34 +05:30
|
|
|
# Force evaluation of a (possibly) lazy value
|
|
|
|
def self.force(value)
|
|
|
|
case value
|
2021-01-29 00:20:46 +05:30
|
|
|
when ::Gitlab::Graphql::Lazy
|
|
|
|
value.force
|
2020-07-28 23:09:34 +05:30
|
|
|
when ::BatchLoader::GraphQL
|
|
|
|
value.sync
|
2021-02-22 17:27:13 +05:30
|
|
|
when ::Gitlab::Graphql::Deferred
|
|
|
|
value.execute
|
2021-01-29 00:20:46 +05:30
|
|
|
when ::GraphQL::Execution::Lazy
|
|
|
|
value.value # part of the private api, but we can force this as well
|
2020-07-28 23:09:34 +05:30
|
|
|
when ::Concurrent::Promise
|
2021-01-29 00:20:46 +05:30
|
|
|
value.execute if value.state == :unscheduled
|
|
|
|
|
|
|
|
value.value # value.value(10.seconds)
|
2020-07-28 23:09:34 +05:30
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
def self.with_value(unforced, &block)
|
|
|
|
self.new { unforced }.then(&block)
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|