Skip to content

Commit f11f182

Browse files
committed
make Halt return an Attempt<SideEffect>
1 parent ce4cca5 commit f11f182

File tree

6 files changed

+33
-13
lines changed

6 files changed

+33
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use Innmind\TimeContinuum\Period;
2626
function yourApp(Halt $halt): void
2727
{
2828
// do something
29-
$halt(Period::minute(42));
29+
$halt(Period::minute(42))->unwrap();
3030
// do some more
3131
}
3232

proofs/logger.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Halt\Usleep,
88
};
99
use Innmind\TimeContinuum\Period;
10+
use Innmind\Immutable\SideEffect;
1011
use Psr\Log\NullLogger;
1112

1213
return static function() {
@@ -22,10 +23,12 @@
2223

2324
yield test(
2425
'Logger',
25-
static fn($assert) => $assert->null(
26-
Logger::psr(new Usleep, new NullLogger)(
27-
Period::millisecond(100),
28-
),
29-
),
26+
static fn($assert) => $assert
27+
->object(
28+
Logger::psr(new Usleep, new NullLogger)(
29+
Period::millisecond(100),
30+
)->unwrap(),
31+
)
32+
->instance(SideEffect::class),
3033
);
3134
};

proofs/usleep.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Halt\Usleep,
77
};
88
use Innmind\TimeContinuum\Period;
9+
use Innmind\Immutable\SideEffect;
910

1011
return static function() {
1112
yield test(
@@ -19,9 +20,11 @@
1920
'Usleep',
2021
static fn($assert) => $assert
2122
->time(static function() use ($assert) {
22-
$assert->null(
23-
(new Usleep)(Period::millisecond(500)),
24-
);
23+
$assert
24+
->object(
25+
(new Usleep)(Period::millisecond(500))->unwrap(),
26+
)
27+
->instance(SideEffect::class);
2528
})
2629
->inMoreThan()
2730
->milliseconds(500),

src/Halt.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
namespace Innmind\TimeWarp;
55

66
use Innmind\TimeContinuum\Period;
7+
use Innmind\Immutable\{
8+
Attempt,
9+
SideEffect,
10+
};
711

812
interface Halt
913
{
1014
/**
1115
* Halt the program for the given period
16+
*
17+
* @return Attempt<SideEffect>
1218
*/
13-
public function __invoke(Period $period): void;
19+
public function __invoke(Period $period): Attempt;
1420
}

src/Halt/Logger.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Innmind\TimeWarp\Halt;
77
use Innmind\TimeContinuum\Period;
8+
use Innmind\Immutable\Attempt;
89
use Psr\Log\LoggerInterface;
910

1011
final class Logger implements Halt
@@ -19,7 +20,7 @@ private function __construct(Halt $halt, LoggerInterface $logger)
1920
}
2021

2122
#[\Override]
22-
public function __invoke(Period $period): void
23+
public function __invoke(Period $period): Attempt
2324
{
2425
$this->logger->debug('Halting current process...', ['period' => [
2526
'years' => $period->years(),
@@ -30,7 +31,8 @@ public function __invoke(Period $period): void
3031
'seconds' => $period->seconds(),
3132
'milliseconds' => $period->milliseconds(),
3233
]]);
33-
($this->halt)($period);
34+
35+
return ($this->halt)($period);
3436
}
3537

3638
public static function psr(Halt $halt, LoggerInterface $logger): self

src/Halt/Usleep.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
PeriodToMilliseconds,
99
};
1010
use Innmind\TimeContinuum\Period;
11+
use Innmind\Immutable\{
12+
Attempt,
13+
SideEffect,
14+
};
1115

1216
final class Usleep implements Halt
1317
{
@@ -19,9 +23,11 @@ public function __construct()
1923
}
2024

2125
#[\Override]
22-
public function __invoke(Period $period): void
26+
public function __invoke(Period $period): Attempt
2327
{
2428
/** @psalm-suppress ArgumentTypeCoercion todo update types to fix this error */
2529
\usleep(($this->periodToMilliseconds)($period) * 1000);
30+
31+
return Attempt::result(SideEffect::identity());
2632
}
2733
}

0 commit comments

Comments
 (0)