Skip to content

Commit

Permalink
Documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 8, 2024
1 parent 7cdfa94 commit 4d161e6
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/fiber/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

require 'fiber'

# @namespace
class Fiber
# Provides compatibility shims for fiber storage.
module Storage
# Initialize the fiber with the given storage.
def initialize(*arguments, storage: true, **options, &block)
case storage
when true
Expand All @@ -30,6 +33,7 @@ def storage
@storage.dup
end

# @private
def __storage__
@storage ||= {}
end
Expand All @@ -52,6 +56,7 @@ def self.[]= key, value
self.current.__storage__[key] = value
end
else
# Whether the fiber storage has buggy keys. Unfortunately the original implementation of fiber storage was broken, this method detects the bug and is used to apply a fix.
def self.__borked_keys__
!Fiber.new do
key = :"#{self.object_id}.key"
Expand All @@ -61,13 +66,16 @@ def self.__borked_keys__
end

if __borked_keys__
# This is a fix for the original implementation of fiber storage which incorrectly handled non-dynamic symbol keys.
module FixBorkedKeys
# Lookup the value for the key, ensuring the symbol is dynamic.
def [](key)
raise TypeError, "Key must be symbol!" unless key.is_a?(Symbol)

super(eval(key.inspect))
end

# Assign the value to the key, ensuring the symbol is dynamic.
def []=(key, value)
raise TypeError, "Key must be symbol!" unless key.is_a?(Symbol)

Expand Down

0 comments on commit 4d161e6

Please sign in to comment.