view piecrust/events.py @ 1154:aad9b5a0a809

prepare: Add micro-blogging template.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 19 Jan 2019 17:41:56 -0800
parents f070a4fc033c
children
line wrap: on
line source


class Event(object):
    """ A simple implementation of a subscribable event.
    """
    def __init__(self):
        self._handlers = []

    def __iadd__(self, handler):
        self._handlers.append(handler)
        return self

    def __isub__(self, handler):
        self._handlers.remove(handler)
        return self

    def fire(self, *args, **kwargs):
        # Make a copy of the handlers list in case some handler removes
        # itself while executing.
        handlers = list(self._handlers)
        for handler in handlers:
            handler(*args, **kwargs)