import assign from 'object-assign';
import EventEmitter from 'eventemitter3';
import dispatcher from '../dispatcher';

const SETTINGS_KEY = 'uwaveSettings';

const settings = {
  muted: false,
  videoSize: 'large',
  volume: 0
};

// cookie blocking safe localStorage settings persistence
function load() {
  try {
    assign(settings, JSON.parse(localStorage.getItem(SETTINGS_KEY)));
  } catch (e) {
    // Ok!
  }
}
function save() {
  try {
    localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings));
  } catch (e) {
    // Ok!
  }
}

const SettingsStore = assign(new EventEmitter, {
  getAll() {
    return assign({}, settings);
  },
  getSetting(name) {
    return settings[name];
  },

  dispatchToken: dispatcher.register(({ type, payload }) => {
    switch (type) {
    case 'loadSettings':
    case 'setSettings':
      assign(settings, payload);
      SettingsStore.emit('change');
      break;
    default:
      // Not for us
    }
  })
});

SettingsStore.on('change', save);
load();

export default SettingsStore;

@@ -4,116 +4,27 @@
 ort 
-assign from 'object-assign';%0Aimport EventEmitter from 'eventemitter3';%0Aimport dispatcher from '../dispatcher
+Store from './Store
 ';%0A%0A
@@ -68,24 +68,28 @@
 %0A%0Aconst 
-settings
+initialState
  = %7B%0A  m
@@ -143,369 +143,422 @@
 %7D;%0A%0A
-// cookie blocking safe localStorage settings persistence%0Afunction load() %7B%0A  try %7B%0A    assign(settings, JSON.parse(localStorage.getItem(SETTINGS_KEY)));%0A  %7D catch (e) %7B%0A    // Ok!%0A  %7D%0A%7D%0Afunction save() %7B%0A  try %7B%0A    localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings));%0A  %7D catch (e) %7B%0A    // Ok!%0A  %7D%0A%7D%0A%0Aconst SettingsStore = assign(new EventEmit
+function reduce(state = initialState, action = %7B%7D) %7B%0A  const %7B type, payload %7D = action;%0A  switch (type) %7B%0A  case 'loadSettings':%0A  case 'setSettings':%0A    return %7B ...state, ...payload %7D;%0A  default:%0A    return state;%0A  %7D%0A%7D%0A%0Aclass SettingsStore extends Store %7B%0A  constructor() %7B%0A    super();%0A    this.on('change', ::this.save);%0A    this.load();%0A  %7D%0A%0A  reduce(state, action) %7B%0A    return reduce(sta
 te
-r
 , 
-%7B
+action);%0A  %7D%0A
 %0A  g
@@ -582,34 +582,23 @@
 urn 
-assign(%7B%7D, settings)
+this.state
 ;%0A  %7D
-,
 %0A  g
@@ -626,24 +626,26 @@
  return 
-settings
+this.state
 %5Bname%5D;%0A
@@ -651,131 +651,186 @@
 %0A  %7D
-,
 %0A%0A  
-dispatchToken: dispatcher.register((%7B type, payload %7D) =%3E %7B%0A    switch (type) %7B%0A    case 'loadSettings':%0A    case 'set
+// localStorage settings persistence. try-catches are to deal with cookie%0A  // blocking.%0A  load() %7B%0A    try %7B%0A      this.state = this.reduce(this.state, %7B%0A        type: 'load
 Sett
@@ -838,182 +838,259 @@
 ngs'
-:
+,
 %0A      
-assign(settings, payload);%0A      SettingsStore.emit('change');
+  payload: JSON.parse(localStorage.getItem(SETTINGS_KEY))
 %0A      
-break
+%7D)
 ;%0A    
-default:%0A      // Not for us%0A    %7D%0A  %7D)%0A%7D);%0A%0ASettingsStore.on('change', save);%0Aload();
+%7D catch (e) %7B%0A      // Ok!%0A    %7D%0A  %7D%0A%0A  save() %7B%0A    try %7B%0A      localStorage.setItem(SETTINGS_KEY, JSON.stringify(this.state));%0A    %7D catch (e) %7B%0A      // Ok!%0A    %7D%0A  %7D%0A%7D
 %0A%0Aex
@@ -1102,16 +1102,20 @@
 default 
+new 
 Settings

rewrite SettingsStore as a reducer function