Skip to content

Commit 022ec34

Browse files
committed
Improve message box config
1 parent 320d547 commit 022ec34

6 files changed

Lines changed: 109 additions & 29 deletions

File tree

src/Api/MessageBox/MessageBoxCreateInfo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
final readonly class MessageBoxCreateInfo
88
{
99
public function __construct(
10-
public string $title,
11-
public string $text,
10+
public string $title = '',
11+
public string $text = '',
1212
public bool $cancel = false,
1313
public ?MessageBoxIcon $icon = null,
1414
) {}

src/Internal/Poller/SaucerPoller.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
use Boson\ApplicationId;
88
use Boson\Component\Saucer\SaucerInterface;
9+
use Boson\Poller\CancellableTask;
910
use Boson\Poller\PollerInterface;
1011
use Boson\Poller\Suspension;
12+
use Boson\Poller\TaskInterface;
1113
use Boson\Shared\IdValueGenerator\IdValueGeneratorInterface;
1214
use Boson\Shared\IdValueGenerator\PlatformDependentIntValueGenerator;
1315
use FFI\CData;
@@ -76,6 +78,9 @@ public function next(): void
7678
break;
7779
}
7880

81+
// Reduces CPU usage
82+
\time_nanosleep(0, 1);
83+
7984
$this->type = $this->type->next();
8085
}
8186

@@ -104,35 +109,35 @@ private function executeQueuedTask(): void
104109
}
105110
}
106111

107-
public function defer(callable $task): int|string
112+
public function defer(callable $task): CancellableTask
108113
{
109114
$this->queueTasks[$id = $this->ids->nextId()] = $task(...);
110115

111-
return $id;
116+
return new CancellableTask($this, $id);
112117
}
113118

114-
public function repeat(callable $task): int|string
119+
public function repeat(callable $task): CancellableTask
115120
{
116121
$this->periodicTasks[$id = $this->ids->nextId()] = $task(...);
117122

118-
return $id;
123+
return new CancellableTask($this, $id);
119124
}
120125

121-
public function delay(float $delay, callable $task): int|string
126+
public function delay(float $delay, callable $task): CancellableTask
122127
{
123128
$stopsAfter = \microtime(true) + $delay;
124129

125-
return $this->repeat(function (string|int $taskId) use ($stopsAfter, $task): void {
130+
return $this->repeat(function (TaskInterface $id) use ($stopsAfter, $task): void {
126131
if (\microtime(true) > $stopsAfter) {
127-
$task($taskId);
132+
$task($id);
128133

129-
$this->cancel($taskId);
134+
$this->cancel($id);
130135
}
131136
});
132137
}
133138

134-
public function cancel(int|string $taskId): void
139+
public function cancel(TaskInterface $task): void
135140
{
136-
unset($this->periodicTasks[$taskId], $this->queueTasks[$taskId]);
141+
unset($this->periodicTasks[$task->id], $this->queueTasks[$task->id]);
137142
}
138143
}

src/Poller/CancellableTask.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Boson\Poller;
6+
7+
/**
8+
* @template TTaskId of array-key = array-key
9+
*
10+
* @template-implements CancellableTaskInterface<TTaskId>
11+
*/
12+
final class CancellableTask implements CancellableTaskInterface, \Stringable
13+
{
14+
public private(set) bool $isCancelled = false;
15+
16+
public function __construct(
17+
/**
18+
* @var PollerInterface<TTaskId>
19+
*/
20+
private readonly PollerInterface $parent,
21+
/**
22+
* @var array-key
23+
*/
24+
public readonly string|int $id,
25+
) {}
26+
27+
public function cancel(): void
28+
{
29+
$this->isCancelled = true;
30+
$this->parent->cancel($this);
31+
}
32+
33+
public function __toString(): string
34+
{
35+
return (string) $this->id;
36+
}
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Boson\Poller;
6+
7+
/**
8+
* @template TTaskId of array-key = array-key
9+
*
10+
* @template-extends TaskInterface<TTaskId>
11+
*/
12+
interface CancellableTaskInterface extends TaskInterface
13+
{
14+
/**
15+
* Returns {@see true} in case of the task is
16+
* cancelled, {@see false} otherwise.
17+
*/
18+
public bool $isCancelled { get; }
19+
20+
/**
21+
* Cancel the task.
22+
*/
23+
public function cancel(): void;
24+
}

src/Poller/PollerInterface.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Boson\Poller;
66

77
/**
8-
* @phpstan-type TaskIdType array-key
8+
* @template TTaskId of array-key = array-key
99
*/
1010
interface PollerInterface
1111
{
@@ -23,43 +23,39 @@ public function createSuspension(): SuspensionInterface;
2323
/**
2424
* Defer the execution of a callback.
2525
*
26-
* @param callable(TaskIdType):void $task the callback to defer
26+
* @param callable(TaskInterface<TTaskId>):void $task the callback to defer
2727
*
28-
* @return TaskIdType a unique identifier that can be used to cancel
29-
* the callback
28+
* @return TaskInterface<TTaskId>
3029
*/
31-
public function defer(callable $task): int|string;
30+
public function defer(callable $task): TaskInterface;
3231

3332
/**
3433
* Repeatedly execute a callback.
3534
*
36-
* @param callable(TaskIdType):void $task the callback to execute
35+
* @param callable(TaskInterface<TTaskId>):void $task the callback to execute
3736
*
38-
* @return TaskIdType a unique identifier that can be used to cancel
39-
* the callback
37+
* @return TaskInterface<TTaskId>
4038
*/
41-
public function repeat(callable $task): int|string;
39+
public function repeat(callable $task): TaskInterface;
4240

4341
/**
4442
* Delay the execution of a callback.
4543
*
4644
* @param float $delay the amount of time, in seconds, to delay the execution for
47-
* @param callable(TaskIdType):void $task the callback to delay
45+
* @param callable(TaskInterface<TTaskId>):void $task the callback to delay
4846
*
49-
* @return TaskIdType a unique identifier that can be used to
50-
* cancel the callback
47+
* @return TaskInterface<TTaskId>
5148
*/
52-
public function delay(float $delay, callable $task): int|string;
49+
public function delay(float $delay, callable $task): TaskInterface;
5350

5451
/**
5552
* Cancel a task.
5653
*
5754
* This will detach the event loop from all resources that are associated
5855
* to the callback. After this operation the callback is permanently
59-
* invalid. Calling this function MUST NOT fail, even if passed an invalid
60-
* identifier.
56+
* invalid.
6157
*
62-
* @param TaskIdType $taskId the callback identifier
58+
* @param TaskInterface<TTaskId> $task
6359
*/
64-
public function cancel(int|string $taskId): void;
60+
public function cancel(TaskInterface $task): void;
6561
}

src/Poller/TaskInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Boson\Poller;
6+
7+
/**
8+
* @template TTaskId of array-key = array-key
9+
*/
10+
interface TaskInterface
11+
{
12+
/**
13+
* An identifier of the task.
14+
*
15+
* @var TTaskId
16+
*/
17+
public int|string $id { get; }
18+
}

0 commit comments

Comments
 (0)