Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simultaneous $wire method calls always return the value of the last call #3510

Merged
merged 5 commits into from
Aug 14, 2021
Merged

Simultaneous $wire method calls always return the value of the last call #3510

merged 5 commits into from
Aug 14, 2021

Conversation

danharrin
Copy link
Contributor

Failing test for #3065.

@danharrin danharrin marked this pull request as draft August 11, 2021 23:09
@calebporzio
Copy link
Collaborator

@danharrin - good catch. This is a bug. Here's what's happening:

The Problem

After an "action" (component method) is called, the returned value is captured and dealt with in the following line of code:

$this->returnsByIdAndAction[$component->id][$action] = $returned;

As you can see, the returned values are stored in a key/value pair where the key is the action name.

Therefore, if the same action is called twice with different returns, the last executed one will overwrite the previous one and both will be used on the front-end.

The Solution

I see two solution paths:

A) Assign a unique identifier/signature to each action on the front-end before the request is fired, then change the key/value pair to use the action "signature" instead of the action name, that way information can be tracked properly.

B) Preserve the current schema, but allow for arrays of return values in the order of execution - then, assuming the order is preserved throughout the lifecycle, the front-end can retrieve the proper return value.

Which one to choose depends on how well the order of actions is preserved throughout the request/response. And also if it is preserved, do we want to depend on that order preservation.

My gut tells me an action "signature" is the right way to go here, my only hesitancy is adding a new concept to the payloads and adding extra bytes to every single request/response - the latter is probably negligible, but it's always a consideration with Livewire.

What are your thoughts?

@danharrin
Copy link
Contributor Author

Thanks for the insight! I was discussing this with @joshhanley the other day and we settled on the signature approach too - didn't consider the performance implications but IMHO this is really not a massive problem.

Would you like to implement the solution yourself or do you want me to have a go / pair with you?

@calebporzio
Copy link
Collaborator

I'll have a go at this myself - also, this failing test was failing for the wrong reasons - I updated it.

@calebporzio calebporzio marked this pull request as ready for review August 14, 2021 15:58
@calebporzio calebporzio merged commit 854416a into livewire:master Aug 14, 2021
@calebporzio
Copy link
Collaborator

Thanks for bringing this up @danharrin. I went with the signature route and test is now passing.

@danharrin
Copy link
Contributor Author

Awesome, thanks Caleb!

@danharrin danharrin deleted the fix/simultaneous-$wire-calls branch August 14, 2021 16:07
@Casmo
Copy link

Casmo commented Sep 29, 2021

Since this update I get the following error: Undefined index: id on
vendor/livewire/livewire/src/HydrationMiddleware/PerformActionCalls.php:27

@joshhanley
Copy link
Member

@Casmo please see this pinned discussion on how to fix #3769

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants