Sha256: 052bb7c41a28d24da9ae909562439b8527b4f9abccd4caaf7265f7b08ac81a40

Contents?: true

Size: 872 Bytes

Versions: 3

Compression:

Stored size: 872 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
					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

3 entries across 3 versions & 1 rubygems

Version Path
protocol-http-0.26.4 lib/protocol/http/body/completable.rb
protocol-http-0.26.3 lib/protocol/http/body/completable.rb
protocol-http-0.26.2 lib/protocol/http/body/completable.rb