diff piecrust/events.py @ 3:f485ba500df3

Gigantic change to basically make PieCrust 2 vaguely functional. - Serving works, with debug window. - Baking works, multi-threading, with dependency handling. - Various things not implemented yet.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 10 Aug 2014 23:43:16 -0700
parents
children f070a4fc033c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/piecrust/events.py	Sun Aug 10 23:43:16 2014 -0700
@@ -0,0 +1,20 @@
+
+class Event(object):
+    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)
+