Sha256: 3880af96224799a793624ff9d86f6bb15047650bd0cc67e1650ced40780ded3c

Contents?: true

Size: 955 Bytes

Versions: 4

Compression:

Stored size: 955 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 rewindable?
					false
				end
				
				def rewind
					false
				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

4 entries across 4 versions & 1 rubygems

Version Path
protocol-http-0.32.0 lib/protocol/http/body/completable.rb
protocol-http-0.31.0 lib/protocol/http/body/completable.rb
protocol-http-0.30.0 lib/protocol/http/body/completable.rb
protocol-http-0.29.0 lib/protocol/http/body/completable.rb