Turn DOM element events into streams
$ component install jwerle/domstream
binding the 'mousemove' and 'mouseout' events as starting and ending points to a stream on a DOM element
var domstream = require('domstream')
, el = document.getElementById('el')
, stream = domstream(el).source({start: 'mousemove', end: 'mouseout'})
stream.through(
function write (data) {
this.push({x:data.x, y:data.y});
},
function end (buf) {
for (var i = 0; i < buf.length; ++i) {
var d = buf.shift()
console.log(d.x, d.y)
}
});
The above is just short hand for:
stream.on('data', function (data) {
stream.push({x: data.x, y: data.y})
});
stream.on('end', function () {
var buf = stream.read();
for (var i = 0; i < buf.length; ++i) {
var d = buf.shift()
console.log(d.x, d.y)
}
});
Accepts a DOM Node and returns a readable/writable DOMStream
influenced from node.js
var stream = domstream(document.getElementById('node'));
When there is data ready to be consumed, this event will fire.
Emitted when data is written to stream.
Emitted when the end of stream event has been emitted.
Emitted if there was an error receiving data.
.start
- The event that when emitted instantiates the 'data' event of the stream.end
- The event that when emitted instantiates the end of the stream which will emit the 'end' event
stream.source({start: 'dragstart', end: 'dragend'});
Writes data to stream
stream.write({some: 'data'});
Pushes a chunk to the stream buffer
stream.push({data: 'for later'});
Unshifts a chunk to the stream buffer
stream.unshift({data: 'for later'});
Reads a given optional size to read from the stream buffer
var data = stream.read(5);
var buf = stream.read();
Writes data to stream and emits end event
stream.end({even: 'more data'});
Pushes a function to the startStack
array for acting like middle ware to data emitted from for the start event defined with bind()
or source()
stream.use(function (data, next) {
data.property = "value";
next();
});
Defines a write and end handle for the stream handle
stream.through(
function write (data) {
this.push(data);
},
function end () {
console.log(this.read());
});
Pipes stream to a Writeable stream (lightly ported from node.js)
stream.pipe(otherStream);
MIT