# @xizher/observer

> Observer: 监听者

Latest version **1.1.1** (published 2021-04-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install @xizher/observer
pnpm add @xizher/observer
yarn add @xizher/observer
bun add @xizher/observer
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2021-04-11 |
| First published | 2021-04-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 25.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | wuxizhe |
| Maintainers | xizher |

## Links

- npm: https://www.npmjs.com/package/@xizher/observer
- npm.io page: https://npm.io/package/@xizher/observer

## Recent versions

- 1.1.1 (latest) — 2021-04-11
- 1.1.0 — 2021-04-11
- 1.0.3 — 2021-04-09
- 1.0.2 — 2021-04-04
- 1.0.1 — 2021-04-03
- 1.0.0 — 2021-04-03

## README

# @xizher/observer

## 安装

```bash
npm install @xizher/observer
```

## 对象模型

| 类型成员     | 描述                       |
| ------------ | -------------------------- |
| - _eventPool | 监听处理函数存储池         |
| + on ()      | 绑定监听函数               |
| + off ()     | 移除监听函数               |
| + fire ()    | 触发监听函数               |
| + onve ()    | 绑定监听函数（仅监听一次） |

## 使用

```typescript
import Observer from '@xizher/observer'

class TestClass extends Observer<{
  'inc': { value: number },
  'dev': { val: number }
}> {
  private _value = 1
  constructor () {
    super()
  }
  inc () : void {
    this._value++
    this.fire('inc', {
      value: this._value
    })
  }
  dec () : void {
    this._value--
    this.fire('dev', {
      val: this._value
    })
  }
}

```

```javascript
const testObj = new TestClass()
let count = 0
testObj.on('inc', () => {
  count++
})
testObj.on('dev', () => {
  count++
})
testObj.inc() // +1
testObj.inc() // +1
testObj.inc() // +1
testObj.dec() // +1
testObj.dec() // +1
// output: count = 5
```

```javascript
const testObj = new TestClass()
let count = 0
const handler = testObj.on('inc', () => {
count++
})
testObj.on('dev', () => {
count++
})
testObj.inc() // +1
testObj.inc() // +1
handler.remove()
testObj.dec() // +1
testObj.inc()
testObj.dec() // +1
// output: count = 4
```

```javascript
const testObj = new TestClass()
let count = 0
testObj.on('inc', () => {
  count++
})
testObj.on('inc', () => {
  count++
})
testObj.on('dev', () => {
  count++
})
testObj.inc() // +2
testObj.dec() // +1
testObj.off('inc')
testObj.inc()
testObj.inc()
testObj.dec() // +1
// output: count = 4
```

```javascript
const testObj = new TestClass()
let count = 0
const handler = testObj.on('inc', () => {
  count++
})
testObj.off('inc')
handler.remove()
testObj.inc()
// output: count = 0
```

```javascript
const testObj = new TestClass()
let count = 0
const handler = testObj.on('inc', () => {
  count++
  handler.remove()
})
testObj.inc()
testObj.inc()
// output: count = 1
```

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