# enhance-eventbus

> a enhance eventbus

Latest version **1.0.6** (published 2020-03-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install enhance-eventbus
pnpm add enhance-eventbus
yarn add enhance-eventbus
bun add enhance-eventbus
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2020-03-27 |
| First published | 2020-03-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Unpacked size | 58.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | cjfff |
| Maintainers | cjfff |
| Keywords | eventbus, vue eventbus, window communication, tab communication |

## Links

- npm: https://www.npmjs.com/package/enhance-eventbus
- npm.io page: https://npm.io/package/enhance-eventbus

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.0.6 (latest) — 2020-03-27
- 1.0.5 — 2020-03-26
- 1.0.4 — 2020-03-23
- 1.0.3 — 2020-03-23
- 1.0.2 — 2020-03-23
- 1.0.1 — 2020-03-23
- 1.0.0 — 2020-03-23

## README

# enhance-eventbus

[![NPM Download](https://badgen.net/npm/dm/enhance-eventbus)](https://www.npmjs.com/package/enhance-eventbus)
[![NPM Version](https://badgen.net/npm/v/enhance-eventbus)](https://www.npmjs.com/package/enhance-eventbus)
[![NPM License](https://badgen.net/npm/license/enhance-eventbus)](https://github.com/cjfff/enhance-eventbus/blob/master/LICENSE)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/cjfff/enhance-eventbus/pulls)
[![typescript](https://badgen.net/badge/icon/typescript?icon=typescript&label)](https://badgen.net/badge/icon/typescript?icon=typescript&label)
[![package size](https://badgen.net/bundlephobia/minzip/enhance-eventbus)](https://badgen.net/bundlephobia/minzip/enhance-eventbus)


## 介绍

`eventBus` 是一个典型的发布订阅模式的实践，而这个包的基础功能就是发布订阅。

[`eventBus`](http://nodejs.cn/api/events.html) 在 `node.js` 中本就自带，所以为什么需要写这个包呢？

其实从包名 `enhance-eventbus` 中也可以看出来，这是一个增强 (其实是不会起名字) 的 `eventBus`。

它比普通的 `eventBus` 多了可以在浏览器跨 `tab` (有同源限制)。

它的 `api` 跟普通的 `eventBus` 是一样的使用方式的，极其简单, 相信使用过 `vue` 里面 `eventBus`（ `new` 一个 `vue` 实例作为`eventBus`）的小伙伴们, 都知道怎么使用～


## example

![example](https://github.com/cjfff/enhance-eventbus/raw/master/screenshot/example.gif)

### StorageEventBus

这里实现原理是利用了 storage 去监听的，所以这里在相同一个 window tab 页面是无法触发事件的，需要开启别的 tab 才能通信。

```js
// yarn add enhance-eventbus or npm i -S enhance-eventbus

const eventBus = new tabeventbus.StorageEventBus({
  isDev: () => true
})

eventBus.on('someEvent', (data) => {

  console.log(data)
})

eventBus.emit('someEvent', {msg: 'hello world'})
```

## use in your vue project

```js
// in the main.js
import enhanceEventbus from "enhance-eventbus"
import Vue from "vue"

Vue.use(enhanceEventbus, {
  type: "storage",
  globalKey: "$eventBus" // 这里配置的是 vue.prototype 挂载的属性名叫什么
})

// and then you can use it by the gloalKey that your define

new Vue({

  mounted() {
    this.$eventBus.on('someEvent', (data) => {
      // ...to do sth
    })

    this.$eventBus.emit('someEvent', data)
  }
})
```


## 感谢以下项目（排名不分先后）
* [typescript-library-starter](https://github.com/alexjoverm/typescript-library-starter) 库的依赖有点老旧，升级下就能用了
* [typescript](https://github.com/microsoft/TypeScript)

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