Skip to content

Instantly share code, notes, and snippets.

@davidchase
Last active June 30, 2016 03:27
Show Gist options
  • Save davidchase/832005d37a3b9a6e70be1bb6ac0e97d3 to your computer and use it in GitHub Desktop.
Save davidchase/832005d37a3b9a6e70be1bb6ac0e97d3 to your computer and use it in GitHub Desktop.
pull-stream for DOM events
// http://www.webpackbin.com/EJxIbvTSW
import pull from 'pull-stream'
import {click /*, mouseover, pullEvent*/ } from './pull-dom-events'
const {filter, map, take, through, drain} = pull
const throughs =
pull(
filter(event => event.target.matches('.only-me')),
map(event => event.target.textContent),
through(data => console.count(data)),
take(10)
)
pull(click(document), throughs, drain())
export const pullEvent = function pullEvent(type, elm, capture = false) {
let callback
const handler = function(evnt) {
if (callback) {
return callback(null, evnt)
}
}
elm.addEventListener(type, handler, capture)
return function read(end, next) {
if (end) {
elm.removeEventListener(type, handler, capture)
return next(end)
}
callback = next
}
}
export const click = (elem, capture) => pullEvent('click', elem, capture)
export const mouseover = (elem, capture) => pullEvent('mouseover', elem, capture)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment