Skip to content

Commit

Permalink
[8.x] Allow sync broadcast via method (#38557)
Browse files Browse the repository at this point in the history
* allow sync broadcast via method

* add hook to disable broadcasting via callback
  • Loading branch information
taylorotwell authored Aug 26, 2021
1 parent 3d5cae3 commit 8940ff3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Illuminate/Broadcasting/BroadcastManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ public function event($event = null)
*/
public function queue($event)
{
if ($event instanceof ShouldBroadcastNow) {
if ($event instanceof ShouldBroadcastNow ||
(is_object($event) &&
method_exists($event, 'shouldBroadcastNow') &&
$event->shouldBroadcastNow())) {
return $this->app->make(BusDispatcherContract::class)->dispatchNow(new BroadcastEvent(clone $event));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ public function onChannels(array $channels)
return $this;
}

/**
* Determine if the event should be broadcast synchronously.
*
* @return bool
*/
public function shouldBroadcastNow()
{
return $this->event === 'deleted' &&
! method_exists($this->model, 'bootSoftDeletes');
}

/**
* Get the event name.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/BroadcastsEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public function broadcastDeleted($channels = null)
*/
protected function broadcastIfBroadcastChannelsExistForEvent($instance, $event, $channels = null)
{
if (! static::$isBroadcasting) {
return;
}

if (! empty($this->broadcastOn($event)) || ! empty($channels)) {
return broadcast($instance->onChannels(Arr::wrap($channels)));
}
Expand Down
26 changes: 26 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ abstract class Model implements Arrayable, ArrayAccess, HasBroadcastChannel, Jso
*/
protected static $lazyLoadingViolationCallback;

/**
* Indicates if broadcasting is currently enabled.
*
* @var bool
*/
protected static $isBroadcasting = true;

/**
* The name of the "created at" column.
*
Expand Down Expand Up @@ -377,6 +384,25 @@ public static function handleLazyLoadingViolationUsing(callable $callback)
static::$lazyLoadingViolationCallback = $callback;
}

/**
* Execute a callback without broadcasting any model events for all model types.
*
* @param callable $callback
* @return mixed
*/
public static function withoutBroadcasting(callable $callback)
{
$isBroadcasting = static::$isBroadcasting;

static::$isBroadcasting = false;

try {
return $callback();
} finally {
static::$isBroadcasting = $isBroadcasting;
}
}

/**
* Fill the model with an array of attributes.
*
Expand Down

0 comments on commit 8940ff3

Please sign in to comment.