# @ceramicnetwork/transport-subject

> Message-based communication transport as a [RxJS Subject](https://rxjs.dev/api/index/class/Subject)

Latest version **0.6.1** (published 2022-03-25) · (Apache-2.0 OR MIT) license · 0 weekly downloads

## Install

```sh
npm install @ceramicnetwork/transport-subject
pnpm add @ceramicnetwork/transport-subject
yarn add @ceramicnetwork/transport-subject
bun add @ceramicnetwork/transport-subject
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.1 |
| Published | 2022-03-25 |
| First published | 2021-03-08 |
| Weekly downloads | 0 |
| License | (Apache-2.0 OR MIT) |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.14 |
| Dependencies | 1 |
| Unpacked size | 11.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | 3Box Labs |
| Maintainers | npm.3box, smrz2001, oed, zachferland, paul_lecam, stbrody, ukstv |
| Keywords | rxjs, transport |

## Links

- npm: https://www.npmjs.com/package/@ceramicnetwork/transport-subject
- Repository: https://github.com/ceramicnetwork/js-transports
- Homepage: https://github.com/ceramicnetwork/js-transports#readme
- Issues: https://github.com/ceramicnetwork/js-transports/issues
- npm.io page: https://npm.io/package/@ceramicnetwork/transport-subject

## Dependencies (1)

- [rxjs](https://npm.io/package/rxjs.md) ^7.5.2

## Recent versions

- 0.6.1 (latest) — 2022-03-25
- 0.6.0 — 2022-01-17
- 0.4.0 — 2021-09-29
- 0.3.0 — 2021-03-30
- 0.2.0 — 2021-03-16
- 0.1.0 — 2021-03-08

## README

# TransportSubject

Message-based communication transport as a [RxJS Subject](https://rxjs.dev/api/index/class/Subject)

## Installation

```sh
npm install @ceramicnetwork/transport-subject
```

## Usage

```ts
import { TransportSubject } from '@ceramicnetwork/transport-subject'
import { Subscriber, interval } from 'rxjs'
import { map } from 'rxjs'

type Message = { type: string }

class MyTransport extends TransportSubject<Message> {
  constructor(time = 1000) {
    const source = interval(time).map(() => ({ type: 'ping' }))
    const sink = new Subscriber((message) => {
      console.log('send message', message)
    })
    super(source, sink)
  }
}

const transport = new MyTransport()
transport.subscribe((message) => {
  console.log('received message', message)
})
transport.next({ type: 'pong' })
```

## Types

### Wrapped

```ts
type Wrapped<Message, Namespace extends string> = { __tw: true; msg: Message; ns: Namespace }
```

### Wrapper

```ts
type Wrapper<MsgIn, MsgOut, WrappedOut> = {
  wrap: (msg: MsgOut) => WrappedOut
  unwrap: (input: any) => MsgIn
}
```

### UnwrapOperatorOptions

```ts
type UnwrapOperatorOptions = {
  onInvalidInput?: (input: unknown, error: Error) => void
  throwWhenInvalid?: boolean
}
```

## API

### TransportSubject class

Extends [RxJS Subject class](https://rxjs.dev/api/index/class/Subject)

**Type parameters**

1. `MsgIn`: the type of the messages coming in from the `source`
1. `MsgOut = MsgIn`: the type of the messages going out to the `sink`

#### new TransportSubject()

**Arguments**

1. [`source: Observable<MsgIn>`](https://rxjs.dev/api/index/class/Observable)
1. [`sink: Observer<MsgOut>`](https://rxjs.dev/api/index/interface/Observer)

#### .next()

**Arguments**

1. `message: MsgOut`

**Returns** `void`

### createWrap()

**Type parameters**

1. `MsgOut`
1. `Namespace extends string = string`

**Arguments**

1. `namespace: Namespace`

**Returns** `(msg: MsgOut) => Wrapped<MsgOut, Namespace>`, see [Wrapped](#wrapped) type

### createUnwrap()

**Type parameters**

1. `MsgIn`
1. `Namespace extends string = string`

**Arguments**

1. `namespace: Namespace`

**Returns** `(input: any) => MsgIn`

### createWrapper()

Combines [`createWrap()`](#createwrap) and [`createUnwrap()`](#createunwrap)

**Type parameters**

1. `MsgIn`: the type of the messages coming in from the returned transport
1. `MsgOut = MsgIn`: the type of the messages pushed to the returned transport
1. `Namespace extends string = string`

**Arguments**

1. `namespace: Namespace`

**Returns** `Wrapper<MsgIn, MsgOut, Wrapped<MsgOut, Namespace>>`, see [Wrapper](#wrapper) and [Wrapped](#wrapped) types

### createUnwrapOperator()

**Type parameters**

1. `WrappedIn`: the type of the messages coming in from the input `source`
1. `MsgIn`: the type of the messages coming in from the returned observable

**Arguments**

1. `unwrap: (input: any) => MsgIn`
1. [`options?: UnwrapOperatorOptions = {}`](#unwrapoperatoroptions)

**Returns** `OperatorFunction<WrappedIn, MsgIn>`

### createWrapObserver()

**Type parameters**

1. `MsgOut`: the type of the messages pushed to the returned observer
1. `WrappedOut`: the type of the messages going out to the input `sink`

**Arguments**

1. `sink: Observer<WrappedOut>`
1. `wrap: (msg: MsgOut) => WrappedOut`

**Returns** `Observer<MsgOut>`

### createWrappedTransport()

Combines [`createUnwrapObservable()`](#createunwrapobservable) and [`createWrapObserver()`](#createwrapobserver) in a [`TransportSubject`](#transportsubject-class)

**Type parameters**

1. `MsgIn`: the type of the messages coming in from the returned transport
1. `MsgOut`: the type of the messages pushed to the returned transport
1. `WrappedIn`: the type of the messages coming in from the input `transport` source
1. `WrappedOut = WrappedIn`: the type of the messages going out to the input `transport` sink

**Arguments**

1. [`transport: TransportSubject<WrappedIn, WrappedOut>`](#transportsubject-class)
1. [`wrapper: MessageWrapper<MsgIn, MsgOut, WrappedOut>`](#messagewrapper)
1. [`options?: UnwrapObservableOptions = {}`](#unwrapobservableoptions)

**Returns** [`TransportSubject<MsgIn, MsgOut>`](#transportsubject-class)

### createNamepacedTransport()

Combines [`createWrappedTransport()`](#createwrappedtransport) and [`createWrapper()`](#createwrapper)

**Type parameters**

1. `MsgIn`: the type of the messages coming in from the returned transport
1. `MsgOut = MsgIn`: the type of the messages pushed to the returned transport
1. `Namespace extends string = string`

**Arguments**

1. `transport TransportSubject<Wrapped<MsgIn, Namespace>, Wrapped<MsgOut, Namespace>>>`
1. `namespace: Namespace`
1. [`options?: UnwrapOperatorOptions = {}`](#unwrapoperatoroptions)

**Returns** [`TransportSubject<MsgIn, MsgOut>`](#transportsubject-class)

## License

Apache-2.0 OR MIT

---
_Source: https://npm.io/package/@ceramicnetwork/transport-subject · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
