Skip to content

Instantly share code, notes, and snippets.

@evanplaice
Created June 24, 2019 18:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanplaice/77c70188b06ce3e6d67cbce2980cdea3 to your computer and use it in GitHub Desktop.
Save evanplaice/77c70188b06ce3e6d67cbce2980cdea3 to your computer and use it in GitHub Desktop.
PersistentStore
import { PubSub } from './pubsub.js';
export class PersistentStore {
actions = new Map();
state = {};
events = new PubSub();
constructor(name, params = {}) {
this.name = name;
if (params.actions) {
params.actions.map(action => {
this.actions.set(action[0], action[1]);
});
}
if (params.save) {
this.save = params.save;
}
if (params.load) {
this.load = params.load;
}
const initialState = (this.load(this.name))
? this.load(this.name) : params.state;
this.state = new Proxy(initialState, {
set: (state, key, value) => {
state[key] = value;
// persist state
if (this.save) {
this.save(this.name, this.state);
}
console.log(`stateChange: { ${key}: ${value} }`);
this.events.publish('stateChange', this.state);
return true;
}
});
}
dispatch(action, data) {
if (!this.actions.has(action)) {
console.error(`Action: ${action} doesn't exist`);
}
const newState = this.actions.get(action)(this.state, data);
this.state = Object.assign(this.state, newState)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment