From 85072690373a0ee88347710f8fa5d475dc60468e Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Wed, 13 Dec 2023 02:03:02 +1100 Subject: [PATCH] Bail on store when entries is empty (#212) --- src/Pulse.php | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Pulse.php b/src/Pulse.php index 25c8d686..54c65a9a 100644 --- a/src/Pulse.php +++ b/src/Pulse.php @@ -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]); @@ -267,8 +265,11 @@ public function ignore($callback): mixed public function flush(): self { $this->entries = collect([]); + $this->lazy = collect([]); + $this->rememberedUserId = null; + return $this; } @@ -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; } /** @@ -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; } /**