Last active
March 10, 2016 20:57
-
-
Save dariocravero/65599fb2c1eee03c0fce to your computer and use it in GitHub Desktop.
overly simplistic and dumb cold reload poc
This file contains hidden or 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 coldReload from './cold-reload'; | |
// ... | |
const cold = coldReload(); | |
const store = configureStore(cold.state); | |
cold.run(store); | |
// ... |
This file contains hidden or 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 debounce from 'lodash.debounce'; | |
export default function coldReload({destroy = 1000, key = 'cold', storage = window.sessionStorage}={}) { | |
const last = `${key}-last`; | |
const now = Date.now(); | |
if (parseInt(storage.getItem(last), 10) + destroy > now) { | |
console.warn('cold storage destroyed'); | |
storage.removeItem(key); | |
} else { | |
console.warn(`cold storage found. refresh in less than ${destroy}ms to get rid of it`); | |
} | |
storage.setItem(last, now); | |
return { | |
run: store => store.subscribe(debounce(() => storage.setItem(key, JSON.stringify(store.getState())), destroy)), | |
state: JSON.parse(storage.getItem(key)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment