Skip to content

Commit

Permalink
Bail on store when entries is empty (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Dec 12, 2023
1 parent 489bca2 commit 8507269
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/Pulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,16 @@ public function register(array $recorders): self
->filter(fn ($recorder) => $recorder->listen ?? null)
->each(fn ($recorder) => $event->listen(
$recorder->listen,
fn ($event) => $this->rescue(fn () => Collection::wrap($recorder->record($event)))
fn ($event) => $this->rescue(fn () => $recorder->record($event))
))
);

$recorders
->filter(fn ($recorder) => method_exists($recorder, 'register'))
->each(function ($recorder) {
$record = function (...$args) use ($recorder) {
$this->rescue(fn () => Collection::wrap($recorder->record(...$args)));
};

$this->app->call($recorder->register(...), ['record' => $record]);
$this->app->call($recorder->register(...), [
'record' => fn (...$args) => $this->rescue(fn () => $recorder->record(...$args)),
]);
});

$this->recorders = collect([...$this->recorders, ...$recorders]);
Expand Down Expand Up @@ -267,8 +265,11 @@ public function ignore($callback): mixed
public function flush(): self
{
$this->entries = collect([]);

$this->lazy = collect([]);

$this->rememberedUserId = null;

return $this;
}

Expand All @@ -289,21 +290,33 @@ public function filter(callable $filter): self
*/
public function store(): int
{
$entries = $this->rescue(function () {
$this->lazy->each(fn ($lazy) => $lazy());

return $this->entries->filter($this->shouldRecord(...));
}) ?? collect([]);

if ($entries->isEmpty()) {
$this->flush();

return 0;
}

$ingest = $this->app->make(Ingest::class);

$this->lazy->each(fn ($lazy) => $lazy());
$count = $this->rescue(function () use ($entries, $ingest) {
$ingest->ingest($entries);

$this->rescue(fn () => $ingest->ingest(
$this->entries->filter($this->shouldRecord(...)),
));
return $entries->count();
}) ?? 0;

Lottery::odds(...$this->app->make('config')->get('pulse.ingest.trim_lottery'))
->winner(fn () => $this->rescue($ingest->trim(...)))
->choose();

$this->rememberedUserId = null;
$this->flush();

return tap($this->entries->count(), $this->flush(...));
return $count;
}

/**
Expand Down Expand Up @@ -508,15 +521,20 @@ public function handleExceptionsUsing(callable $callback): self
/**
* Execute the given callback handling any exceptions.
*
* @param (callable(): mixed) $callback
* @template TReturn
*
* @param (callable(): TReturn) $callback
* @return TReturn|null
*/
public function rescue(callable $callback): void
public function rescue(callable $callback): mixed
{
try {
$callback();
return $callback();
} catch (Throwable $e) {
($this->handleExceptionsUsing ?? fn () => null)($e);
}

return null;
}

/**
Expand Down

0 comments on commit 8507269

Please sign in to comment.