Sha256: 8e9e81fef38e8be76710ff455db8172dbf465b8c46dee22e3c9c2ce9eb00fc8b

Contents?: true

Size: 872 Bytes

Versions: 8

Compression:

Stored size: 872 Bytes

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2024, by Samuel Williams.

require_relative 'wrapper'

module Protocol
	module HTTP
		module Body
			# Invokes a callback once the body has completed, either successfully or due to an error.
			class Completable < Wrapper
				def self.wrap(message, &block)
					if body = message&.body and !body.empty?
						message.body = self.new(message.body, block)
					else
						yield
					end
				end
				
				def initialize(body, callback)
					super(body)
					
					@callback = callback
				end
				
				def finish
					super.tap do
						if @callback
							@callback.call
							@callback = nil
						end
					end
				end
				
				def close(error = nil)
					super.tap do
						if @callback
							@callback.call(error)
							@callback = nil
						end
					end
				end
			end
		end
	end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
protocol-http-0.28.2 lib/protocol/http/body/completable.rb
protocol-http-0.28.1 lib/protocol/http/body/completable.rb
protocol-http-0.28.0 lib/protocol/http/body/completable.rb
protocol-http-0.27.0 lib/protocol/http/body/completable.rb
protocol-http-0.26.8 lib/protocol/http/body/completable.rb
protocol-http-0.26.7 lib/protocol/http/body/completable.rb
protocol-http-0.26.6 lib/protocol/http/body/completable.rb
protocol-http-0.26.5 lib/protocol/http/body/completable.rb