michaelcontento / redux-storage

Persistence layer for redux with flexible backends
MIT License
675 stars 50 forks source link

Why SAVE action dispatched with state not processed with engine? #135

Closed nd0ut closed 8 years ago

nd0ut commented 8 years ago

I was playing with redux-storage-decorator-filter and noticed that SAVE action comes with untouched state. I supposed to catch the action with the filtered state.

I imagine all this flow like that: image

I mean I think the SAVE action should be dispatched with state processed with decorators.

But with the current architecture it's hard to catch state between decorators and engine. The easiest way is to catch it right from engine.

const dispatchSave = (state = getState()) => dispatch(actionSave);
engine.save(saveState).then(dispatchSave).catch(swallow);
nd0ut commented 8 years ago

Also I think it would be great to customize a save dispatcher image I'll make PR soon

michaelcontento commented 8 years ago

Sorry for the very very delayed response! 😞

Yes, the state passed with the SAVE action is the whole state tree and not the actual part that has been saved by the engine. This is by done design and there are argument for both ways (passing the whole state or only the final slice).

If you want to track only the written slices (and, for example, other informations like the size in bytes or the duration) you can add a new decorator to the mix like:

// first we filter the state for this engine
import filter from 'redux-storage-decorator-filter'
engine = filter(engine, 'whitelisted-key');

// and now we add the telemetry decorator
import logEngineStatsDecorator from 'redux-storage-decorator-engine-logger';
engine = logEngineStatsDecorator(engine);

Would such a solution solve your needs?