@basic-streams/repair

repair operator for basic-streams

Usage no npm install needed!

<script type="module">
  import basicStreamsRepair from 'https://cdn.skypack.dev/@basic-streams/repair';
</script>

README

@basic-streams/repair

repair<T>(streamLoose: StreamLoose<T>): Stream<T>

Creates a stream from a loose stream that may not follow all the requirements of the protocol. The loose stream is allowed to:

  1. Return not a function. If the return value is not a function, it will be ignored.
  2. Pass more than one argument to the callback. The resulting stream will pass only the first argument to its callback.
  3. Disposer may return value of any type. The resulting stream's disposer will always return undefined.
  4. Call the callback after disposer was called. The resulting stream will ignore these calls.
import repair from "@basic-streams/repair"

const stream = repair(cb => {
  // extra arguments will be ignored
  cb(1, "extra")

  // we don't have to return a function
  return null
})

const unsubscribe = stream((...args) => {
  console.log(...args)
})

unsubscribe()

// > 1

The type StreamLoose defined as follows, and you can import it from @basic-streams/repair.

type StreamLoose<T> = (cb: (payload: T, ...rest: any[]) => void) => any

import {StreamLoose} from "@basic-streams/repair"