event-framework 1.0.0
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 +7 -0
- data/lib/event-framework.rb +218 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3cbda10e80b6df93d13f522cefa9ed3abb171275
|
4
|
+
data.tar.gz: 93f72136192172841a40c7bbaae3fd16c95a331d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f62d9fcb4efebc163969330c73e1711d4d874b670abd346d643943516c5493b1bae7465342ca740d51d29cb5a089721df13595c8df21fdd2ab69edf7e6ccc651
|
7
|
+
data.tar.gz: e19680345421584544dd0562781573a4ef2b9be0ce59d68c5bf6e4411eaaf7c5e005287184248a93836a7d4514a28f7bcb3448559045eb9770acb8a967ab89c0
|
@@ -0,0 +1,218 @@
|
|
1
|
+
##
|
2
|
+
# = Event Framework
|
3
|
+
#
|
4
|
+
# Event Framework is a minimalistic library providing publish–subscribe pattern
|
5
|
+
#
|
6
|
+
# === Example
|
7
|
+
#
|
8
|
+
# require_relative 'event-framework'
|
9
|
+
#
|
10
|
+
# class Server
|
11
|
+
# include EF::Object
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# class Client
|
15
|
+
# include EF::Object
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# server = Server.new
|
19
|
+
# client = Client.new
|
20
|
+
#
|
21
|
+
# EF::Thread.new do
|
22
|
+
# loop do
|
23
|
+
# sleep 1
|
24
|
+
# server.trigger('event', 'message')
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# EF::Thread.new do
|
29
|
+
# client.listen_to(server, 'event') do |server, message|
|
30
|
+
# puts message
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# EF::Loop.loop
|
35
|
+
#
|
36
|
+
# === Notices
|
37
|
+
#
|
38
|
+
# * EF::Object should be included after initialize
|
39
|
+
# * Callbacks will be executed in threads of subscribers (where they were defined)
|
40
|
+
module EF
|
41
|
+
##
|
42
|
+
# === provides threads with separated event loops
|
43
|
+
class Thread < ::Thread
|
44
|
+
##
|
45
|
+
# returns instances of EF::Thread
|
46
|
+
def self.instances
|
47
|
+
ObjectSpace.each_object(self).to_a
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# creates a thread <br>
|
52
|
+
# parameters will be passed to the block
|
53
|
+
def initialize(*args, &block)
|
54
|
+
@queue = Queue.new
|
55
|
+
|
56
|
+
super do
|
57
|
+
block.call *args if block_given?
|
58
|
+
|
59
|
+
while true
|
60
|
+
args, block = @queue.pop
|
61
|
+
block.call *args
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# adds a task which will be executed in the event loop <br>
|
68
|
+
# parameters will be passed to the block
|
69
|
+
def add(*args, &block)
|
70
|
+
raise 'block not given' unless block_given?
|
71
|
+
@queue << [args, block]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# === provides blocking event loop
|
77
|
+
module Loop
|
78
|
+
@@thread = nil
|
79
|
+
|
80
|
+
##
|
81
|
+
# starts the loop
|
82
|
+
def self.loop
|
83
|
+
@@thread = Thread.new
|
84
|
+
@@thread.value
|
85
|
+
end
|
86
|
+
|
87
|
+
##
|
88
|
+
# returns corresponding EF::Thread object <br>
|
89
|
+
# which you can use to add new tasks
|
90
|
+
def self.thread
|
91
|
+
@@thread
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# === provides methods for publish–subscribe pattern
|
97
|
+
module Object
|
98
|
+
##
|
99
|
+
# patches initialize to admix needed variables
|
100
|
+
def self.included(base)
|
101
|
+
base.class_exec do
|
102
|
+
alias alias_initialize initialize
|
103
|
+
|
104
|
+
##
|
105
|
+
# defines needed variables and calls original initialize
|
106
|
+
def initialize(*args, &block)
|
107
|
+
alias_initialize *args, &block
|
108
|
+
|
109
|
+
@mutex = Mutex.new
|
110
|
+
|
111
|
+
@thread = Thread.current
|
112
|
+
|
113
|
+
@observers = []
|
114
|
+
@observables = []
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
##
|
120
|
+
# returns the thread where the object was defined
|
121
|
+
def thread
|
122
|
+
@thread
|
123
|
+
end
|
124
|
+
|
125
|
+
##
|
126
|
+
# calls handlers for observers for the event <br>
|
127
|
+
# parameters and the caller will be passed to the handlers <br>
|
128
|
+
# notice: usually in threads of sibscribers
|
129
|
+
def trigger(event, *args)
|
130
|
+
raise 'event is not string' unless event.is_a? String
|
131
|
+
|
132
|
+
@mutex.synchronize do
|
133
|
+
@observers.each do |o, e, b|
|
134
|
+
if e == event
|
135
|
+
if Thread.instances.include? o.thread
|
136
|
+
o.thread.add self, *args, &b
|
137
|
+
elsif Loop.thread
|
138
|
+
Loop.thread.add self, *args, &b
|
139
|
+
else
|
140
|
+
b.call self, *args
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
##
|
148
|
+
# registrate a handler for the event
|
149
|
+
def listen_to(observable, event, &block)
|
150
|
+
raise 'observable is not EF::Object' unless observable.is_a? Object
|
151
|
+
raise 'event is not string' unless event.is_a? String
|
152
|
+
raise 'block not given' unless block_given?
|
153
|
+
|
154
|
+
@mutex.synchronize do
|
155
|
+
@observables << [observable, event, block]
|
156
|
+
observable.registrate(self, event, block)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
##
|
161
|
+
# listen to self
|
162
|
+
def on(event, &block)
|
163
|
+
raise 'event is not string' unless event.is_a? String
|
164
|
+
raise 'block not given' unless block_given?
|
165
|
+
|
166
|
+
@mutex.synchronize do
|
167
|
+
listen_to self, event, &block
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
##
|
172
|
+
# unregistrate all matching handlers
|
173
|
+
def stop_listening(observable=nil, event=nil, block=nil)
|
174
|
+
@mutex.synchronize do
|
175
|
+
observables = []
|
176
|
+
|
177
|
+
@observables.each do |o, e, b|
|
178
|
+
if (!observable || o == observable) && (!event || e == event) && (!block || b == block)
|
179
|
+
o.unregistrate(self, e, b)
|
180
|
+
else
|
181
|
+
observables << [o, e, b]
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
@observables = observables
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
##
|
190
|
+
# stop listening to self
|
191
|
+
def off(event=nil, block=nil)
|
192
|
+
@mutex.synchronize do
|
193
|
+
stop_listening self, event, block
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
protected
|
198
|
+
|
199
|
+
##
|
200
|
+
# adds observer to the list of observers
|
201
|
+
def registrate(observer, event, block)
|
202
|
+
@mutex.synchronize do
|
203
|
+
@observers << [observer, event, block]
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
##
|
208
|
+
# removes observer from the list of observers
|
209
|
+
def unregistrate(observer, event, block)
|
210
|
+
@mutex.synchronize do
|
211
|
+
@observers.reject! do |o, e, b|
|
212
|
+
o == observer && e == event && b == block
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: event-framework
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Speransky Danil
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Event Framework is a minimalistic library providing publish–subscribe
|
14
|
+
pattern
|
15
|
+
email: speranskydanil@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/event-framework.rb
|
21
|
+
homepage: http://speranskydanil.github.io/ruby-event-framework/
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.1.11
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Event Framework is a minimalistic library providing publish–subscribe pattern
|
45
|
+
test_files: []
|