npm.io
3.0.30 • Published 7 years ago

the-stream-base

Licence
MIT
Version
3.0.30
Deps
3
Size
128 kB
Vulns
0
Weekly
0
DeprecatedThis package is deprecated

the-stream-base

Build Status npm Version JS Standard

Base of the streams

Installation

$ npm install the-stream-base --save

Usage

'use strict'

const { TheStream } = require('the-stream-base')
const fs = require('fs')
const asleep = require('asleep')

async function tryExample () {

  class CountupStream extends TheStream {

    streamDidOpen () {
      this.counts = 0
    }

    streamWillClose () {
      console.log('Counts', this.counts)
    }

    // Consume values from `stream.push()`
    async consume (provided) {
      for await (const chunk of provided) {
        console.log('pushed', chunk)
        this.counts++
      }
    }

    // Provide values for `stream.pull()`
    async * provide () {
      do {
        yield await this.counts--
        console.log('pulled', this.counts)
      } while (this.counts > 0)
    }
  }

  const stream = new CountupStream({})

  await stream.open()

  for (let i = 0; i < 100; i++) {
    await stream.push(i * Math.random())
  }

  console.log(
    await stream.pull()
  )

  await stream.close()
}

tryExample().catch((err) => console.error(err))

API Guide

License

This software is released under the MIT License.

Keywords