Files
news.gitea.happylizard.me/bin/gems/async-2.23.0/lib/async/variable.rb
T
pizza2d1 4cee170d66
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 9s
Add bin and edit workflow
2026-09-16 13:11:16 -06:00

60 lines
1.1 KiB
Ruby

# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2021-2024, by Samuel Williams.
require_relative "condition"
module Async
# A synchronization primitive that allows one task to wait for another task to resolve a value.
class Variable
# Create a new variable.
#
# @parameter condition [Condition] The condition to use for synchronization.
def initialize(condition = Condition.new)
@condition = condition
@value = nil
end
# Resolve the value.
#
# Signals all waiting tasks.
#
# @parameter value [Object] The value to resolve.
def resolve(value = true)
@value = value
condition = @condition
@condition = nil
self.freeze
condition.signal(value)
end
# Alias for {#resolve}.
def value=(value)
self.resolve(value)
end
# Whether the value has been resolved.
#
# @returns [Boolean] Whether the value has been resolved.
def resolved?
@condition.nil?
end
# Wait for the value to be resolved.
#
# @returns [Object] The resolved value.
def wait
@condition&.wait
return @value
end
# Alias for {#wait}.
def value
self.wait
end
end
end