# snow-bus

> 订阅发布模式

Latest version **2.0.1** (published 2021-04-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install snow-bus
pnpm add snow-bus
yarn add snow-bus
bun add snow-bus
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2021-04-19 |
| First published | 2019-12-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 2.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | 蓝枫 |
| Maintainers | lanfeng_snow |

## Links

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

## Recent versions

- 2.0.1 (latest) — 2021-04-19
- 1.0.2 — 2019-12-11
- 1.0.1 — 2019-12-11

## README

# 订阅发布模式
/**
 * Created by 蓝枫 on 2019/5/21.
 * @desc 订阅发布模式。
 */


### 存放 订阅消息
let _listeners = {};

let Bus = {
  ### 添加 订阅
  watch(target, callback) {
    if (!_listeners[target]) {
      _listeners[target] = [];
    }
    _listeners[target].push(callback);
  },
  ### 发布信息
  trigger(target, ...args) {
    if (!_listeners[target]) return false;
    _listeners[target].forEach(listener => {
      // listener.apply(this, args)
      // 20210419 改动
      listener(args) 
    })
  },
  ### 移除订阅
  unwatch(target, callback) {
    if (!_listeners[target]) return false;
    if (!callback) {
      _listeners[target] = [];
    } else {
      _listeners[target].forEach((listener, index) => {
        if (listener == callback) _listeners[target].splice(index, 1);
      })
    }
  }
}

module.exports = Bus;

# 使用
npm install snow-bus --save

vue项目中 main.js 引入
import Bus from 'snow-bus'
<!-- Vue.prototype.$bus = Bus; -->
<!-- 20210419 改为use使用 -->
Vue.use(Bus)

组件中使用
订阅
this.$bus.watch("ADD_TODE", this.addTodo);
发布
this.$bus.trigger('ADD_TODE',item);

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