<commit_before>import {loadProject} from './actions/project-actions';

const storageKey = 'metior_scene_autosave_v1';
const localStorage = window.hasOwnProperty('localStorage') ? window.localStorage : false;

export default function autosave(store) {

  if (!localStorage) return;

  //revert
  if (localStorage.getItem(storageKey) !== null) {
    let data = localStorage.getItem(storageKey);
    let json = JSON.parse(data);
    store.dispatch(loadProject(json));
  }

  //update
  store.subscribe(() => {
    let {scene} = store.getState();
    let json = JSON.stringify(scene.toJSON());
    localStorage.setItem(storageKey, json);
  });

}<commit_msg>enhances autosave feature<commit_after>import {loadProject} from './actions/project-actions';
import * as constants from './constants';

const storageKey = 'metior_scene_autosave_v1';
const localStorage = window.hasOwnProperty('localStorage') ? window.localStorage : false;
const SAFE_MODES = [
  constants.MODE_IDLE,
  constants.MODE_3D_VIEW,
  constants.MODE_3D_FIRST_PERSON,
  constants.MODE_WAITING_DRAWING_LINE
];

export default function autosave(store) {

  if (!localStorage) return;

  //revert
  if (localStorage.getItem(storageKey) !== null) {
    let data = localStorage.getItem(storageKey);
    let json = JSON.parse(data);
    store.dispatch(loadProject(json));
  }

  //update
  store.subscribe(() => {
    let {scene, mode} = store.getState();
    if (SAFE_MODES.includes(mode)) {
      let json = JSON.stringify(scene.toJSON());
      localStorage.setItem(storageKey, json);
    }
  });

}
{'fraction_non_alphanumeric': 0.13653846153846153, 'fraction_numerical': 0.002564102564102564, 'mean_word_length': 4.458041958041958, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6489917', 'n_tokens_mistral': 497, 'n_tokens_neox': 500, 'n_words': 96}