module Fibre::Synchrony

Extend some classes to support synchronous operations

Outline,

use Fibre::Synchrony [req1, req2].sync { op1: req1, op2: req2 }.sync

Public Instance Methods

await() click to toggle source
# File lib/fibre/core_ext/synchrony.rb, line 12
def await
  res = Fiber.scope { collect(&:await) }
  res.collect(&:result)
end
await!() click to toggle source
# File lib/fibre/core_ext/synchrony.rb, line 17
def await!
  res = Fiber.scope do
    await_deep_scoped
  end

  res.each do |mock|
    node = self
    path = mock.path.split(/./)
    key = path.pop
    path.each { |k| node = node[k] }
    node[key] = mock.result
  end

  self
end
await_deep_scoped(path: []) click to toggle source
# File lib/fibre/core_ext/synchrony.rb, line 33
def await_deep_scoped(path: [])
  each_with_index do |item, index|
    # deeeep
    if item.is_a?(Array) || item.is_a?(Hash) # item.respond_to?(:deep_sync_scoped) not works in ruby 2.1.2
      item.await_deep_scoped(path: path + [index])
      next
    end

    item.await.tap do |mock|
      mock.path = path + [index]
    end
  end
end