Git Product home page Git Product logo

Comments (3)

aohua avatar aohua commented on August 9, 2024

I think it's not necessary to store those actions which are not allowed. Do you want to send a pull request with the tweak that you did?

from redux-state-sync.

initFabian avatar initFabian commented on August 9, 2024

We have some syntactic sugar(wrapper) around the framework, so we made the change in our codebase. We overwrote the middleware portion of your framework. From what I'm seeing, it may require a few changes to the API, I could be mistaken though. Could you checkout my code snippet below and fiddle around with some ideas on how you would like to accomplish this?

// TabSyncMiddleware.js
import { createStorageListener, withReduxStateSync } from 'redux-state-sync';
import ActionWhiteList from './ActionWhiteList'; // actions we want to sync across tabs

function s4() {
  return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}

function guid() {
  return `${s4()}-${s4()}-${s4()}-${s4()}-${s4()}-${s4()}-${s4()}-${s4()}`;
}

export const reducerTabSync = withReduxStateSync;

export const tabSyncMiddleware = ({ getState }) => (next) => (action) => {
  if (action && !action.$uuid) {
    const stampedAction = {
      $uuid: guid(),
      ...action
    };

    try {
      if (action.type === '&_SEND_INIT_STATE') {
        if (getState()) {
          stampedAction.payload = getState();
          localStorage.setItem('LAST_ACTION', JSON.stringify(stampedAction));
        }
        return next(action);
      }

      if (ActionWhiteList.includes(action.type)) { // here's the part we needed
        localStorage.setItem('LAST_ACTION', JSON.stringify(stampedAction));
      }
    } catch (e) {
      console.error('Failed to store `LAST_ACTION` to localStorage.');
    }
  }
  return next(action);
};

export const TabSyncWrapper = (store) => {
  createStorageListener(store, {
    initiateWithState: true,
    predicate: (actionType) => {
      return ActionWhiteList.includes(actionType);
    }
  });

  return store;
};

Usage

//  index.js
import { tabSyncMiddleware, TabSyncWrapper, reducerTabSync } from './middleware/TabSyncMiddleware';

// ...


const enhancer = composeEnhancers(
  applyMiddleware( ..., ..., tabSyncMiddleware)
);

const store = TabSyncWrapper(createStore(
  reducerTabSync(reducers),
  enhancer
));

// ...

render(
  <Provider store={store}>
    <AppRouter />
  </Provider>,
  document.getElementById('root')
);

from redux-state-sync.

aohua avatar aohua commented on August 9, 2024

Sorry for the long delay, I just released redux-state-sync 2.0, Your problem should be fixed in the new version.

from redux-state-sync.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.