summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/sass-embedded-1.89.2-arm64-darwin/lib/sass/compiler/channel.rb
blob: 254732bd1a7c4cfd36c9968d84fb7975e3c88b7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

module Sass
  class Compiler
    # The {Channel} class.
    #
    # It manages the lifecycle of {Dispatcher}.
    class Channel
      def initialize(*args, **kwargs, &block)
        @args = args
        @kwargs = kwargs
        @block = block
        @dispatcher = Dispatcher.new(*@args, **@kwargs, &@block)
        @mutex = Mutex.new
      end

      def close
        @mutex.synchronize do
          unless @dispatcher.nil?
            @dispatcher.close
            @dispatcher = nil
          end
        end
      end

      def closed?
        @mutex.synchronize do
          @dispatcher.nil?
        end
      end

      def stream(host)
        @mutex.synchronize do
          raise IOError, 'closed compiler' if @dispatcher.nil?

          Stream.new(@dispatcher, host)
        rescue Errno::EBUSY
          @dispatcher = Dispatcher.new(*@args, **@kwargs, &@block)
          Stream.new(@dispatcher, host)
        end
      end

      # The {Stream} between {Dispatcher} and {Host}.
      class Stream
        attr_reader :id

        def initialize(dispatcher, host)
          @dispatcher = dispatcher
          @id = @dispatcher.subscribe(host)
        end

        def close
          @dispatcher.unsubscribe(@id)
        end

        def error(...)
          @dispatcher.error(...)
        end

        def send_proto(...)
          @dispatcher.send_proto(...)
        end
      end

      private_constant :Stream
    end

    private_constant :Channel
  end
end