All files / client persistState.js

38.46% Statements 5/13
100% Branches 0/0
0% Functions 0/3
38.46% Lines 5/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28        2x 2x   2x               2x                 2x      
/*
  Saves and loads a snapshot of the Redux store
  state to session storage
*/
const key = 'p5js-editor';
const storage = sessionStorage;
 
export const saveState = (state) => {
  try {
    storage.setItem(key, JSON.stringify(state));
  } catch (error) {
    console.warn('Unable to persist state to storage:', error);
  }
};
 
export const loadState = () => {
  try {
    return JSON.parse(storage.getItem(key));
  } catch (error) {
    console.warn('Failed to retrieve initialize state from storage:', error);
    return null;
  }
};
 
export const clearState = () => {
  storage.removeItem(key);
};