Last active
May 1, 2020 09:17
-
-
Save heyimalex/0f5ee73e581f64b6c290 to your computer and use it in GitHub Desktop.
localStorage sync with redux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { reduxStorageReducer, ReduxStorage } from 'reduxStorage'; | |
import myReducer from './myReducer'; | |
function compose(...reducers) { | |
return (state, action) => { | |
return reducers.reduce((reducedState, reducer) => { | |
return reducer(reducedState, action); | |
}, state); | |
}; | |
} | |
const finalReducer = compose(myReducer, reduxStorageReducer); | |
const storage = new ReduxStorage('redux'); | |
const redux = createRedux(finalReducer, storage.load()); | |
storage.connect(redux); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import WebStorage from 'WebStorage'; | |
const STORAGE_SYNC = 'STORAGE_SYNC'; | |
export const actions = { STORAGE_SYNC }; | |
export function syncActionCreator(state) { | |
return { | |
type: STORAGE_SYNC, | |
synced: state, | |
}; | |
} | |
export function reduxStorageReducer(state, action) { | |
if (action.type === STORAGE_SYNC) { | |
// Depends on state being an object|null|undefined | |
return Object.assign({}, state, action.synced); | |
} else { | |
return state; | |
} | |
} | |
export class ReduxStorage extends WebStorage { | |
connect(redux, actionCreator = syncActionCreator, listen = true) { | |
redux.subscribe(() => this.save(redux.getState())); | |
if (listen) { | |
this.listen(() => redux.dispatch(actionCreator(this.load()))); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class WebStorage { | |
constructor(key, storageArea = window.localStorage) { | |
this.key = key; | |
this.storageArea = storageArea; | |
} | |
load(defaultValue) { | |
const serialized = this.storageArea.getItem(this.key); | |
return serialized === null ? defaultValue : this.deserialize(serialized); | |
} | |
save(data) { | |
if (data === undefined) { | |
this.storageArea.removeItem(this.key); | |
} else { | |
this.storageArea.setItem(this.key, this.serialize(data)); | |
} | |
} | |
clear() { | |
this.storageArea.removeItem(this.key); | |
} | |
serialize(value) { | |
return JSON.stringify(value); | |
} | |
deserialize(value) { | |
return JSON.parse(value); | |
} | |
listen(callback) { | |
const handler = (e) => { | |
if (e.key === this.key && e.storageArea === this.storageArea) { | |
callback(e); | |
} | |
}; | |
window.addEventListener('storage', handler); | |
return () => window.removeEventListener('storage', handler); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
WebStorage.js
, theserialize
method is misspelled asserialze
. (Just an FYI for people copy-pasting)