From f6092343df715027072b7e3e4fe537632d4dfb54 Mon Sep 17 00:00:00 2001 From: Harrie van der Lubbe Date: Sun, 22 Dec 2019 04:58:19 +0000 Subject: [PATCH] only add js (checked by hash) once - to prevent multiple listeners added through queued tasks adding more than once --- src/LaravelViewBinder.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/LaravelViewBinder.php b/src/LaravelViewBinder.php index 6a2b5ec..4dc39b2 100644 --- a/src/LaravelViewBinder.php +++ b/src/LaravelViewBinder.php @@ -34,14 +34,23 @@ public function __construct(Dispatcher $event, $views) /** * Bind the given JavaScript to the view. + * Tracks if Javascript has already been added + * by keeping hashes of the js code to check against in the view data * * @param string $js */ public function bind($js) { foreach ($this->views as $view) { - $this->event->listen("composing: {$view}", function () use ($js) { - echo ""; + $this->event->listen("composing: {$view}", function ($view) use ($js) { + $data = $view->getData(); + $hashes = isset($data['hashes']) ? $data['hashes'] : []; + $hash = md5($js); + if (!in_array($hash,$hashes)) { + $hashes[] = md5($js); + $view->with('hashes',$hashes); + echo ""; + } }); } }