wires 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/wires/channel.rb +5 -1
- data/lib/wires/convenience.rb +15 -12
- data/lib/wires/hub.rb +6 -4
- data/lib/wires/time.rb +38 -18
- 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: 358d9d6b0b4094ed729bc94b80a5af81d2b6db25
|
4
|
+
data.tar.gz: 33eb37077d42bb24312b0a56ad910b4360396ee7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ffd7d42fa653093a03c07528487629192a1a8007733e300a8aac1eafb52746beccb10362b9ef215af400f81e538e5768a49bf8b17af07659d98ef73f10e53d0d
|
7
|
+
data.tar.gz: e99e2c37b935818c4641a2a381e19d9774846563b5f57adfd1f6baff51d87462c3363f36a365588a25cf9b6a1e3a25e2d96baafd9a0792d3acc5346032f84342
|
data/lib/wires/channel.rb
CHANGED
@@ -88,7 +88,11 @@ module Wires
|
|
88
88
|
for chan in relevant_channels()
|
89
89
|
for target in chan.target_list
|
90
90
|
for string in target[0] & event.class.codestrings
|
91
|
-
self.class.hub.spawn(event,
|
91
|
+
self.class.hub.spawn(event, # fired event object event
|
92
|
+
self.name, # name of channel fired from
|
93
|
+
target[1], # proc to execute
|
94
|
+
blocking, # boolean from blocking kwarg
|
95
|
+
backtrace) # captured backtrace
|
92
96
|
end end end
|
93
97
|
|
94
98
|
self.class.run_hooks(:@after_fires, event, self)
|
data/lib/wires/convenience.rb
CHANGED
@@ -3,29 +3,32 @@ module Wires
|
|
3
3
|
|
4
4
|
module Convenience
|
5
5
|
|
6
|
-
|
6
|
+
def Channel(*args) Channel.new(*args) end
|
7
7
|
|
8
8
|
def on(events, channels='*', &codeblock)
|
9
9
|
channels = [channels] unless channels.is_a? Array
|
10
10
|
for channel in channels
|
11
|
-
Channel.new(channel).
|
11
|
+
channel=Channel.new(channel) unless channel.is_a? Channel
|
12
|
+
channel.register(events, codeblock)
|
12
13
|
end
|
13
14
|
nil end
|
14
15
|
|
15
|
-
def fire(event, channel='*')
|
16
|
-
Channel.new(channel).
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
def fire(event, channel='*', **kwargs)
|
17
|
+
channel = Channel.new(channel) unless channel.is_a? Channel
|
18
|
+
unless kwargs[:time] or (kwargs[:count] and kwargs[:count]!=1)
|
19
|
+
channel.fire(event, **kwargs)
|
20
|
+
else
|
21
|
+
time = kwargs[:time] or Time.now
|
22
|
+
kwargs.reject!{|k,v| k==:time}
|
23
|
+
TimeScheduler.add(time, event, channel, **kwargs)
|
24
|
+
end
|
21
25
|
nil end
|
22
26
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
27
|
+
def fire_and_wait(*args, **kwargs)
|
28
|
+
kwargs[:blocking]=true
|
29
|
+
fire(*args, **kwargs)
|
26
30
|
end
|
27
31
|
|
28
|
-
def Channel(*args) Channel.new(*args) end
|
29
32
|
|
30
33
|
|
31
34
|
class << self
|
data/lib/wires/hub.rb
CHANGED
@@ -127,12 +127,13 @@ module Wires
|
|
127
127
|
return neglect(*args) if dead?
|
128
128
|
|
129
129
|
event, ch_string, proc, blocking, fire_bt = *args
|
130
|
-
*
|
130
|
+
*proc_args = event, ch_string
|
131
|
+
*exc_args = event, ch_string, fire_bt
|
131
132
|
|
132
133
|
# If blocking, run the proc in this thread
|
133
134
|
if blocking
|
134
135
|
begin
|
135
|
-
proc.call(
|
136
|
+
proc.call(*proc_args)
|
136
137
|
rescue Exception => exc
|
137
138
|
unhandled_exception(exc, *exc_args)
|
138
139
|
end
|
@@ -155,10 +156,11 @@ module Wires
|
|
155
156
|
# Start the new child thread; follow with chain of neglected tasks
|
156
157
|
new_thread = Thread.new do
|
157
158
|
begin
|
158
|
-
proc.call(
|
159
|
-
spawn_neglected_task_chain
|
159
|
+
proc.call(*proc_args)
|
160
160
|
rescue Exception => exc
|
161
161
|
unhandled_exception(exc, *exc_args)
|
162
|
+
ensure
|
163
|
+
spawn_neglected_task_chain
|
162
164
|
end
|
163
165
|
end
|
164
166
|
|
data/lib/wires/time.rb
CHANGED
@@ -9,8 +9,10 @@ module Wires
|
|
9
9
|
|
10
10
|
def initialize(time, event, channel='*',
|
11
11
|
interval:0.seconds, count:1,
|
12
|
-
ignore_past:false, cancel:false
|
12
|
+
ignore_past:false, cancel:false,
|
13
|
+
**kwargs)
|
13
14
|
|
15
|
+
time ||= Time.now
|
14
16
|
expect_type time, Time
|
15
17
|
|
16
18
|
@active = (not cancel)
|
@@ -28,10 +30,11 @@ module Wires
|
|
28
30
|
end
|
29
31
|
|
30
32
|
@time = time
|
31
|
-
@event = Event.new_from(event)
|
32
|
-
@channel = channel
|
33
33
|
@interval = interval
|
34
34
|
|
35
|
+
@event = Event.new_from(event)
|
36
|
+
@channel = Channel.new(channel) unless channel.is_a? Channel
|
37
|
+
@kwargs = kwargs
|
35
38
|
end
|
36
39
|
|
37
40
|
def active?; @active end
|
@@ -53,28 +56,28 @@ module Wires
|
|
53
56
|
def count_dec(x=1); self.count=(@count-x) end
|
54
57
|
|
55
58
|
# Fire the event now, regardless of time or active status
|
56
|
-
def fire(
|
57
|
-
|
59
|
+
def fire(**kwargs) # kwargs merge with and override @kwargs
|
60
|
+
@channel.fire(@event, **(@kwargs.merge(kwargs)))
|
58
61
|
count_dec
|
59
62
|
@time += @interval if @active
|
60
63
|
nil end
|
61
64
|
|
62
65
|
# Fire the event only if it is ready
|
63
|
-
def fire_if_ready(
|
66
|
+
def fire_if_ready(**args); self.fire(**kwargs) if ready? end
|
64
67
|
|
65
68
|
# Block until event is ready
|
66
69
|
def wait_until_ready; sleep 0 until ready? end
|
67
70
|
|
68
71
|
# Block until event is ready, then fire and block until it is done
|
69
|
-
def fire_when_ready(
|
72
|
+
def fire_when_ready(**kwargs);
|
70
73
|
wait_until_ready
|
71
|
-
fire(
|
74
|
+
self.fire(**kwargs)
|
72
75
|
end
|
73
76
|
|
74
77
|
# Lock (almost) all instance methods with common re-entrant lock
|
75
|
-
threadlock instance_methods-
|
76
|
-
|
77
|
-
|
78
|
+
threadlock instance_methods(false)-[\
|
79
|
+
:wait_until_ready,
|
80
|
+
:fire_when_ready]
|
78
81
|
end
|
79
82
|
|
80
83
|
# A singleton class to schedule future firing of events
|
@@ -90,12 +93,15 @@ module Wires
|
|
90
93
|
class << self
|
91
94
|
|
92
95
|
# Add an event to the schedule
|
93
|
-
def add(
|
94
|
-
|
96
|
+
def add(*args)
|
97
|
+
new_item = (args.first.is_a? TimeSchedulerItem) ?
|
98
|
+
(args.first) :
|
99
|
+
(TimeSchedulerItem.new(*args))
|
95
100
|
schedule_add(new_item)
|
96
101
|
nil end
|
102
|
+
|
97
103
|
# Add an event to the schedule using << operator
|
98
|
-
|
104
|
+
def <<(arg); add(*arg); end
|
99
105
|
|
100
106
|
# Get a copy of the event schedule from outside the class
|
101
107
|
def list; @schedule.clone end
|
@@ -114,12 +120,26 @@ module Wires
|
|
114
120
|
nil end
|
115
121
|
|
116
122
|
def schedule_add(new_item)
|
123
|
+
|
124
|
+
if new_item.ready?
|
125
|
+
loop do
|
126
|
+
new_item.fire
|
127
|
+
break unless new_item.ready?
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
117
131
|
if new_item.ready?(@next_pass)
|
118
|
-
Thread.new
|
119
|
-
|
120
|
-
|
121
|
-
|
132
|
+
Thread.new do
|
133
|
+
loop do
|
134
|
+
new_item.fire_when_ready(blocking:true)
|
135
|
+
break unless new_item.ready?(@next_pass)
|
136
|
+
end
|
137
|
+
end
|
122
138
|
end
|
139
|
+
|
140
|
+
@schedule << new_item
|
141
|
+
schedule_reshuffle
|
142
|
+
|
123
143
|
nil end
|
124
144
|
|
125
145
|
def schedule_concat(other_list)
|