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

Endless loop #9

Open
vickysongang opened this issue Apr 18, 2017 · 3 comments
Open

Endless loop #9

vickysongang opened this issue Apr 18, 2017 · 3 comments

Comments

@vickysongang
Copy link

handleStorageEvent will be endless loop when the count of tab is more than two

@Gycianka
Copy link

Idk if you still need but this seems to fix endless loop when you have more then two tabs/windows open. It just ignore store persist on inactive tabs/windows.

import { KEY_PREFIX } from 'redux-persist/constants';

const CrossTabSync = (persistor) => {
  let isFocused = (document.hasFocus) ? document.hasFocus() : true;

  // If window is active - persis store changes.
  window.addEventListener('focus', () => {
    persistor.resume();
    isFocused = true;
  }, false);

  // If window isn't active - stop persisting store changes.
  window.addEventListener('blur', () => {
    persistor.pause();
    isFocused = false;
  }, false);

  const handleStorageEvent = (event) => {
    if (isFocused) {
      return;
    }

    // IE.
    const realEvent = event || window.event;

    // If key is not from redux-persist.
    const { key } = realEvent;
    if (!key.startsWith(KEY_PREFIX)) {
      return;
    }

    const keySpace = key.substr(KEY_PREFIX.length);
    persistor.rehydrate({
      [keySpace]: realEvent.newValue,
    }, {
      serial: true,
    });
  };

  window.addEventListener('storage', handleStorageEvent, false);
};

export default CrossTabSync;

@irisSchaffer
Copy link
Contributor

I'm having problems with endless loops in IE11, even with only one tab open (originally discussed here: rt2zz/redux-persist#475 (comment))

Not consistently, but every now and then, frequently after changing routes (we use react-router), or also on the initial page load, I get stuck in an infinite loop of persist/REHYDRATE actions being fired. Tested this on browser stack.

image

If I remove this package, everything works fine.

Versions:

  • redux-persist: 4.10.2
  • redux-persist-crosstab: 3.6.0

irisSchaffer added a commit to irisSchaffer/redux-persist-crosstab that referenced this issue Nov 20, 2017
See rt2zz#9 

IE 11 seems to get stuck in an infinite loop of `storage` events sometimes. By only rehydrating if the old and new values aren't the same, this problems seems to be fixed *yaay*
@irisSchaffer
Copy link
Contributor

Added a PR which fixes the problem in the case of IE11. See #16

rt2zz pushed a commit that referenced this issue Nov 20, 2017
See #9 

IE 11 seems to get stuck in an infinite loop of `storage` events sometimes. By only rehydrating if the old and new values aren't the same, this problems seems to be fixed *yaay*
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

3 participants