Skip to content

Instantly share code, notes, and snippets.

@dariocravero
Last active March 10, 2016 20:57
Show Gist options
  • Save dariocravero/65599fb2c1eee03c0fce to your computer and use it in GitHub Desktop.
Save dariocravero/65599fb2c1eee03c0fce to your computer and use it in GitHub Desktop.
overly simplistic and dumb cold reload poc
import coldReload from './cold-reload';
// ...
const cold = coldReload();
const store = configureStore(cold.state);
cold.run(store);
// ...
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