Last active
December 6, 2019 22:30
-
-
Save sebbdk/7cea66720d35c2d5dd1c608f71dd3498 to your computer and use it in GitHub Desktop.
Draft of how basic low level Redux filters could work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---> 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