-
-
Save aCandidMind/3eb8300a73a8b93666af4c88bc99b1d3 to your computer and use it in GitHub Desktop.
Get streams for interaction & mutation events
This file contains hidden or 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
import {Observable} from 'rx'; | |
import {DOM} from 'rx-dom'; | |
import {toArray, getNodesFromSelector} from './utils'; | |
let interactions_ = { | |
'click': Observable.fromEvent(document.body, 'click') | |
} | |
let mutations_ = DOM | |
.fromMutationObserver(document.body, { childList: true, subtree: true}) | |
.flatMap(mutations => Observable.from(mutations)); | |
let interactions = (eventType, selector) => { | |
return interactions_[eventType] | |
.filter(e => { | |
switch(selector[0]) { | |
case '#': return e.target.id === selector.slice(1); | |
case '.': return toArray(e.target.classList).indexOf(selector.slice(1)) >= 0; | |
default: return e.target.tagName === selector; | |
} | |
}); | |
} | |
let mutations = (selector, property) => { | |
return mutations_ | |
.flatMap(mutation => { | |
return Observable.from(mutation[property]); | |
}) | |
.filter(node => { | |
return node.contains(getNodesFromSelector(selector)); | |
}); | |
} | |
export default { | |
on: (eventType, selector) => { | |
switch(eventType) { | |
case 'mount': return mutations(selector, 'addedNodes'); | |
case 'unmount': return mutations(selector, 'removedNodes'); | |
default: return interactions(eventType, selector); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment