Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Add priority parameter #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class Event
*/
protected $_callable = [];

/**
* @var array
*/
protected $_priorities = [];


/**
Expand Down Expand Up @@ -186,10 +190,23 @@ public static function unregister($eventId, $hard = false)
* @param mixed $callable Callable.
* @return \Hoa\Event\Event
*/
public function attach($callable)
public function attach($callable, $priority = 0)
{
$callable = xcallable($callable);
$this->_callable[$callable->getHash()] = $callable;
$callable = xcallable($callable);
$hash = $callable->getHash();
$this->_callable[$hash] = $callable;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can change the _callable data structure for a heap or a priority queue. You might take a look at SplHeap, SplMaxHeap, SplMinHeap or SplPriorityQueue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much better than array

$this->_priorities[$hash] = (int) $priority;

uksort($this->_callable, function ($a, $b) {
$a = $this->_priorities[$a];
$b = $this->_priorities[$b];

/*if (70000 <= PHP_VERSION_ID) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove this comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, first try to make compatibility with spaceship operator but it's parser issue.

return $this->_priorities[$ka] <=> $this->_priorities[$kb];
}*/

return ($a < $b) ? 1 : (($a > $b) ? -1 : 0);
});

return $this;
}
Expand Down
41 changes: 41 additions & 0 deletions Test/Unit/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,47 @@ public function case_attach()
->isIdenticalTo($event)
->boolean($event->isListened())
->isTrue();

$this
->given(
$eventId = 'hoa://Event/Test',
$source = new \Mock\Hoa\Event\Source(),
$bucket = new LUT\Bucket(),
$called = '',

SUT::register($eventId, $source),
SUT::getEvent($eventId)->attach(
function (LUT\Bucket $receivedBucket) use (&$called) {
$called .= '1';
}, 100
),
SUT::getEvent($eventId)->attach(
function (LUT\Bucket $receivedBucket) use (&$called) {
$called .= '2';
}, 10
),
SUT::getEvent($eventId)->attach(
function (LUT\Bucket $receivedBucket) use (&$called) {
$called .= '3';
}
),
SUT::getEvent($eventId)->attach(
function (LUT\Bucket $receivedBucket) use (&$called) {
$called .= '3';
}, 0
),
SUT::getEvent($eventId)->attach(
function (LUT\Bucket $receivedBucket) use (&$called) {
$called .= '4';
}, -1
)
)
->when($result = SUT::notify($eventId, $source, $bucket))
->then
->variable($result)
->isNull()
->string($called)
->isIdenticalTo('12334');
}

public function case_detach()
Expand Down