File: synchrony.rb

package info (click to toggle)
ruby-redis 4.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,168 kB
  • sloc: ruby: 12,820; makefile: 107; sh: 24
file content (26 lines) | stat: -rw-r--r-- 495 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# frozen_string_literal: true

class Wire < Fiber
  # We cannot run this fiber explicitly because EM schedules it. Resuming the
  # current fiber on the next tick to let the reactor do work.
  def self.pass
    f = Fiber.current
    EM.next_tick { f.resume }
    Fiber.yield
  end

  def self.sleep(sec)
    EM::Synchrony.sleep(sec)
  end

  def initialize(&blk)
    super

    # Schedule run in next tick
    EM.next_tick { resume }
  end

  def join
    self.class.pass while alive?
  end
end