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

Disable the state-changed event when not needed #127

Open
TimPietrusky opened this issue Feb 15, 2018 · 8 comments
Open

Disable the state-changed event when not needed #127

TimPietrusky opened this issue Feb 15, 2018 · 8 comments

Comments

@TimPietrusky
Copy link

TimPietrusky commented Feb 15, 2018

Every time the state is changing, the "state-changed" event is dispatched onto every element that is using the ReduxMixin. So the more elements I have, the more events are dispatched, even when I don't want to listen to them at all.

But why is this a problem? This behaviour is increasing the scripting time at least 25% (up to 50% max), which is slowing down the overall performance. It even gets worse when using requestAnimationFrame.

Proposal

Make the "state-changed" event optional:

When creating the mixin:

export default function PolymerRedux(store, mixinProperties = { enableStateChangedEvent: true }) {

And in the redux listener:

// Redux listener
const unsubscribe = store.subscribe(() => {
    const detail = store.getState();
    update(detail);

    if (mixinProperties.enableStateChangedEvent) {
        element.dispatchEvent(new CustomEvent('state-changed', {detail}));
    }
});

Of course I could extend the mixin and make my changes, but as this is something that also might affect others I think this should be part of PolymerRedux itself.

Additional info

@pixelass
Copy link

@tur-nr I can take a look at it and propose a PR on the weekend. Let's see how this looks when implemented.

@pixelass
Copy link

@TimPietrusky

  • do all of the 20 components actually need the mixin?
  • what is the expected behaviour when state changes are disabled?
    • I guess you expect redux to update the store without sending an event?

@tur-nr
Copy link
Owner

tur-nr commented Feb 15, 2018

I think this is down to the use of the library. You should be using higher order elements that make use of the Mixin and then pass the properties down.

I don't like the idea of opting out of events, it's in the developers hands not to use the Mixin for every component that needs state. This is more beneficial for your application as it decouples your elements from Redux and makes them more portal.

@pixelass
Copy link

@tur-nr I agree. Thus the question if all 20 components really need this.

@TimPietrusky
Copy link
Author

TimPietrusky commented Feb 17, 2018

I have a lot of different components that do different things on the data in the state by using reselect and I need the Redux store for using selectors. That's why I add the ReduxMixin to be able to access the state.

But even if I would optimize this: My app gets new components every month and they all need their own space in the state.

I agree that changing polymer-redux might be a bit too much.

Proposal

  • In my project I will extend PolymerRedux and overwrite unsubscribe to remove the state-changed event
  • Contribute the example

@TimPietrusky
Copy link
Author

@tur-nr I was wondering: Why do we even need the state-changed event?

TimPietrusky added a commit to NERDDISCO/polymer-redux that referenced this issue Feb 18, 2018
@tur-nr
Copy link
Owner

tur-nr commented Feb 19, 2018

In case a developer wants to subscribe to state changes to perform logic outside of bindings. Much like the store.subscribe method. I can't presume every element has access to the original store, so it get's proxied via this event.

@developerium
Copy link

I think the resulting flexibility is beneficial for developers.

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

No branches or pull requests

4 participants