wires 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/wires/hub.rb +28 -29
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ddf7c26252b01261e552ad00ca7ae8dcc20515e8
4
- data.tar.gz: 667a3598ae40b52aab46a79a30126a036894429e
3
+ metadata.gz: ed84cacba5f8de403b014bf21dea7f17f715039d
4
+ data.tar.gz: 5db5c2bb5684593b81e0faf26dae9317d1c0bb55
5
5
  SHA512:
6
- metadata.gz: 9df8d83551265f447ae50263a7f0595439f26137dc926ea1c4cf6f660e052bb9be011dcf59debd88bcad453a0b4d1d85b99aceb1911cb768439726c7a1dd38e3
7
- data.tar.gz: 73a9116f9118c88194b47882df37309d8ee024fd7628a1bfe56c6d4a791bdcf0e65a62a092266905e6a922b15f87ee5cc58fae650cccc22086d339a84ee4ec97
6
+ metadata.gz: 18d0e590b738f66685730de3006f978a9faa22124a37e5511e798f909999ceb8b0bd41a72c96a9e18acf8c30a241865dcadbd2a0bca307a5bc7bf301bed57d72
7
+ data.tar.gz: e699ce9a9d99f7077f95c06c700e15c0b03ea2795d804da7b10f9f596d7b180cc278d0938660a2a887d9d9a252cc2949afdcbeb2ceab744c248020b85c43240b
data/lib/wires/hub.rb CHANGED
@@ -3,62 +3,61 @@
3
3
  def puts(x) $stdout.puts(x) end
4
4
 
5
5
 
6
- # An event hub. Event/proc associations come in, and the procs
6
+ # An Event Hub. Event/proc associations come in, and the procs
7
7
  # get called in new threads in the order received
8
8
  class Hub
9
9
  @@queue = Queue.new
10
10
 
11
- def self._run
11
+ # Start the Hub event loop in a new thread
12
+ def self.run
13
+ @@thread = Thread.new() {Hub.run_loop}
14
+ at_exit { @@thread.join if not $! }
15
+ end
16
+
17
+ # Start the Hub event loop in the current thread
18
+ def self.run_in_place() self.run_loop() end
19
+
20
+ # Kill the Hub event loop (softly)
21
+ def self.kill() @@keepgoing=false end
22
+
23
+ # Put x in the queue, and block until x is processed
24
+ def self.fire(x)
25
+ @@queue << [x, Thread.current]
26
+ sleep # yield to event loop thread until awoken by it later
27
+ end
28
+ def self.<<(x) fire(x) end
29
+
30
+ private
31
+
32
+ def self.run_loop
12
33
  @@keepgoing = true
13
34
 
14
35
  while @@keepgoing
15
36
  if @@queue.empty? then sleep(0)
16
- else _process_item(@@queue.shift) end
37
+ else process_item(@@queue.shift) end
17
38
  end
18
39
  end
19
40
 
20
- def self.run!
21
- @@thread = Thread.new() {Hub._run}
22
- at_exit { @@thread.join if not $! }
23
- end
24
-
25
- def self.run_in_place!() self._run() end
26
-
27
- def self.kill!() @@keepgoing=false end
28
-
29
- def self._process_item(x)
41
+ def self.process_item(x)
30
42
  x, waiting_thread = x
31
43
  string, event, proc = x
32
44
  Thread.new do
33
-
34
45
  begin
35
46
  waiting_thread.wakeup
36
47
  proc.call($event = event)
37
48
 
38
49
  rescue Interrupt, SystemExit => e
39
50
  @keepgoing = false
40
- _unhandled_exception(e)
51
+ unhandled_exception(e)
41
52
 
42
53
  rescue Exception => e
43
- _unhandled_exception(e)
54
+ unhandled_exception(e)
44
55
  end
45
56
  end
46
57
  end
47
58
 
48
- def self._unhandled_exception(x)
59
+ def self.unhandled_exception(x)
49
60
  $stderr.puts $!
50
61
  $stderr.puts $@
51
62
  end
52
-
53
- def self.fire(x)
54
- @@queue << [x, Thread.current]
55
- sleep
56
- end
57
-
58
- def self.enqueue(x) fire(x) end
59
- def self.<<(x) fire(x) end
60
-
61
- private_class_method :new
62
63
  end
63
-
64
-
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe McIlvain