-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better handling of state transitions.
- Loading branch information
Showing
4 changed files
with
78 additions
and
62 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2024, by Samuel Williams. | ||
|
||
require "protocol/http/body/wrapper" | ||
require "async/variable" | ||
|
||
module Async | ||
module HTTP | ||
module Protocol | ||
module HTTP1 | ||
# Keeps track of whether a body is being read, and if so, waits for it to be closed. | ||
class Finishable < ::Protocol::HTTP::Body::Wrapper | ||
def initialize(body) | ||
super(body) | ||
|
||
@closed = Async::Variable.new | ||
@error = nil | ||
|
||
@reading = false | ||
end | ||
|
||
def reading? | ||
@reading | ||
end | ||
|
||
def read | ||
@reading = true | ||
|
||
super | ||
end | ||
|
||
def close(error = nil) | ||
unless @closed.resolved? | ||
@error = error | ||
@closed.value = true | ||
end | ||
|
||
super | ||
end | ||
|
||
def wait | ||
if @reading | ||
@closed.wait | ||
else | ||
self.discard | ||
end | ||
end | ||
|
||
def inspect | ||
"#<#{self.class} closed=#{@closed} error=#{@error}> | #{super}" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters