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.



265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/dommy/text_codec.rb', line 265

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.__internal_close__ },
      "abort" => proc { |r| @readable.__internal_error__(r) }
    }
  )
end

Instance Attribute Details

#readableObject (readonly)

Returns the value of attribute readable.



263
264
265
# File 'lib/dommy/text_codec.rb', line 263

def readable
  @readable
end

#writableObject (readonly)

Returns the value of attribute writable.



263
264
265
# File 'lib/dommy/text_codec.rb', line 263

def writable
  @writable
end

Instance Method Details

#__js_get__(key) ⇒ Object



284
285
286
287
288
289
290
291
292
293
# File 'lib/dommy/text_codec.rb', line 284

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

#encodingObject



280
281
282
# File 'lib/dommy/text_codec.rb', line 280

def encoding
  "utf-8"
end