rblocks 1.0.0.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +1 -1
- data/Gemfile.lock +2 -1
- data/README.md +5 -1
- data/lib/rblocks/ticker.rb +20 -5
- data/lib/rblocks/timer.rb +1 -1
- data/rblocks.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01c7018ed705c24b9b3089f4f67ec7fb7e6fdba926de0dd65cdda5a487d4b0ea
|
4
|
+
data.tar.gz: 0b6b80d26556c3d47dfacf3e8e4ab5a8bbd630019feb517708094c9e3c878175
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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@
|
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
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[](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 "
|
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
|
data/lib/rblocks/ticker.rb
CHANGED
@@ -2,21 +2,29 @@
|
|
2
2
|
|
3
3
|
module RBlock
|
4
4
|
#
|
5
|
-
# RBlock::
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# #
|
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
|
-
@
|
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
data/rblocks.gemspec
CHANGED