Skip to content

Commit 1ecaf00

Browse files
authored
fix(delete): dont queue deleting event (#112)
- dont queue deleting event so that a listener can bust cache immediately resolves #95
1 parent 3853a78 commit 1ecaf00

File tree

8 files changed

+93
-24
lines changed

8 files changed

+93
-24
lines changed

src/Events/BroadcastingEvent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Illuminate\Broadcasting\InteractsWithSockets;
88
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
99

10-
class BroadcastingEvent extends Event implements ShouldBroadcast
10+
class BroadcastingEvent extends TaskEvent implements ShouldBroadcast
1111
{
1212
use InteractsWithSockets;
1313

@@ -29,7 +29,7 @@ public function __construct(Task $task)
2929
/**
3030
* Get the channels the event should broadcast on.
3131
*
32-
* @return Channel|array
32+
* @return \Illuminate\Broadcasting\Channel|\Illuminate\Broadcasting\Channel[]|PrivateChannel
3333
*/
3434
public function broadcastOn()
3535
{

src/Events/Deleting.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,15 @@
44

55
class Deleting extends Event
66
{
7+
public $taskId;
8+
9+
/**
10+
* Deleting constructor.
11+
*
12+
* @param $taskId
13+
*/
14+
public function __construct($taskId)
15+
{
16+
$this->taskId = $taskId;
17+
}
718
}

src/Events/Event.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,9 @@
22

33
namespace Studio\Totem\Events;
44

5-
use Studio\Totem\Task;
6-
use Illuminate\Queue\SerializesModels;
75
use Illuminate\Foundation\Events\Dispatchable;
86

97
class Event
108
{
11-
use Dispatchable, SerializesModels;
12-
13-
/**
14-
* @var Task
15-
*/
16-
public $task;
17-
18-
/**
19-
* Updated constructor.
20-
*
21-
* @param Task $task
22-
*/
23-
public function __construct(Task $task)
24-
{
25-
$this->task = $task;
26-
}
9+
use Dispatchable;
2710
}

src/Events/TaskEvent.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Studio\Totem\Events;
4+
5+
use Studio\Totem\Task;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
9+
class TaskEvent extends Event
10+
{
11+
use Dispatchable, SerializesModels;
12+
13+
/**
14+
* @var Task
15+
*/
16+
public $task;
17+
18+
/**
19+
* Constructor.
20+
*
21+
* @param Task $task
22+
*/
23+
public function __construct(Task $task)
24+
{
25+
$this->task = $task;
26+
}
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Studio\Totem\Listeners;
4+
5+
use Studio\Totem\Events\Event;
6+
use Illuminate\Container\Container;
7+
8+
class BustCacheImmediately
9+
{
10+
/**
11+
* @var Container
12+
*/
13+
protected $app;
14+
15+
/**
16+
* Create the event listener.
17+
*
18+
* @param Container $app
19+
*/
20+
public function __construct(Container $app)
21+
{
22+
$this->app = $app;
23+
}
24+
25+
/**
26+
* Handle the event.
27+
*
28+
* @param \Studio\Totem\Events\Event $event
29+
*/
30+
public function handle(Event $event)
31+
{
32+
$this->clear($event);
33+
}
34+
35+
/**
36+
* Clear Cache.
37+
*
38+
* @param Event $event
39+
*/
40+
protected function clear(Event $event)
41+
{
42+
if ($event->taskId) {
43+
$this->app['cache']->forget('totem.task.'.$event->taskId);
44+
}
45+
46+
$this->app['cache']->forget('totem.tasks.all');
47+
$this->app['cache']->forget('totem.tasks.active');
48+
}
49+
}

src/Providers/TotemEventServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ class TotemEventServiceProvider extends EventServiceProvider
1111
'Studio\Totem\Events\Updated' => ['Studio\Totem\Listeners\BustCache', 'Studio\Totem\Listeners\BuildCache'],
1212
'Studio\Totem\Events\Activated' => ['Studio\Totem\Listeners\BustCache', 'Studio\Totem\Listeners\BuildCache'],
1313
'Studio\Totem\Events\Deactivated' => ['Studio\Totem\Listeners\BustCache', 'Studio\Totem\Listeners\BuildCache'],
14-
'Studio\Totem\Events\Deleting' => ['Studio\Totem\Listeners\BustCache'],
14+
'Studio\Totem\Events\Deleting' => ['Studio\Totem\Listeners\BustCacheImmediately'],
1515
];
1616
}

src/Providers/TotemRouteServiceProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TotemRouteServiceProvider extends RouteServiceProvider
2525
public function boot()
2626
{
2727
parent::boot();
28+
2829
Route::bind('task', function ($value) {
2930
return cache()->rememberForever('totem.task.'.$value, function () use ($value) {
3031
return Task::find($value) ?? abort(404);
@@ -42,8 +43,6 @@ public function map()
4243
$this->mapApiRoutes();
4344

4445
$this->mapWebRoutes();
45-
46-
//
4746
}
4847

4948
/**

src/Repositories/EloquentTaskRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function destroy($id)
124124
{
125125
$task = $this->find($id);
126126

127-
if (Deleting::dispatch($task) === false) {
127+
if (Deleting::dispatch($task->id) === false) {
128128
return false;
129129
}
130130

0 commit comments

Comments
 (0)