oldmoe-reactor 0.2.0 → 0.2.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.
Files changed (3) hide show
  1. data/lib/reactor.rb +26 -2
  2. data/reactor.gemspec +1 -1
  3. metadata +1 -1
data/lib/reactor.rb CHANGED
@@ -107,7 +107,6 @@ module Reactor
107
107
  # object is already attached. If it is set to true (default) then the reactor will
108
108
  # append the new call back till the original caller detaches it. If set to false
109
109
  # then the reactor will just override the old callback with the new one
110
-
111
110
  def attach(mode, io, wait_if_attached = true, &callback)
112
111
  selectables = @selectables[mode] || raise("mode is not :read or :write")
113
112
  raise "you must supply a callback block" if callback.nil?
@@ -118,6 +117,14 @@ module Reactor
118
117
  selectables[:callbacks][io.object_id] = [callback]
119
118
  selectables[:dirty] = true
120
119
  end
120
+ @on_attach.call(mode, io) if @on_attach
121
+ end
122
+
123
+ # Supply a callback to on_attach that will be called
124
+ # after each attach operation and supplied with
125
+ # the mode and io object of the attach method
126
+ def on_attach(&block)
127
+ @on_attach = block
121
128
  end
122
129
 
123
130
  # Detach an IO object from the reactor
@@ -125,15 +132,23 @@ module Reactor
125
132
  # mode can be either :read or :write
126
133
  def detach(mode, io, force=false)
127
134
  selectables = @selectables[mode] || raise("mode is not :read or :write")
128
- if !force && selectables[:callbacks][io.object_id].length > 0
135
+ if !force && selectables[:callbacks][io.object_id].length > 1
129
136
  selectables[:callbacks][io.object_id].shift
130
137
  else
131
138
  selectables[:ios].delete(io.object_id)
132
139
  selectables[:callbacks].delete(io.object_id)
133
140
  selectables[:dirty] = true
134
141
  end
142
+ @on_detach.call(mode, io) if @on_attach
135
143
  end
136
144
 
145
+ # Supply a callback to on_detach that will be called
146
+ # after each detach operation and supplied with
147
+ # the mode and io object of the detach method
148
+ def on_detach(&block)
149
+ @on_detach = block
150
+ end
151
+
137
152
  # Detach all IO objects of a certain mode from the reactor
138
153
  #
139
154
  # mode can be either :read or :write
@@ -152,6 +167,15 @@ module Reactor
152
167
  # Add a block of code that will fire after some time
153
168
  def add_timer(time, periodical=false, &block)
154
169
  timer = Timer.new(@timers, time, periodical, &block)
170
+ @on_add_timer.call(timer) if @on_add_timer
171
+ timer
172
+ end
173
+
174
+ # Supply a callback to on_add_timer that will be called
175
+ # after each add_timer operation and supplied with
176
+ # the timer object
177
+ def on_add_timer(&block)
178
+ @on_add_timer = block
155
179
  end
156
180
 
157
181
  # Add a block of code that will fire periodically after some time passes
data/reactor.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "reactor"
3
- s.version = "0.2.0"
3
+ s.version = "0.2.1"
4
4
  s.date = "2009-07-04"
5
5
  s.summary = "A pure Ruby reactor library"
6
6
  s.email = "oldmoe@gmail.com"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oldmoe-reactor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Muhammad A. Ali