Skip to content

Commit

Permalink
Fix: Early flush precedence (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek authored Nov 10, 2023
1 parent b8df541 commit dce646a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/EtlState.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public function getDuration(): float
public function shouldFlush(): bool
{
return match (true) {
$this->earlyFlush => true,
INF === $this->options->flushFrequency => false,
0 === $this->nbLoadedItemsSinceLastFlush => false,
0 === ($this->nbLoadedItemsSinceLastFlush % $this->options->flushFrequency) => true,
$this->earlyFlush => true,
default => false,
};
}
Expand Down
10 changes: 7 additions & 3 deletions src/Loader/CallableLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ public function load(mixed $item, EtlState $state): void
if (!is_callable($callback)) {
throw new LoadException('Invalid destination.');
}
$state->context['output'] = $callback($item, $state);
$state->flush();
$state->context[__CLASS__]['loaded'][] = $callback($item, $state);
}

/**
* @codeCoverageIgnore
*/
public function flush(bool $isPartial, EtlState $state): mixed
{
return $state->context['output'];
foreach ($state->context[__CLASS__]['loaded'] ?? [] as $i => $item) {
$state->context[__CLASS__]['output'][] = $item;
unset($state->context[__CLASS__]['loaded'][$i]);
}

return $state->context[__CLASS__]['output'] ?? [];
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Loader/CallableLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
$loader = new CallableLoader(function (mixed $item) use (&$items) {
$items[] = $item;

return $items;
return $item;
});

// When
Expand Down

0 comments on commit dce646a

Please sign in to comment.