Giter Site home page Giter Site logo

domstream's Introduction

domstream

Turn DOM element events into streams

Installation

$ component install jwerle/domstream

Usage

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)
  }
});

API

domstream(el)

Accepts a DOM Node and returns a readable/writable DOMStream influenced from node.js

var stream = domstream(document.getElementById('node'));

Events

'readable'

When there is data ready to be consumed, this event will fire.

'data'

Emitted when data is written to stream.

'end'

Emitted when the end of stream event has been emitted.

'error'

Emitted if there was an error receiving data.

#source(opts)

  • .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'});

#write(data)

Writes data to stream

stream.write({some: 'data'});

#push(data) | queue(data)

Pushes a chunk to the stream buffer

stream.push({data: 'for later'});

#unshift(data)

Unshifts a chunk to the stream buffer

stream.unshift({data: 'for later'});

#read(size, offset)

Reads a given optional size to read from the stream buffer

var data = stream.read(5);
var buf = stream.read();

#end(data)

Writes data to stream and emits end event

stream.end({even: 'more data'});

#use(fn)

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();
});

#through(write, end)

Defines a write and end handle for the stream handle

stream.through(
function write (data) {
  this.push(data);
},
function end () {
  console.log(this.read());
});

#pipe(dest, opts)

Pipes stream to a Writeable stream (lightly ported from node.js)

stream.pipe(otherStream);

License

MIT

domstream's People

Contributors

jwerle avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.