Sha256: a0103d4f836c324e24ed0384a5df3e04aa37ac53c693f3ae3a986a7ead3b67df

Contents?: true

Size: 856 Bytes

Versions: 1

Compression:

Stored size: 856 Bytes

Contents

# frozen_string_literal: true

require 'stringio'

module NanoBot
  module Components
    class Stream < StringIO
      def write(*args)
        if @callback
          begin
            @accumulated += args.first
          rescue StandardError => _e
            @accumulated = "#{@accumulated.force_encoding('UTF-8')}#{args.first.force_encoding('UTF-8')}"
          end

          @callback.call(@accumulated, args.first, false, args[1])
        end
        super(args.first)
      end

      def callback=(block)
        @accumulated = ''
        @callback = block
      end

      def finish
        flush
        result = string.clone
        truncate(0)
        rewind

        if @callback
          @callback.call(@accumulated, nil, true)
          @callback = nil
          @accumulated = nil
        end

        result
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nano-bots-1.1.0 components/stream.rb