-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOutputDiff.php
More file actions
150 lines (137 loc) · 4.25 KB
/
OutputDiff.php
File metadata and controls
150 lines (137 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types = 1);
namespace Innmind\FileWatch\Ping;
use Innmind\FileWatch\Continuation;
use Innmind\Server\Control\Server\{
Processes,
Command,
Process\Output,
Process\Output\Type,
};
use Innmind\Time\{
Halt,
Period,
};
use Innmind\Immutable\{
Attempt,
Sequence,
Monoid\Concat,
Predicate\Instance,
};
/**
* @internal
*/
final class OutputDiff implements Implementation
{
public function __construct(
private Processes $processes,
private Command $command,
private Halt $halt,
private Period $period,
) {
}
/**
* @template C
* @template R
*
* @param C $carry
* @param callable(R|C, Continuation<R|C>): Continuation<R> $ping
*
* @return Attempt<R|C>
*/
#[\Override]
public function __invoke(mixed $carry, callable $ping): Attempt
{
$stop = new \stdClass;
$result = Sequence::lazy(function() {
while (true) {
yield $this->output();
$_ = ($this->halt)($this->period)->memoize();
}
})
->flatMap(static fn($maybe) => $maybe->match(
Sequence::of(...),
static fn($e) => Sequence::of($e, $stop),
))
->takeWhile(static fn($value) => $value !== $stop)
->keep(
Instance::of(Sequence::class)
->or(Instance::of(\Throwable::class)),
)
->aggregate(function($previous, $now) {
if ($previous instanceof \Throwable) {
return Sequence::of($previous);
}
if ($now instanceof \Throwable) {
return Sequence::of($now);
}
/**
* @psalm-suppress MixedArgument
* @psalm-suppress MixedArgumentTypeCoercion
*/
return match ($this->diff($previous, $now)) {
true => Sequence::of($previous, $now),
false => Sequence::of($now),
};
})
->sink($carry)
->until(static function($carry, $output, $continuation) use ($ping) {
if ($output instanceof \Throwable) {
/** @psalm-suppress InvalidArgument */
return $continuation->stop($output);
}
/** @psalm-suppress MixedArgument */
return $ping($carry, Continuation::of($carry))->match(
$continuation->continue(...),
$continuation->stop(...),
);
});
if ($result instanceof \Throwable) {
return Attempt::error($result);
}
return Attempt::result($result);
}
/**
* @return Attempt<Sequence<Output\Chunk>>
*/
private function output(): Attempt
{
return $this
->processes
->execute($this->command)
->flatMap(fn($process) => $process->wait()->match(
Attempt::result(...),
fn() => Attempt::error(new \RuntimeException(\sprintf(
'Failed to run command "%s"',
$this->command->toString(),
))),
))
->map(static fn($success) => $success->output())
->flatMap(
static fn($output) => $output
->find(static fn($chunk) => $chunk->type() === Type::error)
->match(
static fn($chunk) => Attempt::error(new \RuntimeException(
$chunk->data()->toString(),
)),
static fn() => Attempt::result($output),
),
);
}
/**
* @param Sequence<Output\Chunk> $previous
* @param Sequence<Output\Chunk> $now
*/
private function diff(Sequence $previous, Sequence $now): bool
{
$previous = $previous
->map(static fn($chunk) => $chunk->data())
->fold(Concat::monoid)
->toString();
$now = $now
->map(static fn($chunk) => $chunk->data())
->fold(Concat::monoid)
->toString();
return $previous !== $now;
}
}