Skip to content

Instantly share code, notes, and snippets.

@sebbdk
Last active December 6, 2019 22:30
Show Gist options
  • Save sebbdk/7cea66720d35c2d5dd1c608f71dd3498 to your computer and use it in GitHub Desktop.
Save sebbdk/7cea66720d35c2d5dd1c608f71dd3498 to your computer and use it in GitHub Desktop.
Draft of how basic low level Redux filters could work
// ---> custom filtered sub-store
function createContentObservable(store) {
const contentSubs = {
X: []
}
const dispose = store.subscribe((cat) => {
if (store.getState().lastAction.type === 'content-change') {
const id = store.getState().lastAction.id;
const content = select(store, getContent(id))
contentSubs[id].forEach(s => s(content))
}
});
return {
subscribe: (name, cb) => {
contentSubs[name].push(cb);
// @todo return unsub
},
dispose
}
}
const contentStore = createContentStore(myStore);
const unSub = contentStore.subscribe('X', (content) => {
console.log(content);
});
// unSub when needed
// ---> path sub-store
function createPathObservable(path = [], store) {
const getVal = () => { path.reduce((acc, val) => acc[val], store.getState()) }
let preValue = getVal()
const contentSubs = [];
const dispose = store.subscribe((cat) => {
const val = getVal();
if (val !== preValue) {
contentSubs.forEach(s => s(val));
}
});
return {
subscribe: (cb) => {
contentSubs.push(cb);
// @todo return unsub
},
dispose
}
}
const contentStore = createContentStore(['somereducer', 'some-attribute'], myStore);
const unSub = contentStore.subscribe((content) => {
console.log(content);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment