wires 0.1.0 → 0.1.1
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/channels.rb +26 -16
- data/lib/wires/hub.rb +24 -26
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ddf7c26252b01261e552ad00ca7ae8dcc20515e8
|
4
|
+
data.tar.gz: 667a3598ae40b52aab46a79a30126a036894429e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9df8d83551265f447ae50263a7f0595439f26137dc926ea1c4cf6f660e052bb9be011dcf59debd88bcad453a0b4d1d85b99aceb1911cb768439726c7a1dd38e3
|
7
|
+
data.tar.gz: 73a9116f9118c88194b47882df37309d8ee024fd7628a1bfe56c6d4a791bdcf0e65a62a092266905e6a922b15f87ee5cc58fae650cccc22086d339a84ee4ec97
|
data/lib/wires/channels.rb
CHANGED
@@ -4,12 +4,12 @@ def on(events, channels='*', &codeblock)
|
|
4
4
|
for channel in channels
|
5
5
|
Channel(channel).register(events, codeblock)
|
6
6
|
end
|
7
|
-
end
|
7
|
+
nil end
|
8
8
|
|
9
9
|
|
10
10
|
def fire(event, channel='*')
|
11
11
|
Channel(channel).fire(event)
|
12
|
-
end
|
12
|
+
nil end
|
13
13
|
|
14
14
|
|
15
15
|
def Channel(*args) Channel.new(*args) end
|
@@ -23,23 +23,26 @@ class Channel
|
|
23
23
|
@name = name
|
24
24
|
@target_list = Set.new
|
25
25
|
@@channel_list << self
|
26
|
-
end
|
26
|
+
nil end
|
27
27
|
|
28
28
|
# Ensure that there is only one instance of Channel per name
|
29
29
|
@@channel_list = Set.new
|
30
|
+
@@new_lock = Mutex.new
|
30
31
|
def self.new(*args, &block)
|
31
|
-
|
32
|
-
|
32
|
+
@@new_lock.synchronize do
|
33
|
+
(@@channel_list.select {|x|
|
34
|
+
(x.name==args[0] and x.name.class==args[0].class)} [0]) \
|
35
|
+
or super(*args, &block)
|
36
|
+
end
|
33
37
|
end
|
34
38
|
|
35
39
|
# Class-wide reference to the global channel and event hub
|
36
40
|
@@channel_star = Channel('*')
|
37
|
-
@@hub = Hub.new
|
38
41
|
|
39
42
|
# Register a proc to be triggered by an event on this channel
|
40
43
|
def register(events, proc)
|
41
44
|
|
42
|
-
if not proc then raise SyntaxError, \
|
45
|
+
if not proc.is_a?(Proc) then raise SyntaxError, \
|
43
46
|
"No Proc given to execute on event: #{events}" end
|
44
47
|
|
45
48
|
# Convert all events to strings
|
@@ -49,8 +52,7 @@ class Channel
|
|
49
52
|
events.uniq!
|
50
53
|
|
51
54
|
@target_list << [events, proc]
|
52
|
-
|
53
|
-
end
|
55
|
+
nil end
|
54
56
|
|
55
57
|
# Fire an event on this channel
|
56
58
|
def fire(_event)
|
@@ -74,10 +76,10 @@ class Channel
|
|
74
76
|
for chan in relevant_channels()
|
75
77
|
for target in chan.target_list
|
76
78
|
for string in target[0] & event.class.codestrings
|
77
|
-
|
79
|
+
Hub << [string, event, *target[1..-1]]
|
78
80
|
end end end
|
79
81
|
|
80
|
-
end
|
82
|
+
nil end
|
81
83
|
|
82
84
|
def relevant_channels
|
83
85
|
return @@channel_list if self==@@channel_star
|
@@ -88,11 +90,19 @@ class Channel
|
|
88
90
|
|
89
91
|
relevant = [@@channel_star, self]
|
90
92
|
for c in @@channel_list
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
93
|
+
for code in Event.from_codestring(self.name).codestrings
|
94
|
+
relevant << c if case c.name
|
95
|
+
when Regexp
|
96
|
+
code =~ c.name
|
97
|
+
when Event
|
98
|
+
code == c.name.class
|
99
|
+
when Class
|
100
|
+
c.name <= Event ?
|
101
|
+
code == c.name :
|
102
|
+
code == c.name.to_s
|
103
|
+
else
|
104
|
+
code == c.name.to_s
|
105
|
+
end
|
96
106
|
end
|
97
107
|
end
|
98
108
|
return relevant.uniq
|
data/lib/wires/hub.rb
CHANGED
@@ -3,28 +3,30 @@
|
|
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
|
7
|
+
# get called in new threads in the order received
|
6
8
|
class Hub
|
7
|
-
|
8
|
-
|
9
|
+
@@queue = Queue.new
|
10
|
+
|
11
|
+
def self._run
|
12
|
+
@@keepgoing = true
|
13
|
+
|
14
|
+
while @@keepgoing
|
15
|
+
if @@queue.empty? then sleep(0)
|
16
|
+
else _process_item(@@queue.shift) end
|
17
|
+
end
|
9
18
|
end
|
10
19
|
|
11
|
-
def self.
|
12
|
-
|
20
|
+
def self.run!
|
21
|
+
@@thread = Thread.new() {Hub._run}
|
22
|
+
at_exit { @@thread.join if not $! }
|
13
23
|
end
|
14
24
|
|
15
|
-
def
|
25
|
+
def self.run_in_place!() self._run() end
|
16
26
|
|
17
|
-
def
|
18
|
-
@keepgoing = true
|
19
|
-
|
20
|
-
while @keepgoing
|
21
|
-
if @queue.empty? then sleep(0)
|
22
|
-
else _process_item(@queue.shift) end
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
27
|
+
def self.kill!() @@keepgoing=false end
|
26
28
|
|
27
|
-
def _process_item(x)
|
29
|
+
def self._process_item(x)
|
28
30
|
x, waiting_thread = x
|
29
31
|
string, event, proc = x
|
30
32
|
Thread.new do
|
@@ -43,24 +45,20 @@ class Hub
|
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
46
|
-
def _unhandled_exception(x)
|
48
|
+
def self._unhandled_exception(x)
|
47
49
|
$stderr.puts $!
|
48
50
|
$stderr.puts $@
|
49
51
|
end
|
50
52
|
|
51
|
-
def fire(x)
|
52
|
-
|
53
|
+
def self.fire(x)
|
54
|
+
@@queue << [x, Thread.current]
|
53
55
|
sleep
|
54
56
|
end
|
55
57
|
|
56
|
-
def enqueue(x)
|
57
|
-
|
58
|
-
|
58
|
+
def self.enqueue(x) fire(x) end
|
59
|
+
def self.<<(x) fire(x) end
|
60
|
+
|
61
|
+
private_class_method :new
|
59
62
|
end
|
60
63
|
|
61
64
|
|
62
|
-
# Run the hub in a new thread and join it at main thread exit
|
63
|
-
# However, do not join if an exception caused the exit -
|
64
|
-
# Such an exception indicates usually an error in user code
|
65
|
-
__hub_thread = Thread.new() {Hub.new.run}
|
66
|
-
at_exit { __hub_thread.join if not $! }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wires
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe McIlvain
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An asynchronous (threaded) event routing framework in Ruby. Patch your
|
14
14
|
objects together with wires. Inspired by the python 'circuits' framework.
|