# rx-event-bus

> 基于rxjs的轻量级event bus。使用场景：跨组件传递消息。

Latest version **0.0.4** (published 2018-11-20) · 0 weekly downloads

## Install

```sh
npm install rx-event-bus
pnpm add rx-event-bus
yarn add rx-event-bus
bun add rx-event-bus
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2018-11-20 |
| First published | 2018-11-20 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 57.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | coderpoet |

## Links

- npm: https://www.npmjs.com/package/rx-event-bus
- npm.io page: https://npm.io/package/rx-event-bus

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^1.9.0

## Recent versions

- 0.0.4 (latest) — 2018-11-20
- 0.0.3 — 2018-11-20
- 0.0.2 — 2018-11-20
- 0.0.1 — 2018-11-20

## README

基于rxjs的轻量级event bus。使用场景：跨组件传递消息。

`npm install rx-event-bus`

- 定义事件
```typescript
export enum DemoEvent {
  demoChange = 'DEMO:CHANGE'
}
```
- 发布消息
```typescript
this.eventBus.publish(DemoEvent.demoChange, {name: 'alvin'});
```
- 订阅消息
```typescript
    this.demo$ = this.eventBus.subscribe(DemoEvent.demoChange)
      .pipe(takeWhile(() => this.alive));
```

- angular中使用完整Demo：
```typescript
import {Component, OnDestroy, OnInit} from '@angular/core';
import {RxEventBusService} from './rx-event-bus.service';
import {takeWhile} from 'rxjs/operators';

export enum DemoEvent {
  demoChange = 'DEMO:CHANGE'
}

@Component({
  selector: 'lib-rx-event-bus-demo',
  template: `
    <div>{{(demo$ | async)?.name}}</div>
  `,
})
export class RxEventBusDemoComponent implements OnInit, OnDestroy {

  private alive = true;

  demo$;

  constructor(private eventBus: RxEventBusService) {
  }

  ngOnInit() {
    // 发布消息
    this.eventBus.publish(DemoEvent.demoChange, {name: 'alvin'});

    // 订阅消息
    this.demo$ = this.eventBus.subscribe(DemoEvent.demoChange)
      .pipe(takeWhile(() => this.alive));
  }

  ngOnDestroy(): void {
    this.alive = false;
  }

}

```

- 适配ng依赖注入体系，如果非ng，手动实例化RxEventBusService即可。

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