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

[Fix] Better Octane support - memory fixes in Livewire:listen #3940

Closed

Conversation

luischavez
Copy link
Contributor

Moved from #3936

Fixed memory issues with some of the clases in Features folder.

We need to be careful when working with octane and Livewire::listen, because Livewire instance is a singleton it will keep in memory any reference inside the listener, for example in SupportComponentTraits:

    protected $componentIdMethodMap = [];

    function __construct()
    {
        Livewire::listen('component.hydrate', function ($component) {
            $component->initializeTraits();

            foreach (class_uses_recursive($component) as $trait) {
                $hooks = [
                    'hydrate',
                    'mount',
                    'updating',
                    'updated',
                    'rendering',
                    'rendered',
                    'dehydrate',
                ];

                foreach ($hooks as $hook) {
                    $method = $hook.class_basename($trait);

                    if (method_exists($component, $method)) {
                        $this->componentIdMethodMap[$component->id][$hook][] = [$component, $method];
                    }
                }
            }

            $methods = $this->componentIdMethodMap[$component->id]['hydrate'] ?? [];

            foreach ($methods as $method) {
                ImplicitlyBoundMethod::call(app(), $method);
            }
        });

the $componentIdMethodMap variable referenced in the listener will be the same on each request, so the listener will add more and more callables to the variable and it grows without limit, my solution is keep all the "state" in the component so I added a few methods on the component to store the state.

@luischavez luischavez mentioned this pull request Oct 4, 2021
@luischavez
Copy link
Contributor Author

Meanwhile this problem is fixed, if anyone else has experienced this issue, you can register a listener on the octane config to reset the listeners on each request.

You can get an example here:
https://github.com/luischavez/livewire-extensions/blob/main/src/Luischavez/Livewire/Extensions/OctaneLivewireFixer.php

RequestReceived::class => [
    .....

    OctaneLivewireFixer::class,
],

@joshhanley joshhanley changed the title Better Octane support - memory fixes in Livewire:listen [Fix] Better Octane support - memory fixes in Livewire:listen Oct 8, 2021
@luischavez
Copy link
Contributor Author

Already fixed in #3987

@luischavez luischavez closed this Oct 11, 2021
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

1 participant