Class: Dommy::TextEncoderStream

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/text_codec.rb

Overview

‘TextEncoderStream` — Stream-shaped wrapper over `TextEncoder`. `write(string)` flushes UTF-8 bytes downstream.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ TextEncoderStream

Returns a new instance of TextEncoderStream.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dommy/text_codec.rb', line 112

def initialize(window)
  encoder = TextEncoder.new
  @readable = ReadableStream.new(window)
  controller = TransformStreamDefaultController.new(@readable)

  @writable = WritableStream.new(
    window,
    {
      "write" => proc { |chunk| controller.enqueue(encoder.encode(chunk)) },
      "close" => proc { @readable.__close__ },
      "abort" => proc { |r| @readable.__error__(r) }
    }
  )
end

Instance Attribute Details

#readableObject (readonly)

Returns the value of attribute readable.



110
111
112
# File 'lib/dommy/text_codec.rb', line 110

def readable
  @readable
end

#writableObject (readonly)

Returns the value of attribute writable.



110
111
112
# File 'lib/dommy/text_codec.rb', line 110

def writable
  @writable
end

Instance Method Details

#__js_get__(key) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/dommy/text_codec.rb', line 131

def __js_get__(key)
  case key
  when "readable"
    @readable
  when "writable"
    @writable
  when "encoding"
    encoding
  end
end

#encodingObject



127
128
129
# File 'lib/dommy/text_codec.rb', line 127

def encoding
  "utf-8"
end