Skip to content

Commit

Permalink
[bugfix/refresh-event]:+ Adding a extra if condition where the update…
Browse files Browse the repository at this point in the history
… needs to be of type fireEvent (#66)
  • Loading branch information
wpoortman authored Nov 3, 2022
1 parent 216abc5 commit f9461d3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "magewirephp/magewire",
"description": "A framework that makes building dynamic interfaces simple, without leaving the comfort of Magento 2",
"version": "1.7.1",
"version": "1.7.2",
"type": "magento2-module",
"require": {
"magento/framework": "*",
Expand Down
2 changes: 2 additions & 0 deletions src/Model/Action/CallMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

class CallMethod implements ActionInterface
{
public const ACTION = 'callMethod';

protected TypeFactory $typeFactory;
private array $uncallableMethods;

Expand Down
2 changes: 2 additions & 0 deletions src/Model/Action/FireEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

class FireEvent implements ActionInterface
{
public const ACTION = 'fireEvent';

protected CallMethod $callMethodHandler;
protected ListenerHydrator $listenerHydrator;

Expand Down
10 changes: 8 additions & 2 deletions src/Model/Action/SyncInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@

class SyncInput implements ActionInterface
{
public const ACTION = 'syncInput';

protected PropertyHelper $propertyHelper;
protected LoggerInterface $logger;

/**
* @param PropertyHelper $propertyHelper
* @param LoggerInterface $logger
*/
public function __construct(
PropertyHelper $propertyHelper
PropertyHelper $propertyHelper,
LoggerInterface $logger
) {
$this->propertyHelper = $propertyHelper;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -75,7 +81,7 @@ public function handle(Component $component, array $payload)
$transform = $this->propertyHelper->transformDots($property, $value, $component);
}
} catch (ValidationException $exception) {
// Validation can be done in every single method, so we catch and accept it.
$this->logger->critical('Magewire:' . $exception->getMessage());
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/Plugin/Model/ComponentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Exception;
use Magewirephp\Magewire\Component;
use Magewirephp\Magewire\Helper\Property as PropertyHelper;
use Magewirephp\Magewire\Model\Action\FireEvent;
use Magewirephp\Magewire\Model\ComponentManager as Subject;
use Magewirephp\Magewire\Model\Hydrator\Listener as ListenerHydrator;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -66,6 +67,11 @@ public function beforeHydrate(Subject $subject, Component $component): array
$this->logger->critical('Magewire: ' . $exception->getMessage());
}

/**
* @lifecycle Mark the component request as refreshing in order to prevent weird
* behaviour where a component is trying to refresh itself during a subsequent
* request using the update server memo as it's state.
*/
try {
$component->getRequest()->isRefreshing($this->isComponentTryingToRefresh($component));
} catch (Exception $exception) {
Expand Down Expand Up @@ -107,12 +113,19 @@ public function afterHydrate(Subject $subject, Component $component): Component
return $component;
}

/**
* @param Component $component
* @return bool
*/
public function isComponentTryingToRefresh(Component $component): bool
{
try {
$updates = $component->getRequest('updates');

if (is_array($updates) && count($updates) === 1) {
if (is_array($updates)
&& count($updates) === 1
&& $updates[0]['type'] === FireEvent::ACTION
) {
$listeners = $this->listenerHydrator->assimilateListeners($component);
return ltrim($listeners[$updates[0]['payload']['event']], '$') === $component::REFRESH_METHOD;
}
Expand Down

0 comments on commit f9461d3

Please sign in to comment.