Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event with same priority are fired in reverse order #44

Closed
Anahkiasen opened this issue Feb 20, 2015 · 4 comments
Closed

Event with same priority are fired in reverse order #44

Anahkiasen opened this issue Feb 20, 2015 · 4 comments

Comments

@Anahkiasen
Copy link
Member

Consider the following code:

$emitter = new Emitter();

$emitter->addListener('foobar', function () {
   echo 'a';
}, 50);

$emitter->addListener('foobar', function () {
    echo 'b';
}, 0);

$emitter->addListener('foobar', function () {
    echo 'c';
}, 0);

$emitter->addListener('foobar', function () {
   echo  'd';
}, -50);

$emitter->emit('foobar');

Now, as C was registered after B, I would expect it to fire after B too, but currently instead of abcd I get acbd. Is this expected behaviour? Is there any way to avoid this?

@dstockto
Copy link

I've opened a PR #45 which adds a new sort function that is stable. The emitter is using usort which is not stable so the order of elements that are deemed equivalent by the comparison function is not guaranteed to remain the same as in the original array. PR #45 should fix that and you'll get abcd.

@frankdejonge
Copy link
Member

Closing, explanation is here: #45

@frankdejonge
Copy link
Member

I've come back to this and ensured events are now emitted in order while respecting priority. Consider this fixed.

@Anahkiasen
Copy link
Member Author

Nice, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants