Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use @callback to track the state of the invocation. #52

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions lib/protocol/http/body/completable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,20 @@ def initialize(body, callback)
end

def finish
if @body
result = super

@callback.call

@body = nil

return result
super.tap do
if @callback
@callback.call
@callback = nil
end
end
end

def close(error = nil)
if @body
super

@callback.call(error)

@body = nil
super.tap do
if @callback
@callback.call(error)
@callback = nil
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/protocol/http/body/completable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require 'protocol/http/body/completable'
require 'protocol/http/body/buffered'
require 'protocol/http/request'

describe Protocol::HTTP::Body::Completable do
let(:body) {Protocol::HTTP::Body::Buffered.new}
Expand Down Expand Up @@ -76,5 +77,10 @@
completable.finish
end
end

it "doesn't break #read after finishing" do
completable.finish
expect(completable.read).to be_nil
end
end
end
Loading