-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.php
More file actions
64 lines (54 loc) · 1.72 KB
/
sequence.php
File metadata and controls
64 lines (54 loc) · 1.72 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
<?php
declare(strict_types=1);
/**
* `Cmd::sequence` — run a list of Cmds one after the other,
* waiting for each to dispatch its Msg before starting the
* next. Compare with `Cmd::batch` which fires every Cmd in
* parallel.
*
* php examples/sequence.php
*
* Press 'q' to quit. The model logs each step as it arrives.
*/
require __DIR__ . '/../vendor/autoload.php';
use SugarCraft\Core\Cmd;
use SugarCraft\Core\KeyType;
use SugarCraft\Core\Model;
use SugarCraft\Core\Msg;
use SugarCraft\Core\Msg\KeyMsg;
use SugarCraft\Core\Program;
final class StepMsg implements Msg
{
public function __construct(public readonly string $label) {}
}
final class Sequence implements Model
{
/** @param list<string> $log */
public function __construct(public readonly array $log = []) {}
public function init(): ?\Closure
{
// Sequenced steps: each tick fires its Msg before the next
// tick starts.
return Cmd::sequence(
Cmd::tick(0.4, static fn () => new StepMsg('first')),
Cmd::tick(0.4, static fn () => new StepMsg('second')),
Cmd::tick(0.4, static fn () => new StepMsg('third')),
);
}
public function update(Msg $msg): array
{
if ($msg instanceof KeyMsg && $msg->type === KeyType::Char && $msg->rune === 'q') {
return [$this, Cmd::quit()];
}
if ($msg instanceof StepMsg) {
return [new self([...$this->log, $msg->label]), null];
}
return [$this, null];
}
public function view(): string
{
$body = $this->log === [] ? '(waiting…)' : implode("\n", $this->log);
return "Sequence steps:\n\n$body\n\n(q to quit)\n";
}
}
(new Program(new Sequence()))->run();