eventable 0.1.0.beta2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +26 -8
- data/lib/eventable/eventable.rb +12 -8
- data/lib/eventable/version.rb +1 -1
- metadata +6 -6
data/README.markdown
CHANGED
@@ -8,12 +8,15 @@ Provides an easy to use and understand event model. Other systems did way too mu
|
|
8
8
|
|
9
9
|
If you want a simple way to add events to your classes without a bunch of other unrelated IO stuff this is the solution for you. (If you want to monitor IO events check out EventMachine)
|
10
10
|
|
11
|
-
You might be saying, "What about Observable? Why not just use that?" The problem with observable is that it saves a reference to the observing object. If you drop and add a bunch of them you've got the potential for a huge memory leak
|
11
|
+
You might be saying, "What about Observable? Why not just use that?" The problem with observable is that it saves a reference to the observing object. If you drop and add a bunch of observers without removing them from the observed object you've got the potential for a huge memory leak (depending on the size of your listeners of course).
|
12
12
|
|
13
|
-
Eventable
|
13
|
+
With Eventable you don't have to worry about memory leaks because Eventable only make a reference to the listening object when it needs to talk to it. When it's done the reference goes out of scope and can be garbage collected.
|
14
|
+
|
15
|
+
Eventable will even automatically remove registered listeners when they get garbage collected. You can set up a listener and not worry about removing your event hook yourself; it's done for you.
|
14
16
|
|
15
|
-
Eventable
|
17
|
+
Eventable also allows for more fine-grain control than Observable. You can register for specific events instead of just saying, "Hey, let me know when anything changes."
|
16
18
|
|
19
|
+
##Examples##
|
17
20
|
This example shows the basics of using Eventable to add an event to a class and listen for that event. (Without threading it's a bit pointless):
|
18
21
|
|
19
22
|
require 'eventable'
|
@@ -142,16 +145,31 @@ This example shows you how you might actually use it in a multi-threaded environ
|
|
142
145
|
##Version History##
|
143
146
|
|
144
147
|
**2011.06.06**
|
145
|
-
Ver: 0.1.0
|
146
|
-
|
148
|
+
Ver: 0.1.0
|
149
|
+
Went crazy and just completely wrapped all calls that modify or read callbacks cache with an instance mutex.
|
150
|
+
|
151
|
+
From what I understand locks are really expensive in Ruby so I'll need to clean this up and do some real performance testing.
|
152
|
+
|
153
|
+
Note:
|
154
|
+
Releasing just to stop RubyGems.org from showing last beta instead of newest beta when there are only --pre versions available of a gem. I get why they do it, but it's annoying to have people downloading beta 1 when you're really on beta 2. Plus I need to start using it myself...
|
155
|
+
|
156
|
+
**2011.06.05**
|
157
|
+
Ver: 0.1.0.beta2
|
158
|
+
Wrapped #\_id2ref call in begin...rescue block.
|
159
|
+
Added thread synchronization to calls that modify or read callbacks cache.
|
160
|
+
|
161
|
+
**2011.06.05**
|
162
|
+
Ver: 0.1.0.beta1
|
163
|
+
Completely redesigned from naive first attempt.
|
147
164
|
|
148
165
|
**Added features**
|
149
166
|
|
150
|
-
Now includes RSpecs.
|
167
|
+
Now includes RSpecs.
|
151
168
|
|
152
169
|
Garbage collection safe:
|
153
|
-
|
154
|
-
*
|
170
|
+
|
171
|
+
* Won't keep references to listeners open.
|
172
|
+
* Automatically removes garbage collected listeners from event notifications.
|
155
173
|
|
156
174
|
|
157
175
|
**2011.06.04**
|
data/lib/eventable/eventable.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'thread'
|
2
|
+
|
1
3
|
# Incredibly simple framework for adding events
|
2
4
|
module Eventable
|
3
5
|
|
@@ -30,10 +32,11 @@ module Eventable
|
|
30
32
|
|
31
33
|
# When the event happens the class where it happens runs this
|
32
34
|
def fire_event(event, *return_value, &block)
|
33
|
-
return false unless @callbacks[event] && !@callbacks[event].empty?
|
34
|
-
|
35
35
|
# We don't want the callback array being altered when we're trying to read it
|
36
36
|
@mutex.synchronize{
|
37
|
+
|
38
|
+
return false unless @callbacks[event] && !@callbacks[event].empty?
|
39
|
+
|
37
40
|
@callbacks[event].each do |listener_id, callbacks|
|
38
41
|
begin
|
39
42
|
listener = ObjectSpace._id2ref(listener_id)
|
@@ -79,13 +82,12 @@ module Eventable
|
|
79
82
|
|
80
83
|
# Allows objects to stop listening to events
|
81
84
|
def unregister_for_event(args)
|
82
|
-
event = args[:event]
|
83
|
-
return unless @callbacks && @callbacks[event]
|
84
|
-
|
85
|
-
listener_id = args[:listener_id] || args[:listener].object_id
|
86
|
-
callback = args[:callback]
|
87
|
-
|
88
85
|
@mutex.synchronize {
|
86
|
+
event = args[:event]
|
87
|
+
return unless @callbacks && @callbacks[event]
|
88
|
+
|
89
|
+
listener_id = args[:listener_id] || args[:listener].object_id
|
90
|
+
callback = args[:callback]
|
89
91
|
@callbacks[event].delete_if do |listener, callbacks|
|
90
92
|
callbacks.delete(callback) if listener == listener_id
|
91
93
|
callbacks.empty?
|
@@ -100,3 +102,5 @@ module Eventable
|
|
100
102
|
end
|
101
103
|
|
102
104
|
end
|
105
|
+
|
106
|
+
|
data/lib/eventable/version.rb
CHANGED
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eventable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mike Bethany
|
@@ -13,7 +13,7 @@ date: 2011-06-06 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2156180860 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '2.6'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2156180860
|
25
25
|
description: Provides an easy to use and understand event model. If you want a simple
|
26
26
|
way to add events to your classes without a bunch of other unrelated IO stuff this
|
27
27
|
is the solution for you. (If you want to monitor IO events check out EventMachine)
|
@@ -63,9 +63,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
64
|
none: false
|
65
65
|
requirements:
|
66
|
-
- - ! '
|
66
|
+
- - ! '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: '0'
|
69
69
|
requirements: []
|
70
70
|
rubyforge_project:
|
71
71
|
rubygems_version: 1.8.5
|