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

Emit param handling: allow assoc arrays and multiple emit arguments (like Livewire) #106

Merged
merged 3 commits into from
Aug 22, 2023

Conversation

Vinai
Copy link
Contributor

@Vinai Vinai commented Jun 8, 2023

The PR #104 caused an issue where an empty array is passed as an argument to emits when no arguments where specified.
The changes in this PR resolve the problem.

@wpoortman
Copy link
Collaborator

wpoortman commented Jun 13, 2023

@Vinai,

I've did some testing, but we need to look into this more thoroughly since the outcome between params in Livewire and Magewire now are different which I don't favor and has always been one of my priorities.

Livewire update params:
image

Magewire update params:
image

Update:
On the main branch we have still a positive result, so #104 didn't conflict and can stay as it is 😃

image

@Vinai
Copy link
Contributor Author

Vinai commented Jun 14, 2023

That makes sense to keep the two in sync.
The problem is that the previous code did not allow passing key/value associative arrays as a parameter any more.
Before the array keys are stripped out as described in #104

Please confirm the idea is that this code

$this->emit('foo_bar', ['is_complete' => true]);

results in the params

{"is_complete": true}

and the code

$this->emit('foo_bar', true, 23]);

results in the params

[true, 23]

and the code

$this->emit('foo_bar');

results in the params

[]

Right?

@jesse-deboer
Copy link

jesse-deboer commented Jun 15, 2023

For our Loqate module we use an empty this.$wire.save();. This is currently not working since #104 but is working with this PR.

I would change the check to this though:
$params = (array) ($payload['params'] ?? []);

Same for the other line of code:
'params' => (array) ($this->params ?? []),

This makes more sense to me because you can see it will always cast to an array and otherwise create an empty array.

@Vinai
Copy link
Contributor Author

Vinai commented Jun 16, 2023

@jesse-deboer I think the code you suggested does something different. I'm not 100% sure I understand your intention because so much context is missing, but from what I can see it would change the behavior of the patch in this PR.

@jesse-deboer
Copy link

@Vinai you are right, the solution in this PR fixes it, the solution I looked at the code wrong, the solution I gave does work differently and gives different results. Sorry!

@wpoortman
Copy link
Collaborator

@Vinai,

When no params, it looks like this:
image

@Vinai
Copy link
Contributor Author

Vinai commented Jun 20, 2023

@wpoortman yes, but it should be an empty array, right?
I'm just looking for confirmation of the desired behavior.

@wpoortman
Copy link
Collaborator

wpoortman commented Jun 20, 2023

Not valid JSON, but added the keys to make it more clear on how it ends up in the network tab.

$this->emit('test');

Livewire:

[
    0: {
        "event": "test",
        "params": []
    }
]

Magewire:

[
    0: {
        "event": "test",
        "params": []
    }
]

$this->emit('test', [true, 'foo']);

Livewire:

[
    0: {
        "event": "test",
        "params": [
            [
                0: true,
                1: "foo"
            ]
        ]
    }
]

Magewire:

[
    0: {
        "event": "test",
        "params": [
            0: true,
            1: "foo"
        ]
    }
]

$this->emit('test', true, 'foo');

Livewire:

[
    0: {
        "event": "test",
        "params": [
            0: true,
            1: "foo"
        ]
    }
]

Magewire:

[
    0: {
        "event": "test",
        "params": [
            0: true,
            1: "foo"
        ]
    }
]

$this->emit('test', ['foo' => 'bar']);

Livewire:

[
    0: {
        "event": "test",
        "params": [
            {
                "foo": "bar"
            }
        ]
    }
]

Magewire:

[
    0: {
        "event": "test",
        "params": [
            0: "bar"
        ]
    }
]

Conclusion: there is definitely something that needs to be changed.

@Vinai Vinai force-pushed the fix-empty-emit-params branch from db2e8f9 to 7dc2250 Compare August 18, 2023 09:14
@Vinai
Copy link
Contributor Author

Vinai commented Aug 18, 2023

I've updated the MR so the params always match magewire.
When I run though the cases you listed above in both release 1.10.7 and with this PR, these are the results I get now:

        /*
         * Magewire params 1.10.7: []
         * Magewire params PR:     []
         * Livewire params:        []
         * Listeners arguments 1.10.7: (none)
         * Listeners arguments PR:     (none)
         */
        $this->emit('test');

        /*
         * Magewire params 1.10.7: [0: true, 1: "foo"]
         * Magewire params PR:     [[0: true, 1: "foo"]]
         * Livewire params:        [[0: true, 1: "foo"]]
         * Listeners arguments 1.10.7: true
         * Listeners arguments PR:     true, "foo"
         */
        $this->emit('test', [true, 'foo']);

        /*
         * Magewire params 1.10.7: [0: true, 1: "foo"]
         * Magewire params PR:     [0: true, 1: "foo"]
         * Livewire params:        [0: true, 1: "foo"]
         * Listeners arguments 1.10.7: true
         * Listeners arguments PR:     true
         */
        $this->emit('test', true, 'foo');

        /*
         * Magewire params 1.10.7: ["bar"]
         * Magewire params PR:     [{"foo": "bar"}]
         * Livewire params:        [{"foo": "bar"}]
         * Listeners arguments 1.10.7: "bar"
         * Listeners arguments PR:     ['foo' => 'bar']
         */
        $this->emit('test', ['foo' => 'bar']);

@Vinai
Copy link
Contributor Author

Vinai commented Aug 18, 2023

I've updated the MR so multiple arguments to emit() are now correctly passed to listeners again.
This affects this case:

        /*
         * Magewire params 1.10.7: [0: true, 1: "foo"]
         * Magewire params PR:     [0: true, 1: "foo"]
         * Livewire params:        [0: true, 1: "foo"]
         * Listeners arguments 1.10.7: true
         * Listeners arguments PR:     true, "foo"
         */
        $this->emit('test', true, 'foo');

@Vinai Vinai changed the title Correctly handle empty params Emit param handling: allow assoc arrays and multiple emit arguments (like Livewire) Aug 18, 2023
@wpoortman wpoortman merged commit 14b59f4 into magewirephp:main Aug 22, 2023
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.

3 participants