redux-persist-immutable icon indicating copy to clipboard operation
redux-persist-immutable copied to clipboard

Migrating to immutable state is problematic

Open federicobadini opened this issue 8 years ago • 0 comments

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.

federicobadini avatar May 08 '17 10:05 federicobadini