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.



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/dommy/streams.rb', line 304

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.__internal_close__ },
      "abort" => proc { |reason| @readable.__internal_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.



302
303
304
# File 'lib/dommy/streams.rb', line 302

def readable
  @readable
end

#writableObject (readonly)

Returns the value of attribute writable.



302
303
304
# File 'lib/dommy/streams.rb', line 302

def writable
  @writable
end

Instance Method Details

#__js_get__(key) ⇒ Object



337
338
339
340
341
342
343
344
# File 'lib/dommy/streams.rb', line 337

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