Debian Codegraph @

README

raw
1 NAME
2
3 Event::Distributor - a simple in-process pub/sub mechanism
4
5 SYNOPSIS
6
7 use Event::Distributor;
8
9 my $dist = Event::Distributor->new;
10
11 $dist->declare_signal( "announce" );
12
13
14 $dist->subscribe_sync( announce => sub {
15 my ( $dist, $message ) = @_;
16 say $message;
17 });
18
19 $dist->subscribe_async( announce => sub {
20 my ( $dist, $message ) = @_;
21 return $async_http->POST( "http://server/message", $message );
22 });
23
24
25 $dist->fire_sync( announce => "Hello, world!" );
26
27 DESCRIPTION
28
29 Instances of this class provide a simple publish/subscribe mechanism
30 within a single process, for either synchronous or Future-based
31 asynchronous use.
32
33 A given instance has a set of named events. Subscribers are CODE
34 references attached to a named event. Publishers can declare the
35 existence of a named event, and then later invoke it by passing in
36 arguments, which are distributed to all of the subscribers of that
37 named event.
38
39 It is specifically not an error to request to subscribe an event that
40 has not yet been declared, in order to allow multiple modules of code
41 to be loaded and subscribe events the others publish, without
42 introducing loading order dependencies. An event only needs to be
43 declared by the time it is fired.
44
45 Natively all of the events provided by the distributor are
46 fully-asynchronous in nature. Each subscriber is expected to return a
47 Future instance which will indicate its completion; the results of
48 these are merged into a single future returned by the fire method
49 itself. However, to support synchronous or semi-synchronous programs
50 using it, both the observe and invoke methods also have a synchronous
51 variant. Note however, that this module does not provide any kind of
52 asynchronous detachment of synchronous functions; using the
53 "subscribe_sync" method to subscribe a long-running blocking function
54 will cause the fire_* methods to block until that method returns. To
55 achieve a truely-asynchronous experience the attached code will need to
56 use some kind of asynchronous event system.
57
58 This module is very-much a work-in-progress, and many ideas may still
59 be added or changed about it. It is the start of a concrete
60 implementaion of some of the ideas in my "Event-Reflexive Programming"
61 series of blog posts. See the "TODO" and "SEE ALSO" sections for more
62 detail.
63
64 EVENTS
65
66 Each of the events known by a distributor has a name. Conceptually each
67 also has a type. Currently there are three types of event, a "signal",
68 an "action", and a "query".
69
70 * A signal event simply informs subscribers that some event or
71 condition has occurred. Additional arguments can be passed from the
72 invoker to the subscribers, but subscriptions are not expected to
73 return a meaningful value, nor does firing this event return a value.
74 All subscriber functions are invoked sequentually and synchronously
75 by a fire_* method (though, of course, asynchronous subscribers
76 synchronously return a future instance, which allows them to continue
77 working asynchronously).
78
79 * An action event requires a single subscriber, and represents a
80 request from the invoker to the subscriber to perform some activity.
81 This behaves much like a regular (Future-returning) method call,
82 except that the indirection mechanism of the distributor allows a
83 more flexible method of connection between the two sides.
84
85 * A query event invokes subscriber code expecting a successful
86 return, returning the first result that is successful. If a
87 synchronous subscriber returns a result, or if an asynchronous one
88 returns a successful immediate Future, then no further subscribers
89 are invoked, and that result is taken immediately. Any other pending
90 Futures are then cancelled.
91
92 METHODS
93
94 declare_signal
95
96 $distributor->declare_signal( $name )
97
98 Declares a new "signal" event of the given name.
99
100 declare_action
101
102 $distributor->declare_action( $name )
103
104 Since version 0.04.
105
106 Declares a new "action" event of the given name.
107
108 declare_query
109
110 $distributor->declare_query( $name )
111
112 Since version 0.02.
113
114 Declares a new "query" event of the given name.
115
116 subscribe_async
117
118 $distributor->subscribe_async( $name, $code )
119
120 Adds a new CODE reference to the list of subscribers for the named
121 event. This subscriber is expected to return a Future that will
122 eventually yield its result.
123
124 When invoked the code will be passed the distributor object itself and
125 the list of arguments, and is expected to return a Future.
126
127 $f = $code->( $distributor, @args )
128
129 subscribe_sync
130
131 $distributor->subscribe_sync( $name, $code )
132
133 Adds a new CODE reference to the list of subscribers for the named
134 event. This subscriber is expected to perform its work synchronously
135 and return its result immediately.
136
137 In non-blocking or asynchronous applications, this method should only
138 be used for simple subscribers which can immediately return having
139 completed their work. If the work is likely to take some time by
140 blocking on external factors, consider instead using the
141 "subscribe_async" method.
142
143 When invoked the code will be passed the distributor object itself and
144 the list of arguments.
145
146 $code->( $distributor, @args )
147
148 fire_async
149
150 $f = $distributor->fire_async( $name, @args )
151
152 Invokes the named event, passing the arguments to the subscriber
153 functions. This function returns as soon as all the subscriber
154 functions have been invoked, returning a Future that will eventually
155 complete when all the futures returned by the subscriber functions have
156 completed.
157
158 fire_sync
159
160 $distributor->fire_sync( $name, @args )
161
162 Invokes the named event, passing the arguments to the subscriber
163 functions. This function synchronously waits until all the subscriber
164 futures have completed, and will return once they have all done so.
165
166 Note that since this method calls the get method on the Future instance
167 returned by "fire_async", it is required that this either be an
168 immediate, or be some subclass that can actually perform the await
169 operation. This should be the case if it is provided by an event
170 framework or similar, or custom application logic.
171
172 TODO
173
174 Some of these ideas appear in the "Event-Reflexive Progamming" series
175 of blog posts, and may be suitable for implementation here. All of
176 these ideas are simply for consideration; there is no explicit promise
177 that any of these will actually be implemented.
178
179 * Unsubscription from events.
180
181 * Define (or document the lack of) ordering between subscriptions of
182 a given event.
183
184 * Refine the failure-handling semantics of signals.
185
186 * Ability to invoke signals after the current one is finished, by
187 deferring the fire method. Should this be a new fire_* method, or a
188 property of the signal itself?
189
190 * More control over the semantics of value-returning events -
191 scatter/map/gather pattern.
192
193 * Sub-heirarchies of events.
194
195 * Subclasses for specific event frameworks (IO::Async).
196
197 * Subclasses (or other behaviours) for out-of-process event
198 serialisation and subscribers.
199
200 * Event parameter filtering mechanics - allows parametric
201 heirarchies, instrumentation logging, efficient out-of-process
202 subscribers.
203
204 SEE ALSO
205
206 Event-Reflexive Programming
207 <http://leonerds-code.blogspot.co.uk/search/label/event-reflexive>
208
209 AUTHOR
210
211 Paul Evans <[email protected]>
212