This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Released under the MIT License.
|
||||
# Copyright, 2022-2024, by Samuel Williams.
|
||||
|
||||
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
|
||||
@storage = Fiber.current.storage
|
||||
else
|
||||
raise TypeError, "Storage must be a hash!" unless storage.is_a?(Hash)
|
||||
|
||||
@storage = storage
|
||||
end
|
||||
|
||||
super(*arguments, **options, &block)
|
||||
end
|
||||
|
||||
# Set the storage associated with this fiber, clearing any previous storage.
|
||||
def storage=(hash)
|
||||
@storage = hash.dup
|
||||
end
|
||||
|
||||
# The storage associated with this fiber.
|
||||
def storage
|
||||
@storage.dup
|
||||
end
|
||||
|
||||
# @private
|
||||
def __storage__
|
||||
@storage ||= {}
|
||||
end
|
||||
end
|
||||
|
||||
unless Fiber.current.respond_to?(:storage)
|
||||
prepend Storage
|
||||
|
||||
# Get a value from the current fiber's storage.
|
||||
def self.[] key
|
||||
raise TypeError, "Key must be symbol!" unless key.is_a?(Symbol)
|
||||
|
||||
self.current.__storage__[key]
|
||||
end
|
||||
|
||||
# Set a value in the current fiber's storage.
|
||||
def self.[]= key, value
|
||||
raise TypeError, "Key must be symbol!" unless key.is_a?(Symbol)
|
||||
|
||||
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"
|
||||
Fiber[key] = true
|
||||
Fiber[key]
|
||||
end.resume
|
||||
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)
|
||||
|
||||
super(eval(key.inspect), value)
|
||||
end
|
||||
end
|
||||
|
||||
singleton_class.prepend FixBorkedKeys
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Released under the MIT License.
|
||||
# Copyright, 2022-2024, by Samuel Williams.
|
||||
|
||||
require 'fiber'
|
||||
|
||||
class Fiber
|
||||
module Storage
|
||||
VERSION = "1.0.0"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
# MIT License
|
||||
|
||||
Copyright, 2022-2024, by Samuel Williams.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,35 @@
|
||||
# Fiber::Storage
|
||||
|
||||
This gem provides a shim for `Fiber.[]`, `Fiber.[]=`, `Fiber#storage`, `Fiber#storage=`, which was introduced in Ruby 3.2.
|
||||
|
||||
[](https://github.com/ioquatix/fiber-storage/actions?workflow=Test)
|
||||
|
||||
## Motivation
|
||||
|
||||
Ruby 3.2 introduces inheritable fiber storage for per-request or per-operation state. This gem provides a shim for Ruby 3.1 and earlier to make adoption easier. It isn't able to provide the full range of features, but it should be sufficient for most use cases.
|
||||
|
||||
Notably, it does not support inheritance across threads or lazy Enumerator. This is a limitation of the shim implementation.
|
||||
|
||||
## Usage
|
||||
|
||||
Please see the [project documentation](https://ioquatix.github.io/fiber-storage/) for more details.
|
||||
|
||||
- [Getting Started](https://ioquatix.github.io/fiber-storage/guides/getting-started/index) - This guide explains how to use this gem and provides a brief overview of the features.
|
||||
|
||||
## Contributing
|
||||
|
||||
We welcome contributions to this project.
|
||||
|
||||
1. Fork it.
|
||||
2. Create your feature branch (`git checkout -b my-new-feature`).
|
||||
3. Commit your changes (`git commit -am 'Add some feature'`).
|
||||
4. Push to the branch (`git push origin my-new-feature`).
|
||||
5. Create new Pull Request.
|
||||
|
||||
### Developer Certificate of Origin
|
||||
|
||||
In order to protect users of this project, we require all contributors to comply with the [Developer Certificate of Origin](https://developercertificate.org/). This ensures that all contributions are properly licensed and attributed.
|
||||
|
||||
### Community Guidelines
|
||||
|
||||
This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.
|
||||
Reference in New Issue
Block a user