wires 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/wires/hub.rb +28 -29
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed84cacba5f8de403b014bf21dea7f17f715039d
|
4
|
+
data.tar.gz: 5db5c2bb5684593b81e0faf26dae9317d1c0bb55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
-
|
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
|
37
|
+
else process_item(@@queue.shift) end
|
17
38
|
end
|
18
39
|
end
|
19
40
|
|
20
|
-
def self.
|
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
|
-
|
51
|
+
unhandled_exception(e)
|
41
52
|
|
42
53
|
rescue Exception => e
|
43
|
-
|
54
|
+
unhandled_exception(e)
|
44
55
|
end
|
45
56
|
end
|
46
57
|
end
|
47
58
|
|
48
|
-
def self.
|
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
|
-
|