|
| 1 | +module UV |
| 2 | + def self.sleep sec |
| 3 | + current_yarn.sleep sec |
| 4 | + end |
| 5 | + |
| 6 | + def self.quote cmd |
| 7 | + current_yarn.quote(cmd) |
| 8 | + end |
| 9 | + |
| 10 | + def self.current_yarn |
| 11 | + ret = UV.current_loop.current_yarn |
| 12 | + raise "yarn not running" if ret.nil? |
| 13 | + ret |
| 14 | + end |
| 15 | + |
| 16 | + class Loop |
| 17 | + attr_reader :current_yarn |
| 18 | + |
| 19 | + def current_yarn=(y) |
| 20 | + raise "cannot double run yarn" if current_yarn |
| 21 | + raise "not yarn" unless y.kind_of? UV::Yarn |
| 22 | + |
| 23 | + @current_yarn = y |
| 24 | + end |
| 25 | + |
| 26 | + def clear_current_yarn(y) |
| 27 | + raise 'cannot clear yarn' if y != @current_yarn |
| 28 | + @current_yarn = nil |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + class Yarn |
| 33 | + def initialize(loop = UV.current_loop, &blk) |
| 34 | + @fiber = Fiber.new(&blk) |
| 35 | + @loop = loop |
| 36 | + end |
| 37 | + |
| 38 | + attr_reader :loop, :error, :req |
| 39 | + |
| 40 | + def ended?; @fiber.nil? end |
| 41 | + def result |
| 42 | + raise "not ended" unless ended? |
| 43 | + @result |
| 44 | + end |
| 45 | + |
| 46 | + def check_error |
| 47 | + raise @error if @error |
| 48 | + end |
| 49 | + |
| 50 | + def timer |
| 51 | + @timer ||= UV::Timer.new |
| 52 | + # raise 'timer already used' if @timer.active? |
| 53 | + @timer |
| 54 | + end |
| 55 | + |
| 56 | + def to_proc |
| 57 | + @proc ||= Proc.new{|*args, &blk| self.resume(*args, &blk) } |
| 58 | + end |
| 59 | + |
| 60 | + def start(*args) |
| 61 | + raise "already started" if @started |
| 62 | + @started = true |
| 63 | + |
| 64 | + resume(*args) |
| 65 | + end |
| 66 | + |
| 67 | + def sleep sec |
| 68 | + timer.start(sec * 1000.0, 0, &self.to_proc) |
| 69 | + Fiber.yield(timer, self) |
| 70 | + end |
| 71 | + |
| 72 | + def quote cmd |
| 73 | + out = UV::Pipe.new false |
| 74 | + ps = Process.new file: :sh, args: ['-c', cmd], stdio: [nil, out, nil] |
| 75 | + y = self |
| 76 | + ps.spawn do |x, sig| |
| 77 | + str = '' |
| 78 | + out.read_start do |b| |
| 79 | + if b.kind_of? String |
| 80 | + str.concat b |
| 81 | + next |
| 82 | + end |
| 83 | + y.resume(str) |
| 84 | + out.close |
| 85 | + end |
| 86 | + end |
| 87 | + Fiber.yield ps, self |
| 88 | + end |
| 89 | + |
| 90 | + def resume(*args) |
| 91 | + raise "cannot resume unstarted yarn" unless @started |
| 92 | + raise "cannot run ended yarn" if ended? |
| 93 | + |
| 94 | + @loop.current_yarn = self |
| 95 | + |
| 96 | + prev_loop = UV.current_loop |
| 97 | + @loop.make_current |
| 98 | + |
| 99 | + *ret = @fiber.resume(*args) |
| 100 | + |
| 101 | + if @fiber.alive? |
| 102 | + raise "invalid yield #{ret.inspect}" unless ALIVE_CLASSES.any?{|v| ret.first.kind_of? v } |
| 103 | + else |
| 104 | + @fiber = nil |
| 105 | + @result = ret |
| 106 | + end |
| 107 | + |
| 108 | + rescue => e |
| 109 | + @error = e |
| 110 | + |
| 111 | + ensure |
| 112 | + @loop.clear_current_yarn self |
| 113 | + prev_loop.make_current if prev_loop |
| 114 | + end |
| 115 | + |
| 116 | + ALIVE_CLASSES = [UV::Req, UV::Timer, UV::Process] |
| 117 | + end |
| 118 | +end |
0 commit comments