wires 0.3.7 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95acdf9855630beb97ecabff1acb709c0251ad60
4
- data.tar.gz: 43bd14f65cd614891e42f561bf4056245c7008c4
3
+ metadata.gz: 486d20e1c2995dea50118010dd60c4b9c8a5bbd8
4
+ data.tar.gz: d93b97c600da203bfcabd55781b80f2e6b0190ab
5
5
  SHA512:
6
- metadata.gz: 474fb2acc06c4a06a19239cd56d7aa8a525bcd80d43cc1426babac2c0709343e9f009d8f7006d2ac2f8ea43f5ee18af8b00c094721c24ef9a98b0d8d97cd1614
7
- data.tar.gz: 5b3122f9bd2cf537209527ab3c672c0f5bb15a8aff194dc5db14d212375299da73344b2db3b6a06ac3f155e7c52f3a977bdc41c339bfb0b926209e1170e5adfd
6
+ metadata.gz: 204086554b9b668d2ca5d559d99e3abf0173c23de146fb7232d80c9cb728e2f2c0de2dd82cd8ac619e0a5bc010beaf6c6a83e1b0d09e5682b170f418cd8cf598
7
+ data.tar.gz: f392ff623d48525508a535ea280d1924ea214f3a9276c1351a7d68d661ee78f70a164185be9585b7f560e0310f47f8ea3aa2dc23a4a59a338cf79d65381d04a8
data/lib/wires/channel.rb CHANGED
@@ -64,31 +64,18 @@ module Wires
64
64
  end)
65
65
  end
66
66
 
67
- # Register hook to execute before fire - can call multiple times
68
- def self.before_fire(retain=false, &block)
69
- @before_fires ||= []
70
- @before_fires << [block, retain]
71
- nil end
72
-
73
- # Register hook to execute after fire - can call multiple times
74
- def self.after_fire(retain=false, &block)
75
- @after_fires ||= []
76
- @after_fires << [block, retain]
77
- nil end
78
-
79
- def self.run_hooks(hooks_sym, *exc_args)
80
- hooks = self.instance_variable_get(hooks_sym.to_sym)
81
- for hook in hooks
82
- proc, retain = hook
83
- proc.call(*exc_args)
84
- end if hooks
85
- nil end
86
-
87
- def self.clear_hooks(hooks_sym, force=false)
88
- hooks = self.instance_variable_get(hooks_sym.to_sym)
89
- self.instance_variable_set(hooks_sym.to_sym,
90
- (force ? [] : hooks.select{|h| h[1]})) if hooks
91
- nil end
67
+ # Add hook methods
68
+ class << self
69
+ include Hooks
70
+
71
+ def before_fire(*args, &proc)
72
+ add_hook(:@before_fire, *args, &proc)
73
+ end
74
+
75
+ def after_fire(*args, &proc)
76
+ add_hook(:@after_fire, *args, &proc)
77
+ end
78
+ end
92
79
 
93
80
  # Fire an event on this channel
94
81
  def fire(event, blocking:false)
@@ -100,7 +87,7 @@ module Wires
100
87
  # Create an instance object from one of several acceptable input forms
101
88
  event = Event.new_from event
102
89
 
103
- self.class.run_hooks(:@before_fires, event, self)
90
+ self.class.run_hooks(:@before_fire, event, self)
104
91
 
105
92
  # Fire to each relevant target on each channel
106
93
  for chan in relevant_channels()
@@ -113,10 +100,15 @@ module Wires
113
100
  backtrace) # captured backtrace
114
101
  end end end
115
102
 
116
- self.class.run_hooks(:@after_fires, event, self)
103
+ self.class.run_hooks(:@after_fire, event, self)
117
104
 
118
105
  nil end
119
106
 
107
+ # Fire a blocking event on this channel
108
+ def fire_and_wait(event)
109
+ self.fire(event, blocking:true)
110
+ end
111
+
120
112
  # Convert events to array of unique codestrings
121
113
  def _normalize_event_list(events)
122
114
  events = [events] unless events.is_a? Array
@@ -168,8 +160,8 @@ module Wires
168
160
  end
169
161
 
170
162
  hub.before_kill(true) do
171
- self.clear_hooks(:@before_fires)
172
- self.clear_hooks(:@after_fires)
163
+ self.clear_hooks(:@before_fire)
164
+ self.clear_hooks(:@after_fire)
173
165
  end
174
166
 
175
167
  end
data/lib/wires/hub.rb CHANGED
@@ -76,25 +76,25 @@ module Wires
76
76
  sleep 0 until request_state :dead unless (flags.include? :nonblocking)
77
77
  nil end
78
78
 
79
- # Register hook to execute before run - can call multiple times
80
- def before_run(retain=false, &block)
81
- @before_runs << [block, retain]
82
- nil end
79
+ # Add hook methods
80
+ include Hooks
83
81
 
84
- # Register hook to execute after run - can call multiple times
85
- def after_run(retain=false, &block)
86
- @after_runs << [block, retain]
87
- nil end
82
+ def before_run(*args, &proc)
83
+ add_hook(:@before_run, *args, &proc)
84
+ end
88
85
 
89
- # Register hook to execute before kill - can call multiple times
90
- def before_kill(retain=false, &block)
91
- @before_kills << [block, retain]
92
- nil end
86
+ def after_run(*args, &proc)
87
+ add_hook(:@after_run, *args, &proc)
88
+ end
89
+
90
+ def before_kill(*args, &proc)
91
+ add_hook(:@before_kill, *args, &proc)
92
+ end
93
+
94
+ def after_kill(*args, &proc)
95
+ add_hook(:@after_kill, *args, &proc)
96
+ end
93
97
 
