wires 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/wires/hub.rb +14 -8
  3. data/lib/wires/time.rb +85 -55
  4. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b10bb4a5abcff971b03e3ed715141508e21059b8
4
- data.tar.gz: b2e95646c96b287a988ed2f0608b7b7159bce712
3
+ metadata.gz: 07b54cd8c5d2566dc1df5dbbba359680e290cb44
4
+ data.tar.gz: 32392ed09eae7079456c55ace22840b5d504aa55
5
5
  SHA512:
6
- metadata.gz: b92ae83348fd8024b84324ab06d7f73978ffc516738201519e49898dd2ebd8469529792f99dbf7e3a4d1a0d26eb49bcace82e42cf4dabad279490b9d91c8ba70
7
- data.tar.gz: 788c6c1ef24826e7b94661f0b5a7db4f30bcd83584e48c41c6438bd0e900f54dd7ad441702629723783c3218b66d2883b3edc0baa5eeab7aa4b1f9a5af8e17fe
6
+ metadata.gz: ec3dc587a26c0373cb9b8fd9872208608b6bb5a3f2445dcfd398623ab108b1e7da3205864cb0e15d6911a982ec9ad594e5e06d3975b1bbde075a03532037a3f6
7
+ data.tar.gz: 5264ed0c4c84c2063e62e32876b1e40acd38587e52e9c40af05bb301ed9e381555c3d7a3fe588a4b0f48fedf149b44235b54db533d107db0054139844ca97798
data/lib/wires/hub.rb CHANGED
@@ -20,8 +20,11 @@ class Hub
20
20
  # Start the Hub event loop in a new thread
21
21
  def run
22
22
  if not @running
23
- @running = true
24
- @thread = Thread.new() {self.send(:run_loop)};
23
+ @thread = Thread.new() do
24
+ @running = true
25
+ self.send(:run_loop)
26
+ end
27
+
25
28
  at_exit { @thread.join if not $! }
26
29
  end
27
30
  nil end
@@ -59,11 +62,14 @@ class Hub
59
62
  @after_kills << func
60
63
  end
61
64
 
62
- # Put x in the queue, and block until x is processed
65
+ # Put x in the queue, and block until x is processed (if Hub is running)
63
66
  def fire(x)
64
- @queue << [x, Thread.current]
65
- # yield to event loop thread until awoken by it later
66
- sleep unless not @running
67
+ if @running # yield to event loop thread until awoken by it later
68
+ @queue << [x, Thread.current]
69
+ sleep
70
+ else # don't wait if Hub isn't running - would cause lockup
71
+ @queue << [x, nil]
72
+ end
67
73
  end
68
74
  def <<(x); fire(x); end
69
75
 
@@ -87,9 +93,9 @@ class Hub
87
93
  string, event, blocking, proc = x
88
94
  Thread.new do
89
95
  begin
90
- waiting_thread.wakeup unless blocking
96
+ waiting_thread.wakeup unless blocking or not waiting_thread
91
97
  proc.call($event = event)
92
- waiting_thread.wakeup if blocking
98
+ waiting_thread.wakeup if blocking and waiting_thread
93
99
 
94
100
  rescue Interrupt, SystemExit => e
95
101
  @running = false
data/lib/wires/time.rb CHANGED
@@ -1,75 +1,105 @@
1
1
 
2
2
 
3
- # class StartSchedulerEvent < Event; end
3
+ class StartSchedulerEvent < Event; end
4
4
 
5
- # # A singleton class to schedule future firing of events
6
- # class TimeScheduler
7
- # @schedule = Array.new
8
- # @schedule_lock = Mutex.new
9
- # @grain = 0.2.seconds
5
+ # A singleton class to schedule future firing of events
6
+ class TimeScheduler
7
+ @schedule = Array.new
8
+ @schedule_lock = Mutex.new
9
+ @grain = 0.2.seconds
10
10
 
11
- # # Operate on the metaclass as a type of singleton pattern
12
- # class << self
11
+ # Operate on the metaclass as a type of singleton pattern
12
+ class << self
13
13
 
14
- # # Get or set the time grain from outside the class
15
- # attr_accessor :grain
14
+ # Get or set the time grain from outside the class
15
+ attr_accessor :grain
16
16
 
17
- # # Fire an event delayed by time value
18
- # def fire(time, event, channel='*')
19
- # if not time.is_a? Time
20
- # raise TypeError, "Expected #{time.inspect} to be an instance of Time."
21
- # end
17
+ # Fire an event delayed by time value
18
+ def fire(time, event, channel='*')
19
+ if not time.is_a? Time
20
+ raise TypeError, "Expected #{time.inspect} to be an instance of Time."
21
+ end
22
22
 
