Sha256: 7969399abf111cce9beebb42cafb115d333ffc9268f13e9b48a764c701ed5e81

Contents?: true

Size: 882 Bytes

Versions: 11

Compression:

Stored size: 882 Bytes

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2023, 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
					if @body
						result = super
						
						@callback.call
						
						@body = nil
						
						return result
					end
				end
				
				def close(error = nil)
					if @body
						super
						
						@callback.call(error)
						
						@body = nil
					end
				end
			end
		end
	end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
protocol-http-0.26.1 lib/protocol/http/body/completable.rb
protocol-http-0.26.0 lib/protocol/http/body/completable.rb
protocol-http-0.25.0 lib/protocol/http/body/completable.rb
protocol-http-0.24.7 lib/protocol/http/body/completable.rb
protocol-http-0.24.6 lib/protocol/http/body/completable.rb
protocol-http-0.24.5 lib/protocol/http/body/completable.rb
protocol-http-0.24.4 lib/protocol/http/body/completable.rb
protocol-http-0.24.3 lib/protocol/http/body/completable.rb
protocol-http-0.24.2 lib/protocol/http/body/completable.rb
protocol-http-0.24.1 lib/protocol/http/body/completable.rb
protocol-http-0.24.0 lib/protocol/http/body/completable.rb