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

Migrating to immutable state is problematic #27

Open
federicobadini opened this issue May 8, 2017 · 0 comments
Open

Migrating to immutable state is problematic #27

federicobadini opened this issue May 8, 2017 · 0 comments

Comments

@federicobadini
Copy link

federicobadini commented May 8, 2017

When trying to migrate from a non-immutable to an immutable state, I would like to be able to read the old & non-immutably stored values in order to set up the new immutable state.
However the process fails due to unexpected token o in json at position 1.

In my opinion, this can be due to the fact that redux-persist-immutable adds the immutableTransforms to any subState, and the redux-persist-transform-immutable does not check if raw is already a json before applying transitInstance.fromJSON(raw).

A simple modification like the following to redux-persist-transform-immutable should allow to retrieve from the store something which was stored by means of redux persist in a non immutable way, permitting a transition to immutability.

var transit = require('transit-immutable-js')
var reduxPersist = require('redux-persist')

module.exports = function (config) {
  config = config || {}

  var transitInstance = transit
  if (config.records) {
    transitInstance = transit.withRecords(config.records)
  }

  return reduxPersist.createTransform(
    function(state){
      return transitInstance.toJSON(state)
    },
    function(raw){
	try {
	    return transitInstance.fromJSON(raw)
	} catch (e) {
	    return raw;
	}
    },
    config
  )
}

Given that I'm proposing a modification to redux-persist-transform-immutable maybe it is not totally correct to write here. However I thought that others may try to migrate to immutability via redux-persist-immutable so I feel it could be more interesting for others to see this issue in redux-persist-immutable context.

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

1 participant