Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
- Requires PHP `8.4`
- Requires `innmind/operating-system:~7.0`
- Requires `innmind/time:~1.0`
- Tasks results are now accessible via `Continuation::results()` instead of scope's fourth argument
12 changes: 6 additions & 6 deletions proofs/functional.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ static function($assert, $value) {
$values = Scheduler::of(Factory::build())
->sink(Sequence::of())
->with(
static fn($all, $__, $continuation, $results) => $continuation
->schedule(match ([$all->size(), $results->size()]) {
static fn($all, $__, $continuation) => $continuation
->schedule(match ([$all->size(), $continuation->results()->size()]) {
[0, 0] => Sequence::of(static fn() => $value),
default => Sequence::of(),
})
->carryWith($all->append($results))
->carryWith($all->append($continuation->results()))
->wakeOnResult(),
);
$assert->same([$value], $values->toList());
Expand All @@ -110,7 +110,7 @@ static function($assert) {
Scheduler::of(Factory::build())
->sink(false)
->with(
static fn($started, $os, $continuation, $results) => match ([$started, $results->size()]) {
static fn($started, $os, $continuation) => match ([$started, $continuation->results()->size()]) {
[false, 0] => $continuation
->schedule(Sequence::of(
static function($os) {
Expand Down Expand Up @@ -459,13 +459,13 @@ static function($assert) {
$results = Scheduler::of(Factory::build())
->sink(Sequence::of())
->with(
static fn($all, $__, $continuation, $results) => $continuation
static fn($all, $__, $continuation) => $continuation
->schedule(Sequence::of(
static fn($os) => $os->process()->halt(Period::second(1))->unwrap(),
static fn($os) => $os->process()->halt(Period::second(1))->unwrap(),
static fn($os) => $os->process()->halt(Period::second(1))->unwrap(),
)->map(Task\Discard::result(...)))
->carryWith($all->append($results))
->carryWith($all->append($continuation->results()))
->wakeOnResult(),
);

Expand Down
3 changes: 1 addition & 2 deletions src/Scheduler/Sink.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
Config,
};
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\Immutable\Sequence;

/**
* @template C
Expand Down Expand Up @@ -50,7 +49,7 @@ public static function of(
}

/**
* @param callable(C, OperatingSystem, Continuation<C>, Sequence<mixed>): Continuation<C> $scope
* @param callable(C, OperatingSystem, Continuation<C>): Continuation<C> $scope
*
* @return C
*/
Expand Down
3 changes: 1 addition & 2 deletions src/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Innmind\Async\Scope\Continuation;
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\Immutable\Sequence;

/**
* @internal
Expand All @@ -24,7 +23,7 @@ private function __construct(
* @psalm-pure
* @template C
*
* @param callable(C, OperatingSystem, Continuation<C>, Sequence<mixed>): Continuation<C> $scope
* @param callable(C, OperatingSystem, Continuation<C>): Continuation<C> $scope
*
* @return Scope\Uninitialized<C>
*/
Expand Down
19 changes: 18 additions & 1 deletion src/Scope/Continuation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ final class Continuation
/**
* @param Sequence<callable(OperatingSystem)> $tasks
* @param C $carry
* @param Sequence<mixed> $results
*/
private function __construct(
private Next $next,
private Sequence $tasks,
private mixed $carry,
private Sequence $results,
) {
}

Expand All @@ -30,16 +32,18 @@ private function __construct(
* @internal
*
* @param A $carry
* @param Sequence<mixed> $results
*
* @return self<A>
*/
#[\NoDiscard]
public static function new(mixed $carry): self
public static function new(mixed $carry, Sequence $results): self
{
return new self(
Next::restart,
Sequence::of(),
$carry,
$results,
);
}

Expand All @@ -55,6 +59,7 @@ public function carryWith(mixed $carry): self
$this->next,
$this->tasks,
$carry,
$this->results,
);
}

Expand All @@ -74,9 +79,18 @@ public function schedule(Sequence $tasks): self
->prepend($this->tasks)
->snap(),
$this->carry,
$this->results,
);
}

/**
* @return Sequence<mixed>
*/
public function results(): Sequence
{
return $this->results;
}

/**
* @return self<C>
*/
Expand All @@ -87,6 +101,7 @@ public function finish(): self
Next::finish,
$this->tasks,
$this->carry,
$this->results,
);
}

Expand All @@ -100,6 +115,7 @@ public function wakeOnResult(): self
Next::wake,
$this->tasks,
$this->carry,
$this->results,
);
}

Expand All @@ -116,6 +132,7 @@ public function terminate(): self
Next::terminate,
$this->tasks->clear(),
$this->carry,
$this->results,
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Scope/Restartable.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public function next(
$return = Suspension::of($fiber->start(
$this->carry,
$async,
Continuation::new($this->carry),
$results,
Continuation::new(
$this->carry,
$results,
),
));

if ($return instanceof Suspension) {
Expand Down
6 changes: 4 additions & 2 deletions src/Scope/Uninitialized.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ public function next(OperatingSystem $async): Suspended|Restartable|Wakeable|Ter
$return = Suspension::of($fiber->start(
$this->carry,
$async,
Continuation::new($this->carry),
Sequence::of(), // no results
Continuation::new(
$this->carry,
Sequence::of(), // no results
),
));

if ($return instanceof Suspension) {
Expand Down
6 changes: 4 additions & 2 deletions src/Scope/Wakeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ public function next(
$return = Suspension::of($fiber->start(
$this->carry,
$async,
Continuation::new($this->carry),
$results,
Continuation::new(
$this->carry,
$results,
),
));

if ($return instanceof Suspension) {
Expand Down