23
- # # Under mutex, push the event into the schedule and sort
24
- # @schedule_lock.synchronize do
25
- # @schedule << [time, event, channel]
26
- # @schedule.sort! { |a,b| a[0] <=> b[0] }
27
- # end
23
+ # Under mutex, push the event into the schedule and sort
24
+ @schedule_lock.synchronize do
25
+ @schedule << [time, event, channel]
26
+ @schedule.sort! { |a,b| a[0] <=> b[0] }
27
+ end
28
28
 
29
- # end
29
+ end
30
30
 
31
- # private
31
+ private
32
32
 
33
- # def main_loop
33
+ def main_loop
34
34
 
35
- # pending = Array.new
35
+ pending = Array.new
36
36
 
37
- # while true
37
+ while true
38
38
 
39
- # pending.clear
40
- # this_time = Time.now
39
+ pending.clear
40
+ this_time = Time.now
41
41
 
42
- # # Under mutex, pull any events that are ready
43
- # @schedule_lock.synchronize do
44
- # while ((not @schedule.empty?) and (this_time > @schedule[0][0]))
45
- # puts 'thing'
46
- # pending << @schedule.shift
47
- # end
48
- # end
42
+ # Under mutex, pull any events that are ready
43
+ @schedule_lock.synchronize do
44
+ while ((not @schedule.empty?) and (this_time > @schedule[0][0]))
45
+ pending << @schedule.shift
46
+ end
47
+ end
49
48
 
50
- # # Fire pending events
51
- # pending.each { |x| Channel(x[2]).fire(x[1]) }
49
+ # Fire pending events
50
+ pending.each { |x| Channel(x[2]).fire(x[1]) }
52
51
 
53
- # # Calculate the time to sleep based on the time left in the "grain"
54
- # sleep [@grain-(Time.now-this_time), 0].max
52
+ # Calculate the time to sleep based on the time left in the "grain"
53
+ sleep [@grain-(Time.now-this_time), 0].max
55
54
 
56
- # end
57
- # end
55
+ end
56
+ end
58
57
 
59
- # end
58
+ end
60
59
 
61
- # # Use fired event to only start scheduler when Hub is running
62
- # # This also gets the scheduler loop its own thread within the Hub's threads
63
- # on :start_scheduler, self do; main_loop; end;
64
- # Channel(self).fire(:start_scheduler)
65
-
66
- # # Channel(self).fire(:start_scheduler, blocking=true)
67
- # end
60
+ # Use fired event to only start scheduler when Hub is running
61
+ # This also gets the scheduler loop its own thread within the Hub's threads
62
+ on :start_scheduler, self do; main_loop; end;
63
+ Channel(self).fire(:start_scheduler)
64
+ end
65
+
66
+
67
+ # Reopen the Time class and add the fire method to enable nifty syntax like:
68
+ # 32.minutes.from_now.fire :event
69
+ class Time
70
+ def fire(*args)
71
+ TimeScheduler.fire(self, *args)
72
+ end
73
+ end
74
+
68
75
 
69
- # # Reopen the Time class and add the fire method to enable nifty syntax like:
70
- # # 32.minutes.from_now.fire :event
71
- # class Time
72
- # def fire(*args)
73
- # TimeScheduler.fire(self, *args)
74
- # end
75
- # end
76
+ # Reopen ActiveSupport::Duration to enable nifty syntax like:
77
+ # 32.minutes.from_now do some_stuff end
78
+ class TimeSchedulerAnonEvent < Event; end
79
+ class ActiveSupport::Duration
80
+
81
+ alias :__original_since :since
82
+ def since(*args, &block)
83
+ if block
84
+ on :time_scheduler_anon, block.object_id do block.call end
85
+ __original_since(*args).fire :time_scheduler_anon, block.object_id
86
+ nil
87
+ else
88
+ __original_since(*args)
89
+ end
90
+ end
91
+ alias :from_now :since
92
+
93
+ alias :__original_ago :ago
94
+ def ago(*args, &block)
95
+ if block
96
+ on :time_scheduler_anon, block.object_id do block.call end
97
+ __original_ago(*args).fire :time_scheduler_anon, block.object_id
98
+ nil
99
+ else
100
+ __original_ago(*args)
101
+ end
102
+ end
103
+ alias :until :ago
104
+
105
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wires
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe McIlvain
@@ -9,7 +9,21 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2013-06-27 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: An asynchronous (threaded) event routing framework in Ruby. Patch your
14
28
  objects together with wires. Inspired by the python 'circuits' framework.
15
29
  email: joe.eli.mac@gmail.com