@basic-streams/protect
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@basic-streams/protect

protect<T>(stream: Stream<T>): StreamProtected<T>

Creates a protected stream that will contain same events as the given stream. When you use the protected stream, you don't have to follow the following rules from the protocol:

  • Stream must be called with one argument. You can pass extra arguments. They will be ignored.
  • cb must always return undefined. Your callback may return value of any type.
  • disposer must be called with no arguments. You can pass any arguments to the disposer. They will be ignored.
  • disposer must be called at most once. You can call disposer repeatedly. The second and following calls will have no effect.
import protect from "@basic-streams/protect"

const stream = (cb, ...extra) => {
  console.log("started", extra)
  console.log("callback returned", cb(1))
  return (...args) => {
    console.log("disposed", args)
  }
}

const result = protect(stream)

const disposer = result(x => {
  console.log("received event", x)
  return "should be ignored"
}, "should be ignored")

// > "started" []
// > "received event" 1
// > "callback returned" undefined

disposer()

// > "disposed" []

disposer()

// no output, the second call is ignored

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

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

import {StreamProtected} from "@basic-streams/protect"

Readme

Keywords

Package Sidebar

Install

npm i @basic-streams/protect

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

6.22 kB

Total Files

8

Last publish

Collaborators

  • pozadi