Giter Club home page Giter Club logo

erre's Introduction

erre.js

Build Status

NPM version NPM downloads MIT License

Description

Erre is a modern, performant and tiny (~0.5kb minified) streams script using generators that runs on modern browsers and node. It can be used to manage any kind of sync and async event series and it's inspired to bigger libraries like:

Usage

You can use it to create and manipulate simple event streams

import erre from 'erre'

const stream = erre(
  string => string.toUpperCase(),
  string => [...string].reverse().join('')
)

stream.onValue(console.log) // EVOL, ETAH

stream.push('love')
stream.push('hate')

It supports async and sync event chains thanks to ruit

const userNamesStream = erre(
  async user => await patchUsers(user), // async function returning a users collection
  users => users.map(user => user.name)
)

userNamesStream.onValue(console.log) // ['John'...]

userNamesStream.push({
  name: 'John',
  role: 'Doctor',
  age: 24
})

API

erre(...functions)

@returns stream

Create an erre stream object. The initial functions list is optional and it represents the chain of async or sync events needed to generate the final stream output received via onValue callbacks

stream

stream.push(value)

@returns stream

Push a new value into the stream that will be asynchronously modified and returned as argument to stream.onValue method

Example
const stream = erre()
stream.onValue(console.log) // 1
stream.push(1)

stream.onValue(callback)

@returns stream

Add a callback that will be called receiving the output of the stream asynchronously

Example
const stream = erre(val => val + 1)

stream.onValue(console.log) // 2
stream.onValue(val => console.log(val * 2)) // 4

stream.push(1)

stream.onError(callback)

@returns stream

Add a callback that will be called in case of errors or promise rejections during the output generation

Example
const stream = erre(val => {
  throw 'error'
})

stream.onValue(console.log) // never called!!
stream.onError(console.log) // 'error'

stream.push(1)

stream.onEnd(callback)

@returns stream

Add a callback that will be called when the stream will be ended

Example
const stream = erre()

stream.onEnd(() => console.log('ended!')) // ended!

stream.end()

stream.connect(function)

@returns stream

Enhance the stream adding a new operation to the functions chain to generate its output

Example
const stream = erre(val => val + 1)

stream.onValue(console.log) // 2, 4
stream.push(1)

// enhance the stream
stream.connect(val => val * 2)
stream.push(1)

stream.end()

@returns stream

End the stream

Example
const stream = erre(val => val + 1)

stream.onValue(console.log) // 2
stream.push(1)

// end the stream
stream.end()

// no more events
stream.push(1)
stream.push(1)
stream.push(1)

stream.end()

@returns new stream object

Create a new stream object inheriting the function chain from its parent

Example
const stream = erre(val => val + 1)

stream.onValue(console.log) // 2, 3
stream.push(1)

const fork = stream.fork()
fork.onValue(console.log)
fork.connect(val => val * 10) // 20, 60

// 2 independent streams
fork.push(1)
stream.push(2)
fork.push(5)

erre.cancel()

Static function that if returned by any of the stream functions chain can be used to filter or stop the computation

Example
const stream = erre(val => {
  if (typeof val !== 'number') return erre.cancel()
  return val + 1
})

stream.onValue(console.log) // 2, 3
stream.push(1)
stream.push('foo') // filtered
stream.push('1') // filtered
stream.push(2)

TODO List

  • erre.fromDOM - to stream DOM nodes events
  • erre.merge - to merge multiple stream results into one

erre's People

Contributors

gianlucaguarini 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.