Class: Farce::WeakValue
- Inherits:
-
Object
- Object
- Farce::WeakValue
- Includes:
- Abstract::Value, Internal::Inspect, Internal::MarshalSupport::WeakValue
- Defined in:
- lib/farce/weak_value.rb,
lib/farce/integrations/active_support/duplicable.rb
Overview
ActiveSupport Integration collapse
-
#duplicable? ⇒ Boolean
True.
Methods included from Abstract::Value
Class Method Summary collapse
-
.new(value) ⇒ WeakValue
A new weak value wrapping the given object.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Checks whether the referenced object has avoided garbage collection and has not been moved.
-
#freeze ⇒ self
Freezes the weak value, making the referenced object shareable if it is still alive.
-
#initialize(value) ⇒ WeakValue
constructor
A new instance of WeakValue.
-
#moved? ⇒ Boolean
Checks if the referenced object has been moved to another Ractor.
-
#ractor_shareable? ⇒ Boolean
True if the referenced object is shareable by Ractor, false otherwise.
-
#value ⇒ BasicObject
Retrieves the referenced object if it is still alive and has not been moved.
Methods included from Abstract::Value
Constructor Details
#initialize(value) ⇒ WeakValue
Returns a new instance of WeakValue.
101 102 103 104 105 106 107 108 109 |
# File 'lib/farce/weak_value.rb', line 101 def initialize(value) if Ractor.shareable?(value) @atom = Strict::WeakAtom.new(value) @mutex = nil else @atom = Unshared::WeakAtom.new(value) @mutex = Mutex.new end end |
Class Method Details
.new(value) ⇒ WeakValue
Returns a new weak value wrapping the given object.
89 |
# File 'lib/farce/weak_value.rb', line 89 def self.new(value) = value.nil? ? NIL_VALUE : super |
Instance Method Details
#alive? ⇒ Boolean
Checks whether the referenced object has avoided garbage collection and has not been moved.
113 114 115 116 117 |
# File 'lib/farce/weak_value.rb', line 113 def alive? !_value.nil? rescue Ractor::MovedError false end |
#duplicable? ⇒ Boolean
This methods is only available if ActiveSupport has been loaded.
Returns true.
18 |
# File 'lib/farce/integrations/active_support/duplicable.rb', line 18 def duplicable? = true |
#freeze ⇒ self
Freezes the weak value, making the referenced object shareable if it is still alive. Can be called after the value has been garbage collected or moved. Will raise however if the referenced object is still alive and cannot be made shareable.
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/farce/weak_value.rb', line 123 def freeze @mutex&.synchronize do return self if frozen? value = begin Ractor.make_shareable(_value) rescue Ractor::MovedError MOVED end @atom = Strict::WeakAtom.new(value) @mutex = nil end super end |
#moved? ⇒ Boolean
Checks if the referenced object has been moved to another Ractor.
139 140 141 142 143 |
# File 'lib/farce/weak_value.rb', line 139 def moved? Ractor::MovedObject === _value rescue Ractor::MovedError true end |
#ractor_shareable? ⇒ Boolean
Returns true if the referenced object is shareable by Ractor, false otherwise.
146 147 148 149 150 151 |
# File 'lib/farce/weak_value.rb', line 146 def ractor_shareable? return false unless Ractor.shareable?(_value) freeze && true rescue Ractor::MovedError freeze && true end |
#value ⇒ BasicObject
Retrieves the referenced object if it is still alive and has not been moved. Raises if it is no longer reachable.
157 158 159 160 161 162 163 |
# File 'lib/farce/weak_value.rb', line 157 def value value = _value raise WeakRefError, "Invalid Reference - probably recycled" if value.nil? value rescue Ractor::MovedError raise WeakRefError, "Invalid Reference - referenced object has been moved" end |