~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/longpoll/adapters/event.py

[r=abentley,
        rvb][no-qa] UpdatePreviewDiffJob now issues an ObjectModifiedEvent
        when it updates the preview_diff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
class LongPollEvent:
32
32
    """Base-class for event adapters.
33
33
 
34
 
    Sub-classes need to declare something along the lines of:
35
 
 
36
 
        adapts(Interface, Interface)
37
 
        implements(ILongPollEvent)
 
34
    Sub-classes need to define the `event_key` property and declare something
 
35
    along the lines of::
 
36
 
 
37
        class LongPollAwesomeThingEvent(LongPollEvent):
 
38
            adapts(IAwesomeThing)
 
39
            implements(ILongPollEvent)
 
40
 
 
41
    Alternatively, use the `long_poll_event` class decorator::
 
42
 
 
43
        @long_poll_event(IAwesomeThing)
 
44
        class LongPollAwesomeThingEvent(LongPollEvent):
 
45
            ...
 
46
 
 
47
    In both cases the adapter should be registered in a `configure.zcml`
 
48
    somewhere sensible::
 
49
 
 
50
        <adapter factory=".adapters.LongPollAwesomeThingEvent" />
38
51
 
39
52
    """
40
53
 
41
 
    def __init__(self, source, event):
 
54
    def __init__(self, source):
42
55
        self.source = source
43
 
        self.event = event
44
56
 
45
57
    @property
46
58
    def event_key(self):
47
59
        """See `ILongPollEvent`."""
48
60
        raise NotImplementedError(self.__class__.event_key)
49
61
 
50
 
    def emit(self, data):
51
 
        """See `ILongPollEvent`."""
52
 
        payload = {"event_key": self.event_key, "event_data": data}
53
 
        router = router_factory(self.event_key)
54
 
        router.send(payload)
 
62
    def emit(self, **data):
 
63
        """See `ILongPollEvent`.
 
64
 
 
65
        The data will be updated with `event_key`, a copy of `self.event_key`.
 
66
        """
 
67
        event_key = self.event_key
 
68
        data.update(event_key=event_key)
 
69
        router = router_factory(event_key)
 
70
        router.send(data)