# @lesomnus/channel

> TypeScript implementation of Go Channel

Latest version **0.0.1** (published 2022-10-08) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @lesomnus/channel
pnpm add @lesomnus/channel
yarn add @lesomnus/channel
bun add @lesomnus/channel
```

## Health

**Score 35/100 (D)** — status: abandoned.

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2022-10-08 |
| First published | 2022-10-08 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 108.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Seunghyun Hwang |
| Maintainers | lesomnus |
| Keywords | channel |

## Links

- npm: https://www.npmjs.com/package/@lesomnus/channel
- Repository: https://github.com/lesomnus/channel-ts
- Homepage: https://github.com/lesomnus/channel-ts#readme
- Issues: https://github.com/lesomnus/channel-ts/issues
- npm.io page: https://npm.io/package/@lesomnus/channel

## Recent versions

- 0.0.1 (latest) — 2022-10-08

## README

# channel

[![test](https://github.com/lesomnus/channel-ts/actions/workflows/test.yaml/badge.svg)](https://github.com/lesomnus/channel-ts/actions/workflows/test.yaml)
[![codecov](https://codecov.io/gh/lesomnus/channel-ts/branch/main/graph/badge.svg?token=sZSDd8Zzd7)](https://codecov.io/gh/lesomnus/channel-ts)

TypeScript implementation of [Go channel](https://go.dev/ref/spec#Channel_types).

## Features

-   Select
-   Async iterators
-   Bounded/Unbounded channels

## Usage

### Send/Recv

```ts
import { Chan } from '@lesomnus/channel'

const c = new Chan<number>(0) // Bounded channel with 0 size buffer.

setTimeout(async () => {
	// Blocked until recv() since the size of the buffer is 0.
	await c.send(42)
}, 100)

// Blocked until send(42).
const answer = await c.recv()

// After 100ms...
answer === 42 // true
```

```go
/* Golang equivalent. */

c := make(chan int)

go func(){ c <- 42 }()

answer := <- c
```

### Select

```ts
import { UnChan } from '@lesomnus/channel'
import { recv, send, select } from '@lesomnus/channel'

const c1 = new Chan<number>(0)
const c2 = new Chan<number>(0)

setTimeout(async () => await c2.send(42), 100)

// After 100ms, "c2 received 42" will be logged.
await select([
	recv(c1, (v) => console.log('c1 received', v)),
	recv(c2, (v) => console.log('c2 received', v)),
])

// This blocks forever because the receive from c1 is effectively cancelled.
await c1.send(36)
```

```go
/* Golang equivalent. */

c1 := make(chan int)
c2 := make(chan int)

go func(){ c2 <- 42 }()

select {
	case v := <- c1: fmt.Println("c1 received", v)
	case v := <- c2: fmt.Println("c2 received", v)
}
```

### Async Iterate

```ts
const c = Chan.from([42, 36], 2)

// Logs
// "received 42"
// "received 36"
// then blocked.
for await (const v of c){
	console.log('received', v)
}
```

```go
/* Golang equivalent. */

c := make(chan int, 2)
c <- 42
c <- 36

for v := range c {
	fmt.Println("received", v)
}
```

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