Last active
June 30, 2016 03:27
-
-
Save davidchase/832005d37a3b9a6e70be1bb6ac0e97d3 to your computer and use it in GitHub Desktop.
pull-stream for DOM 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
// 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()) |
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
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