This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Released under the MIT License.
|
||||
# Copyright, 2023, by Samuel Williams.
|
||||
|
||||
require 'fiber'
|
||||
|
||||
class Fiber
|
||||
# A mechanism for annotating fibers.
|
||||
module Annotation
|
||||
# Annotate the current fiber with the given annotation.
|
||||
# @parameter annotation [Object] The annotation to set.
|
||||
def initialize(annotation: nil, **options, &block)
|
||||
@annotation = annotation
|
||||
super(**options, &block)
|
||||
end
|
||||
|
||||
# Get the current annotation.
|
||||
# @returns [Object] The current annotation.
|
||||
attr_accessor :annotation
|
||||
|
||||
# Annotate the current fiber with the given annotation.
|
||||
#
|
||||
# If a block is given, the annotation is set for the duration of the block and then restored to the previous value.
|
||||
#
|
||||
# The block form of this method should only be invoked on the current fiber.
|
||||
#
|
||||
# @parameter annotation [Object] The annotation to set.
|
||||
# @yields {} The block to execute with the given annotation.
|
||||
# @returns [Object] The return value of the block.
|
||||
def annotate(annotation)
|
||||
if block_given?
|
||||
raise "Cannot annotation a different fiber!" unless Fiber.current == self
|
||||
|
||||
begin
|
||||
current_annotation = @annotation
|
||||
@annotation = annotation
|
||||
return yield
|
||||
ensure
|
||||
@annotation = current_annotation
|
||||
end
|
||||
else
|
||||
@annotation = annotation
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
unless Fiber.method_defined?(:annotation)
|
||||
Fiber.prepend(Fiber::Annotation)
|
||||
|
||||
# @scope Fiber
|
||||
# @name self.annotate
|
||||
# Annotate the current fiber with the given annotation.
|
||||
def Fiber.annotate(...)
|
||||
Fiber.current.annotate(...)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Released under the MIT License.
|
||||
# Copyright, 2023, by Samuel Williams.
|
||||
|
||||
class Fiber
|
||||
module Annotation
|
||||
VERSION = "0.2.0"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
# MIT License
|
||||
|
||||
Copyright, 2023, 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,19 @@
|
||||
# Fiber::Annotation
|
||||
|
||||
A simple way to annotate what a fiber is currently doing, useful for debugging, logging and tracing.
|
||||
|
||||
[](https://github.com/ioquatix/fiber-annotation/actions?workflow=Test)
|
||||
|
||||
## Usage
|
||||
|
||||
Please see the [project documentation](https://ioquatix.github.io/fiber-annotation).
|
||||
|
||||
## 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.
|
||||
Reference in New Issue
Block a user