Skip to content

Commit dae21b7

Browse files
authored
Merge pull request #168 from mbonneau/priority-queue-patch
Remove cancelled items from PriorityQueue
2 parents bccbce9 + 6a4f9e7 commit dae21b7

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/Scheduler/PriorityQueue.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public function enqueue(ScheduledItem $item)
2222

2323
public function remove($item)
2424
{
25+
if ($this->count() === 0) {
26+
return false;
27+
}
28+
2529
if ($this->peek() === $item) {
2630
$this->dequeue();
2731
return true;

src/Scheduler/VirtualTimeScheduler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rx\Scheduler;
66

77
use Rx\AsyncSchedulerInterface;
8+
use Rx\Disposable\CallbackDisposable;
89
use Rx\Disposable\EmptyDisposable;
910
use Rx\Disposable\SerialDisposable;
1011
use Rx\DisposableInterface;
@@ -87,7 +88,10 @@ public function scheduleAbsoluteWithState($state, int $dueTime, callable $action
8788

8889
$this->queue->enqueue($scheduledItem);
8990

90-
return $scheduledItem->getDisposable();
91+
return new CallbackDisposable(function () use ($scheduledItem) {
92+
$scheduledItem->getDisposable()->dispose();
93+
$this->queue->remove($scheduledItem);
94+
});
9195
}
9296

9397
public function scheduleRelativeWithState($state, $dueTime, $action): DisposableInterface

test/Rx/Scheduler/PriorityQueueTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,21 @@ public function can_remove_scheduled_items_out_of_order()
130130
$this->assertSame($scheduledItem, $queue->dequeue());
131131
$this->assertSame($scheduledItem3, $queue->dequeue());
132132
}
133+
134+
/**
135+
* @test
136+
*/
137+
public function should_not_remove_nonexistent_item()
138+
{
139+
$queue = new PriorityQueue();
140+
$queue->remove(
141+
new ScheduledItem(
142+
$this->createMock(ScheduledItem::class),
143+
null,
144+
function () {
145+
},
146+
0
147+
)
148+
);
149+
}
133150
}

0 commit comments

Comments
 (0)