[Zope3-dev] SPCC Alert: Simple event handlers
Jim Fulton
jim at zope.com
Tue May 25 08:10:21 EDT 2004
The Society for the Prevention of Cruelty to Chickens has issued an
alert for the INotifiable interface in the new event proposal:
http://dev.zope.org/Zope3/SimplifyEventSystem
INotifiable has a single method that is called without arguments and
that returns no value.
The motivation for this interface is that one should not expect
construction of adapters, used for representing event subscribers, to
produce side effects. This is a reasonable motivation, but it makes
the event system more complex that in should be. The most natural way
to express simple (the most common) event handlers is with Python
functions. Requiring that subscription adapters delay their work
until a notify method is called forces us to use a function wrapper:
class _FunctionNotifiable(object):
implements(INotifiable)
__slots__ = 'event', 'handler'
def notify(self):
return self.handler(self.event)
def Subscriber(handler):
factory = _FunctionNotifiable
def adapt(event):
r = factory()
r.event = event
r.hamdler = handler
return r
return adapt
This wrapper introduces:
- A dead chicken that must be provided for each subscriber
- Needless overhead
In the long term, I think we need to come to terms with the fact that
it will sometimes be useful to think of adapter factories as
multi-methods, which may do interesting things (e.g. produce side
effects) when they are called.
For now, I suggest that we:
- allow subscription adapters to be registered to provide None. An
adapter that produces None obviously does any work in it's
constructor. It is an indirect subroutine. Simple event handlers
will be callables (typically functions) that take arguments and
return None (or raise exceptions).
- provide an adapter service method, callSubscribers, that takes one
or more objects to be adapted and that gets all of the adapters
from those objects to None, thus causing the handler functions to
be called.
This will allow us to:
- Do away with INotifiable.
- Allow simple event handlers to be callables, typically functions.
If there are no very convincing objections, I will update the proposal
accordingly.
Jim
--
Jim Fulton mailto:jim at zope.com Python Powered!
CTO (540) 361-1714 http://www.python.org
Zope Corporation http://www.zope.com http://www.zope.org
More information about the Zope3-dev
mailing list