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

Allow for any type of root level state #113

Merged
merged 4 commits into from
Aug 28, 2016
Merged

Allow for any type of root level state #113

merged 4 commits into from
Aug 28, 2016

Conversation

rufman
Copy link
Contributor

@rufman rufman commented Jun 8, 2016

This addresses issue (#64), when you have an immutable object as the root state.

The redux-persist-immutable-state package provides the functions for actually iterating over an immutable root state as well as getting and setting keys.

Usage of redux-persist-immutable-state:

import { stateIterator, stateGetter, stateSetter, 
         stateReconciler, lastStateInit } from 'redux-persist-immutable-state';

persistStore(state, {
  transforms: [reduxPersistImmutable], 
  stateIterator: stateIterator,  
  stateGetter: stateGetter, stateSetter: stateSetter,
  lastStateInit: lastStateInit
})

I've spent some time benchmarking the persisting of new data on state changes, as well as rehydrating the state from localStorage. Here are the results:
chart

The skeleton around those benchmarks is here: https://github.com/rufman/benchmark-redux-persist
If you want to run the tests yourself just clone https://github.com/rufman/redux-persist into the lib directory of benchmark-redux-persist and run webpack. Then open index.html and run the tests.

@rt2zz
Copy link
Owner

rt2zz commented Jun 13, 2016

awesome 👍

I like this general approach, but want to wait for some resolution to reduxjs/redux#1792 before committing to a specific api.

@rufman
Copy link
Contributor Author

rufman commented Jun 13, 2016

OK, cool. In the mean time I have published this PR as redux-persist-2 for anyone that already wants to use it.

@dpereira411
Copy link

where is reduxPersistImmutable coming from? from redux-persist-transform-immutable? can I have an example?

@rufman
Copy link
Contributor Author

rufman commented Jun 16, 2016

reduxPersistImmutable is from redux-persist-immutable, which serializes ImmutableJS substates. If your root level state is an ImmutableJS object you'll need to use redux-persist-immutable-state, but if it's a plain object and just your sub states are Immutable objects redux-persist-immutable will be all you need.

@dpereira411
Copy link

thanks, so whats the difference between redux-persist-imutable and redux-persist-transform-immutable? github redirects redux-persist-immutable to redux-persist-transform-immutable

@rt2zz
Copy link
Owner

rt2zz commented Jun 16, 2016

@dpereira411 redux-persist-immutable is deprecated, as redux-persist-transform-immutable works with redux-persist v3+

@mattkrick
Copy link

Could i get a little help on this API?
Still not getting it to persist from localStorage:

import {persistStore, autoRehydrate} from 'redux-persist-2';
import immutableTransform from 'redux-persist-transform-immutable';
import { stateIterator, stateGetter, stateSetter,
  stateReconciler, lastStateInit } from 'redux-persist-immutable-state';

persistStore(store, {
    transform: [immutableTransform],
    stateIterator: stateIterator,
    stateGetter: stateGetter,
    stateSetter: stateSetter,
    lastStateInit: lastStateInit
  });

@rufman
Copy link
Contributor Author

rufman commented Jun 17, 2016

transform: [immutableTransform] should be transforms: [immutableTransform]

@mattkrick
Copy link

that'll do it, thanks!

@dpereira411
Copy link

shouldn't that be transforms: [immutableTransform()] ?

@ntucker
Copy link

ntucker commented Aug 19, 2016

autoRehydrate doesn't seem to be working

import { persistStore, autoRehydrate } from 'redux-persist-2'
import immutableTransform from 'redux-persist-transform-immutable'
import { stateIterator, stateGetter, stateSetter,
         stateReconciler, lastStateInit } from 'redux-persist-immutable-state'

  let enhancer = compose(
    autoRehydrate(),
    applyMiddleware(
      ...middleware,
    ),
  )

  const store = createStore(rootReducer, Map(), enhancer)
  persistStore(store, {
    whitelist: ['counter'],
    debounce: 500,
    storage: localforage,
    transforms: [immutableTransform({})],
    stateIterator,
    stateGetter, stateSetter,
    lastStateInit, stateReconciler,
  })

It saves state fine (I checked the store), but it won't initialize. It creates an action properly with all the data, but that doesn't update the state for some reason. I put some console.log in the autorehydrate enhancer and I noticed its reducer is only called once on the @@init action, then never again. It's not called on the persist/REHYDRATE or anything else. Anyone know why that might be?

@marcellosachs
Copy link

marcellosachs commented Aug 26, 2016

@ntucker I ran into a similar issue - basically you have to pass stateReconciler as a config option to autoRehydrate instead of to persistStore :

import { persistStore, autoRehydrate } from 'redux-persist-2'
import immutableTransform from 'redux-persist-transform-immutable'
import { stateIterator, stateGetter, stateSetter,
         stateReconciler, lastStateInit } from 'redux-persist-immutable-state'

  let enhancer = compose(
    autoRehydrate({stateReconciler}),
    applyMiddleware(
      ...middleware,
    ),
  )

  const store = createStore(rootReducer, Map(), enhancer)
  persistStore(store, {
    whitelist: ['counter'],
    debounce: 500,
    storage: localforage,
    transforms: [immutableTransform({})],
    stateIterator,
    stateGetter, stateSetter,
    lastStateInit, 
  })

@rufman may be helpful to add this note to your original comment. Thanks for making this btw!

@ntucker
Copy link

ntucker commented Aug 26, 2016

Thanks @marcellosachs !

@rufman
Copy link
Contributor Author

rufman commented Aug 26, 2016

Yeah, I guess I need to go over the docs and add that. My bad.

@rt2zz rt2zz mentioned this pull request Aug 28, 2016
Merged
@rt2zz rt2zz merged commit 5e8b5c4 into rt2zz:master Aug 28, 2016
@ntucker
Copy link

ntucker commented Aug 28, 2016

FYI @rt2zz : #158 still has the redux-persist-2 stuff in the README

+# What about redux-persist?
+This is a fork of redux-persist published as redux-persist-2 until the PR #113 gets merged.

@rt2zz
Copy link
Owner

rt2zz commented Aug 28, 2016

@ntucker yikes good catch 🙌

@Jbarget
Copy link

Jbarget commented Aug 31, 2016

@rufman @marcellosachs
if anyone could cast an eye at the issue below i'd be very grateful! I'm having some issues with my set up

rt2zz/redux-persist-transform-immutable#15

@ntucker
Copy link

ntucker commented Aug 31, 2016

The reconciler problem wasn't my issue. The problem is the reducer from createRehydrationReducer is only run once from the @@init action.

stateReconciler only matters INSIDE that reducer, but it doesn't even get the chance to run.

Maybe this is because I'm not using Action Buffers?

PS) I'm using "redux-persist": "3.6.0-alpha1",

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

Successfully merging this pull request may close these issues.

7 participants