Sha256: 0e9c452eab0a798a94531a3f17f690f7466f0c265d399891a3f8d81ae1f0f7a1

Contents?: true

Size: 1.6 KB

Versions: 43

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true
require "thread"

module ShopifyCLI
  module Theme
    module DevServer
      # Server-Sent events implementation for Rack.
      # Based on https://gist.github.com/raggi/ff7971991297e5c8a1ce
      class SSE
        class Event < Struct.new(:data)
          def to_s
            "data: #{data}\n" \
            "\n\n"
          end
        end

        class Stream
          def initialize(streams)
            @streams = streams
            @queue = Queue.new
          end

          def <<(event)
            raise TypeError, "expected SSE::Event" unless event.is_a?(Event)
            @queue << event
          end

          def each
            while (event = @queue.pop)
              yield event.to_s
            end
          end

          def close
            @streams.remove(self)
            @queue << nil
          end
        end

        class Streams
          def initialize
            @list = []
          end

          def size
            @list.size
          end

          def new
            stream = Stream.new(self)
            @list << stream
            stream
          end

          def remove(stream)
            raise TypeError, "expected SSE::Stream" unless stream.is_a?(Stream)
            @list.delete(stream)
          end

          def broadcast(data)
            raise TypeError, "expected String" unless data.is_a?(String)
            @list.each do |stream|
              stream << Event.new(data)
            end
          end

          def close
            @list.each(&:close)
            @list.clear
          end
        end
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
shopify-cli-2.13.0 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.12.0 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.11.2 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.11.1 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.11.0 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.10.2 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.10.1 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.10.0 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.9.0 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.8.0 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.7.4 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.7.3 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.7.2 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.7.1 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.7.0 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.6.6 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.6.5 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.6.4 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.6.3 lib/shopify_cli/theme/dev_server/sse.rb
shopify-cli-2.6.2 lib/shopify_cli/theme/dev_server/sse.rb