1.2.0 • Published 4 years ago

@jacobbubu/pull-pushable-duplex v1.2.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

@jacobbubu/pull-pushable-duplex

Build Status Coverage Status npm

A helper class for constructing full/half pushable duplex in pull-stream manner.

Intro.

Constructing a full-duplex, pushable duplex requires complex state management considerations.

Especially, you need to make sure that you follow the guidelines of the pull-stream (pull-stream-protocol-checker).

Usage

import * as pull from 'pull-stream'
import { PushableDuplex } from '@jacobbubu/pull-pushable-duplex'

function valuesToRead<T>(values: T[] = []) {
  let i = 0
  return (cb: OnReadCallback<T>) => {
    i === values.length ? cb(true) : cb(null, values[i])
    i += 1
  }
}

const results: any[] = []
const d = new PushableDuplex({
  allowHalfOpen: true,
  onRead: valuesToRead([1, 2, 3]),
  onReceived: (data) => {
    results.push(data)
  },
  onFinished: (err) => {
    console.log("we've" got, results)
  },
})
const peer = {
  source: pull.values(['a', 'b', 'c']),
  sink: pull.collect((err, results) => {
    console.log("peer's got", results)
  })
}
pull(d, peer, d)