Giter Club home page Giter Club logo

happy-event-bus's Introduction

happy-event-bus

Github Actions Test codecov.io npm version file size license MIT

Event bus util and support symbol/string/number.

  • Typescript
  • Can be run in a browser or Node.js(works in any JavaScript runtime)
  • Support multiple event types(symbol/string/number)
  • Easy and quickly cancel listen

install

npm
npm i happy-event-bus

# or

yarn add happy-event-bus
CDN

UMD build

<script src="https://unpkg.com/happy-event-bus/dist/index.umd.js"></script>

use

init
// import and init
import Emitter from 'happy-event-bus'
const emitter = new Emitter()
// or export emitter
export default emitter
on and emit
import emitter from './emitter.js'

// string
emitter.on('anyone', (...item) => { console.log(item) })
emitter.emit('anyone', 'foo') // -> ['foo']
emitter.emit('anyone', 'foo', 'bar') // ['foo', 'bar']

// number
const ONE = 1
emitter.on(ONE, () => {})
emitter.emit(ONE)

// symbol
const foo = Symbol('foo')
emitter.on(foo, (item) => { console.log(item) }) // -> foo
emitter.emit(foo, 'foo')
once
import emitter from './emitter.js'
// remove auto after emit
emitter.once('foo')
quickly cancel listen

on/once will return a function and can be off himself

import emitter from './emitter.js'

const cb = () => {}
const fooListenStopHandle = emitter.on('foo', cb)
// off
fooListenStopHandle()
emitter.emit('foo') // -> foo cb never call

const barCb = () => {}
const barListenStopHandle = emitter.once('bar', barCb)
// off
barListenStopHandle()
emitter.emit('bar') // -> bar cb never call
off
import emitter from './emitter.js'

// off callback
const cb = () => {}
emitter.on('foo', cb)
emitter.off('foo', cb)

// off foo callback
emitter.off('foo')

// off all item
emitter.off()

LICENSE

MIT

Copyright (c) 2020 唐道海

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.