Class: Dommy::TransformStream

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

Overview

‘TransformStream` — links a writable + readable so chunks flow through a `transform` callback.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, transformer = nil) ⇒ TransformStream

Returns a new instance of TransformStream.



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/dommy/streams.rb', line 315

def initialize(window, transformer = nil)
  @window = window
  t = transformer.is_a?(Hash) ? transformer.transform_keys(&:to_s) : {}

  @readable = ReadableStream.new(window)
  controller = TransformStreamDefaultController.new(@readable)

  @writable = WritableStream.new(
    window,
    {
      "write" => proc do |chunk|
        if t["transform"].respond_to?(:__js_call__)
          t["transform"].__js_call__("call", [chunk, controller])
        elsif t["transform"].respond_to?(:call)
          t["transform"].call(chunk, controller)
        else
          controller.enqueue(chunk)
        end
      end,
      "close" => proc { @readable.__close__ },
      "abort" => proc { |reason| @readable.__error__(reason) }
    }
  )

  if t["start"]
    if t["start"].respond_to?(:__js_call__)
      t["start"].__js_call__("call", [controller])
    elsif t["start"].respond_to?(:call)
      t["start"].call(controller)
    end
  end
end

Instance Attribute Details

#readableObject (readonly)

Returns the value of attribute readable.



313
314
315
# File 'lib/dommy/streams.rb', line 313

def readable
  @readable
end

#writableObject (readonly)

Returns the value of attribute writable.



313
314
315
# File 'lib/dommy/streams.rb', line 313

def writable
  @writable
end

Instance Method Details

#__js_get__(key) ⇒ Object



348
349
350
351
352
353
354
355
# File 'lib/dommy/streams.rb', line 348

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