rblocks 1.0.0.1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63d8fe569f6c82bd50be2c575253688e7a9a627b1715c95791d26811e786f7aa
4
- data.tar.gz: 8468c3c058ad92285f66f67b65f17139fd7aad458747642c1f6784ab4a4d6071
3
+ metadata.gz: 01c7018ed705c24b9b3089f4f67ec7fb7e6fdba926de0dd65cdda5a487d4b0ea
4
+ data.tar.gz: 0b6b80d26556c3d47dfacf3e8e4ab5a8bbd630019feb517708094c9e3c878175
5
5
  SHA512:
6
- metadata.gz: c9f0b79a34e6e0e7ab8acc5f140f4edac65aa6329b50f1dc14fde607e4c23393c827e9e33f8497526238327f47edbf7fc8a1edf0468ae4eab14ad54dc66b9533
7
- data.tar.gz: efb80c3663d06b2d18708702e219fd6a102c9645c2286dd47cc049a97d8b3e0862594025bb6040b6cef6f138c588078358342602cf09625962b6481990e7a4b2
6
+ metadata.gz: 0ba53ff9d418298ee802cad380a79b6db41f4b948e9c0c5a92f547d499355ddcdb7a5b121d5d378531b3ff222e33067550ac22986c377c68a85481b3ea2bc827
7
+ data.tar.gz: c1da736151f7b4bb39c68f977f307fae936fc1a02e800af8ed8ec06a71a6215f1876fd17711d904093c8e0342951a7a8c5d12abd5731e96feef5bb6588960c94
data/CODE_OF_CONDUCT.md CHANGED
@@ -39,7 +39,7 @@ This Code of Conduct applies within all community spaces, and also applies when
39
39
 
40
40
  ## Enforcement
41
41
 
42
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at armin@hypofriend.de. All complaints will be reviewed and investigated promptly and fairly.
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at armin@pasalic.me. All complaints will be reviewed and investigated promptly and fairly.
43
43
 
44
44
  All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
45
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rblocks (1.0.0.1)
4
+ rblocks (1.0.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -35,6 +35,7 @@ GEM
35
35
 
36
36
  PLATFORMS
37
37
  arm64-darwin-20
38
+ x86_64-linux
38
39
 
39
40
  DEPENDENCIES
40
41
  minitest (~> 5.0)
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Ruby](https://github.com/Krule/rblocks/actions/workflows/main.yml/badge.svg)](https://github.com/Krule/rblocks/actions/workflows/main.yml)
2
+
1
3
  # Ruby Blocks
2
4
 
3
5
  Ruby Blocks is a set of basic utilities built on top of the Ruby Ractor.
@@ -23,7 +25,7 @@ Or install it yourself as:
23
25
  ### Timer
24
26
 
25
27
  ```ruby
26
- require "rblock/timer"
28
+ require "rblocks/timer"
27
29
 
28
30
  timer = RBlock::Timer.new(30) # Set timer for 30s
29
31
  timer.wait # block until timer is done
@@ -48,6 +50,8 @@ ticker = RBlock::Ticker.new(1) # Tick once every second
48
50
  10.times do
49
51
  ticker.wait { puts "tick" } # output "tick" 10 times with a delay of 1s between each
50
52
  end
53
+
54
+ ticker.stop!
51
55
  ```
52
56
 
53
57
  ## Development
@@ -2,21 +2,29 @@
2
2
 
3
3
  module RBlock
4
4
  #
5
- # RBlock::Timer is a simple Go inspired timer. Usage
6
- # timer = RBlock::Timer.new(10)
7
- # timer.wait do
8
- # # ... someting once the time is up
5
+ # RBlock::Ticker is a simple Go inspired ticker. Usage
6
+ # ticker = RBlock::Ticker.new(0.5)
7
+ # 10.times do
8
+ # ticker.wait # blocks for 1/2s
9
+ # # do a thing every 1/2s
9
10
  # end
10
11
  #
11
12
  class Ticker
13
+ class ErrorTerminated < StandardError; end
14
+
12
15
  NAME = "r block ticker"
13
16
  #
14
17
  # Constructs and starts a ticker
15
18
  # @param interval [Numeric] interval duration in seconds
16
19
  #
17
20
  def initialize(interval)
18
- @ractor = Ractor.new(interval) do |i|
21
+ @stop_ractor = Ractor.new { loop { Ractor.yield(Ractor.recv, move: true) } }
22
+
23
+ @ractor = Ractor.new(interval, @stop_ractor, name: NAME) do |i, stop|
19
24
  loop do
25
+ r, = Ractor.select(stop, send(nil))
26
+ break if r == stop
27
+
20
28
  sleep(i)
21
29
  Ractor.yield(Time.now)
22
30
  end
@@ -28,6 +36,13 @@ module RBlock
28
36
  t = @ractor.take
29
37
  yield(self) if block_given?
30
38
  t
39
+ rescue Ractor::ClosedError
40
+ raise ErrorTerminated, "ticker stopped"
41
+ end
42
+
43
+ # Stops the ticker. Calls to #wait
44
+ def stop!
45
+ @stop_ractor.send(:stop) and loop { break unless wait }
31
46
  end
32
47
  end
33
48
  end
data/lib/rblocks/timer.rb CHANGED
@@ -15,7 +15,7 @@ module RBlock
15
15
  # @param duration [Numeric] duration in seconds
16
16
  #
17
17
  def initialize(duration)
18
- @ractor = Ractor.new(duration, NAME) do |time|
18
+ @ractor = Ractor.new(duration, name: NAME) do |time|
19
19
  sleep(time)
20
20
 
21
21
  Ractor.yield(:done)
data/rblocks.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "rblocks"
5
- spec.version = "1.0.0.1"
5
+ spec.version = "1.0.1"
6
6
  spec.authors = ["Armin Pasalic"]
7
7
  spec.email = ["armin@pasalic.me"]
8
8
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rblocks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.1
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Armin Pasalic