Permalink
Browse files

Add mechanism for halting the current event queue.

  • Loading branch information...
1 parent ec6db9e commit ab4c45c0764dc46773e8f0d48dcdfe2c6d41dad6 @trq trq committed Oct 31, 2012
@@ -38,6 +38,13 @@
class Standard implements Template
{
/**
+ * Halted queue flag
+ *
+ * @var bool
+ */
+ protected $haltedQueue = false;
+
+ /**
* Store params
*
* @var array
@@ -60,6 +67,23 @@ public function __construct($name)
}
/**
+ * Set the halt queue flag to true
+ */
+ public function haltQueue()
+ {
+ $this->haltedQueue = true;
+ return $this;
+ }
+
+ /**
+ * Check to see if the haltQueue flag is true
+ */
+ public function isQueueHalted()
+ {
+ return $this->haltedQueue;
+ }
+
+ /**
* Set a param
*
* @param string $index
@@ -41,6 +41,16 @@
public function __construct($name);
/**
+ * Set the halt queue flag to true
+ */
+ public function haltQueue();
+
+ /**
+ * Check to see if the haltQueue flag is true
+ */
+ public function isQueueHalted();
+
+ /**
* Set a param
*
* @param string $key
@@ -260,6 +260,11 @@ public function trigger(EventInterface $event, Callable $callback = null)
if ($result instanceof EventInterface && $callback !== null) {
(new EventCallback($callback, $result))->call();
}
+
+ // Halt the queue ?
+ if ($result->isQueueHalted()) {
+ return;
+ }
}
}
}
@@ -80,6 +80,23 @@ public function testListenerCanListenToMultipleEvents()
$this->assertEquals(3, $r->out);
}
+ public function testCanHaltQueue()
+ {
+ $r = new \StdClass;
+ $r->out = 0;
+ (new Manager)
+ ->attach('a', function($e) use ($r) {
+ $r->out++;
+ return $e->haltQueue();
+ })
+ ->attach('a', function($e) use ($r) {
+ $r->out++;
+ })
+ ->trigger(new Event('a'));
+
+ $this->assertEquals(1, $r->out);
+ }
+
public function testListenerCanListenToAllEvents()
{
$r = new \StdClass;

0 comments on commit ab4c45c

Please sign in to comment.