2020-06-29 08:09:20 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
class Lazy
|
2020-10-30 11:08:59 -04: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-22 19:10:04 -05:00
|
|
|
def catch(error_class = StandardError, &block)
|
|
|
|
self.class.new do
|
|
|
|
force
|
|
|
|
rescue error_class => e
|
|
|
|
yield e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-29 08:09:20 -04:00
|
|
|
# Force evaluation of a (possibly) lazy value
|
|
|
|
def self.force(value)
|
|
|
|
case value
|
2020-10-30 11:08:59 -04:00
|
|
|
when ::Gitlab::Graphql::Lazy
|
|
|
|
value.force
|
2020-06-29 08:09:20 -04:00
|
|
|
when ::BatchLoader::GraphQL
|
|
|
|
value.sync
|
2020-12-13 22:10:06 -05:00
|
|
|
when ::Gitlab::Graphql::Deferred
|
|
|
|
value.execute
|
2020-10-30 11:08:59 -04:00
|
|
|
when ::GraphQL::Execution::Lazy
|
|
|
|
value.value # part of the private api, but we can force this as well
|
2020-06-29 08:09:20 -04:00
|
|
|
when ::Concurrent::Promise
|
2020-10-30 11:08:59 -04:00
|
|
|
value.execute if value.state == :unscheduled
|
|
|
|
|
|
|
|
value.value # value.value(10.seconds)
|
2020-06-29 08:09:20 -04:00
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
2020-10-30 11:08:59 -04:00
|
|
|
|
|
|
|
def self.with_value(unforced, &block)
|
2020-11-05 07:09:05 -05:00
|
|
|
self.new { unforced }.then(&block)
|
2020-10-30 11:08:59 -04:00
|
|
|
end
|
2020-06-29 08:09:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|