Code Monkey home page Code Monkey logo

streamtoiter's Introduction

Async iterable stream

This is a tiny library for creating async iterable objects:

for await (const item of iter) {
  console.log('some async item')
}

from streams:

import { createIterableStream } from 'ministreamiterator'

const stream = createIterableStream()

// Send items into the stream
setInterval(() => stream.append(Date.now()), 1000)

// And consume them using a for-await loop:
for await (const time of stream.iter) {
  console.log('the time is', time)
}

Its similar to streamiterator, except:

  • There is no out-of-the-box iteroperability with nodejs stream objects (because node streams are big and complicated)
  • This library has no external dependancies. Minified and gzipped, this library adds only a couple hundred bytes to your bundle.

Lifecycle

The stream has 2 states: Open and closed. The stream can be closed either from the producer or the consumer.

To close the stream from the producer, call stream.end():

const stream = createIterableStream()
stream.append(1)
stream.append(2)
stream.end() // Producer indicates there are no more events to send into the stream

for await (const item of stream.iter) {
  console.log(item)
}
console.log('done')

// Prints 1, 2, done.

The consumer can indicate they are done consuming the stream by calling iter.return():

When the consumer closes the stream, the stream will call a registered onClosed function. This function is registered by passing it as the first (and only) argument to the streamToIter constructor. (Eg streamToIter(() => console.log('closed')))

NOTE: The onClosed function is only called if the consumer closes the stream via iter.return(). It is not called if the producer closes the stream via stream.end().

let timer
const stream = createIterableStream(() => {
  clearInterval(timer) // Stop firing the interval once the consumer has finished reading from the stream
})
timer = setInterval(() => stream.append(Date.now()), 1000)

for await (const item of stream.iter) {
  if (Math.random() < 0.5) break
}
stream.iter.return()

Handing errors

You can also inject errors into the stream. Errors will cause an exception to be thrown from the for await loop:

const stream = createIterableStream()
stream.throw(new Error('oops!'))

// ...


for await (const item of stream.iter) {

} // throws Error('oops!')!

Once an error is injected into the stream, the stream is considered to be done. No further events can be injected into the stream. The stream currently does call a registered onClose handler here - I'm not sure if thats the right call but this decision will not change without a bump to the major version of this library.

streamtoiter's People

Contributors

josephg avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

canadaduane

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.