Skip to content

Instantly share code, notes, and snippets.

@mugi-uno
Created September 23, 2017 14:24
Show Gist options
  • Save mugi-uno/e4589a03609a3e4163a8c6ea9148c8cc to your computer and use it in GitHub Desktop.
Save mugi-uno/e4589a03609a3e4163a8c6ea9148c8cc to your computer and use it in GitHub Desktop.
light_flux
import { EventEmitter } from 'events';
import _ from 'lodash';
export const emitter = new EventEmitter();
export const createStore = (updater) => {
let store = {};
emitter.on('pub', (...args) => {
const beforeStore = store;
store = updater(store, ...args);
if (!_.isEqual(beforeStore, store)) {
console.log('[store updated / before]', beforeStore);
console.log('[store updated / after]', store);
emitter.emit('sub', store, beforeStore);
}
});
emitter.on('force', () => {
emitter.emit('sub', store, store);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment