summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/eventmachine-1.2.7/lib/em/tick_loop.rb
blob: a95d51614d33005474fb1c90765e08a1309cc989 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module EventMachine
  # Creates and immediately starts an EventMachine::TickLoop
  def self.tick_loop(*a, &b)
    TickLoop.new(*a, &b).start
  end

  # A TickLoop is useful when one needs to distribute amounts of work
  # throughout ticks in order to maintain response times. It is also useful for
  # simple repeated checks and metrics.
  # @example
  #    # Here we run through an array one item per tick until it is empty, 
  #    # printing each element.
  #    # When the array is empty, we return :stop from the callback, and the
  #    # loop will terminate.
  #    # When the loop terminates, the on_stop callbacks will be called.  
  #    EM.run do
  #      array = (1..100).to_a
  #      
  #      tickloop = EM.tick_loop do
  #        if array.empty?
  #          :stop
  #        else
  #          puts array.shift
  #        end
  #      end
  #      
  #      tickloop.on_stop { EM.stop }
  #    end
  #
  class TickLoop

    # Arguments: A callback (EM::Callback) to call each tick. If the call
    # returns +:stop+ then the loop will be stopped. Any other value is 
    # ignored.
    def initialize(*a, &b)
      @work = EM::Callback(*a, &b)
      @stops = []
      @stopped = true
    end

    # Arguments: A callback (EM::Callback) to call once on the next stop (or
    # immediately if already stopped).
    def on_stop(*a, &b)
      if @stopped
        EM::Callback(*a, &b).call
      else
        @stops << EM::Callback(*a, &b)
      end
    end

    # Stop the tick loop immediately, and call it's on_stop callbacks.
    def stop
      @stopped = true
      until @stops.empty?
        @stops.shift.call
      end
    end

    # Query if the loop is stopped.
    def stopped?
      @stopped
    end

    # Start the tick loop, will raise argument error if the loop is already
    # running.
    def start
      raise ArgumentError, "double start" unless @stopped
      @stopped = false
      schedule
    end

    private
    def schedule
      EM.next_tick do
        next if @stopped
        if @work.call == :stop
          stop
        else
          schedule
        end
      end
      self
    end
  end
end