| 1 |
|
| 2 |
|
| 3 |
require_relative '../error' |
| 4 |
|
| 5 |
module Plugin::MastodonSseStreaming::Middleware |
| 6 |
class ConnectionKeepingStream |
| 7 |
def initialize(stream, keeper:) |
| 8 |
@stream = stream |
| 9 |
@cooldown_time = keeper |
| 10 |
end |
| 11 |
|
| 12 |
def to_enum |
| 13 |
Enumerator.new do |yielder| |
| 14 |
loop do |
| 15 |
connect(yielder) |
| 16 |
@cooldown_time.sleep |
| 17 |
end |
| 18 |
end |
| 19 |
end |
| 20 |
|
| 21 |
def connect(yielder) |
| 22 |
@stream.to_enum.each do |chunk| |
| 23 |
@cooldown_time.reset |
| 24 |
yielder << chunk |
| 25 |
end |
| 26 |
@cooldown_time.client_error |
| 27 |
rescue Plugin::MastodonSseStreaming::ResponseError => exception |
| 28 |
@cooldown_time.status_code(exception.code) |
| 29 |
rescue Plugin::MastodonSseStreaming::ConnectionRefusedError => exception |
| 30 |
notice exception |
| 31 |
@cooldown_time.client_error |
| 32 |
end |
| 33 |
end |
| 34 |
end |