94
- # Register hook to execute after kill - can call multiple times
95
- def after_kill(retain=false, &block)
96
- @after_kills << [block, retain]
97
- nil end
98
98
 
99
99
  def on_neglect(&block)
100
100
  @on_neglect=block
@@ -243,35 +243,6 @@ module Wires
243
243
  end
244
244
  nil end
245
245
 
246
- # Flush/run queue of [proc, retain]s, retaining those with retain==true
247
- def run_hooks(hooks, *args)
248
- retained = Queue.new
249
- while not hooks.empty?
250
- proc, retain = hooks.shift
251
- retained << [proc, retain] if retain
252
- proc.call(*args)
253
- end
254
- while not retained.empty?
255
- hooks << retained.shift
256
- end
257
- nil end
258
-
259
- # Run queue of [proc, retain]s, retaining all
260
- def run_fire_hooks(hooks, *args)
261
- for hook in hooks
262
- proc, retain = hook
263
- proc.call(*args)
264
- end
265
- nil end
266
-
267
- # Clear queue of [proc, retain]s, retaining those with retain==true
268
- def clear_fire_hooks(hooks, *args)
269
- hooks.select! do |hook|
270
- proc, retain = hook
271
- retain
272
- end
273
- nil end
274
-
275
246
  end
276
247
 
277
248
 
@@ -299,20 +270,18 @@ module Wires
299
270
 
300
271
  declare_state :dead do
301
272
  transition_to :alive do
302
- before { run_hooks @before_runs }
303
- after { run_hooks @after_runs }
273
+ before { flush_hooks :@before_run }
274
+ after { flush_hooks :@after_run }
304
275
  end
305
276
  end
306
277
 
307
278
  declare_state :alive do
308
279
  transition_to :dead do
309
- before { run_hooks @before_kills }
280
+ before { flush_hooks :@before_kill }
310
281
  before { purge_neglected }
311
282
  before { join_children if @please_finish_all }
312
283
  after { @please_finish_all = false }
313
- after { run_hooks @after_kills }
314
- after { clear_fire_hooks @before_fires }
315
- after { clear_fire_hooks @after_fires }
284
+ after { flush_hooks :@after_kill }
316
285
  end
317
286
  end
318
287
 
File without changes
@@ -0,0 +1,56 @@
1
+
2
+ module Wires
3
+ module Hooks
4
+
5
+ # Register a hook - can be called multiple times if retain is true
6
+ def add_hook(hooks_sym, retain=false, &proc)
7
+ hooks = instance_variable_get(hooks_sym.to_sym)
8
+ if hooks
9
+ hooks << [proc, retain]
10
+ else
11
+ instance_variable_set(hooks_sym.to_sym, [[proc, retain]])
12
+ end
13
+ proc
14
+ end
15
+
16
+ # Remove a hook by proc reference
17
+ def remove_hook(hooks_sym, &proc)
18
+ hooks = instance_variable_get(hooks_sym.to_sym)
19
+ return unless hooks
20
+ hooks.reject! {|h| h[0]==proc}
21
+ end
22
+
23
+ # Run all hooks, deleting those not marked for retention
24
+ def run_hooks(hooks_sym, *exc_args)
25
+ hooks = instance_variable_get(hooks_sym.to_sym)
26
+ return unless hooks
27
+ for hook in hooks
28
+ proc, retain = hook
29
+ proc.call(*exc_args)
30
+ end
31
+ nil end
32
+
33
+ # Clear hooks not marked for retention (or all hooks if force)
34
+ def clear_hooks(hooks_sym, force=false)
35
+ hooks = instance_variable_get(hooks_sym.to_sym)
36
+ return unless hooks
37
+ (force ? hooks.clear : hooks.select!{|h| h[1]})
38
+ nil end
39
+
40
+ # Flush/run all hooks, keeping only those marked for retention
41
+ def flush_hooks(hooks_sym, *exc_args)
42
+ hooks = instance_variable_get(hooks_sym.to_sym)
43
+ return unless hooks
44
+ retained = Queue.new
45
+ while not hooks.empty?
46
+ proc, retain = hooks.shift
47
+ retained << [proc, retain] if retain
48
+ proc.call(*exc_args)
49
+ end
50
+ while not retained.empty?
51
+ hooks << retained.shift
52
+ end
53
+ end
54
+
55
+ end
56
+ end
data/lib/wires.rb CHANGED
@@ -4,7 +4,9 @@ require 'active_support/core_ext' # Convenience functions from Rails
4
4
  require 'threadlock' # Easily add re-entrant lock to instance methods
5
5
  require 'hegemon' # State machine management
6
6
 
7
- require 'wires/expect_type'
7
+ require 'wires/util/expect_type'
8
+ require 'wires/util/hooks'
9
+
8
10
  require 'wires/event'
9
11
  require 'wires/hub'
10
12
  require 'wires/channel'
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.3.7
4
+ version: 0.3.8
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-09-02 00:00:00.000000000 Z
11
+ date: 2013-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport-core-ext
@@ -105,8 +105,9 @@ files:
105
105
  - lib/wires/time.rb
106
106
  - lib/wires/hub.rb
107
107
  - lib/wires/core_ext.rb
108
- - lib/wires/expect_type.rb
109
108
  - lib/wires/clean.rb
109
+ - lib/wires/util/expect_type.rb
110
+ - lib/wires/util/hooks.rb
110
111
  - lib/wires/convenience.rb
111
112
  - lib/wires/channel.rb
112
113
  - lib/wires/event.rb