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.



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
336
337
338
339
# File 'lib/dommy/streams.rb', line 308

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.



306
307
308
# File 'lib/dommy/streams.rb', line 306

def readable
  @readable
end

#writableObject (readonly)

Returns the value of attribute writable.



306
307
308
# File 'lib/dommy/streams.rb', line 306

def writable
  @writable
end

Instance Method Details

#__js_get__(key) ⇒ Object



341
342
343
344
345
346
347
348
349
350
# File 'lib/dommy/streams.rb', line 341

def __js_get__(key)
  case key
  when "readable"
    @readable
  when "writable"
    @writable
  else
    Bridge::ABSENT
  end
end