Simple cancellation token using channels, based on the VS Code CancellationToken.
-
Add the dependency to your
shard.yml
:dependencies: cancellation_token: github: nobodywasishere/cancellation_token
-
Run
shards install
require "cancellation_token"
# Create a new token source
token_source = CancellationTokenSource.new
token = token_source.token
# Spawn a fiber that checks the token to see if it should stop
spawn do
loop do
# Check to see if the fiber should stop
if token.cancelled?
break
end
puts "hello world"
sleep(1.second)
# Do fiber work here
end
end
# Later on, if desired, cancel the token
token_source.cancel
- Fork it (https://github.com/nobodywasishere/cancellation_token/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Margret Riegert - creator and maintainer