3.1.1 • Published 3 years ago

@tawaship/emitter v3.1.1

Weekly downloads
19
License
MIT
Repository
github
Last release
3 years ago

@tawaship/emitter

A little useful emitter.

Build Status MIT License


How to install

  1. on you npm project
npm install --save @tawaship/emitter
  1. on your code
import { Emitter } from '@tawaship/emitter';

// or

const { Emitter } = require('@tawaship/emitter');

How to build

git clone https://github.com/tawaship/Emitter

cd Emitter

npm install

npm run build

How to load on browser

After install or build

<script src="/path/to/dist/Emitter.min.js"></script>

Usage

Basic

const f = a => {
	console.log('ev', a);
};

new Emitter()
	.on('ev', f)
	.emit('ev', 1) // ev 1
	.off('ev', f)
	.emit('ev', 1) // (nothing)

Basic once

new Emitter()
	.once('ev', a => {
		console.log('ev', a);
	})
	.emit('ev', 1) // ev 1
	.emit('ev', 1) // (nothing)

Repeatable arguments

new Emitter()
	.on('ev', (...args) => {
		console.log('ev', ...args);
	})
	.emit('ev', 1, 2, 3, 4, 5, 6, 7) // ev 1 2 3 4 5 6 7

Specify the context

new Emitter()
	.on('ev', function(a) {
		console.log(this, a);
	})
	.cemit('ev', {hoge: 1}, 2) // {hoge: 1} 2

However, using the arrow function invalidates the context specification.

// on window
new Emitter()
	.on('ev', a => {
		console.log(this, a);
	})
	.cemit('ev', {hoge: 1}, 2) // Window 2

Events delete in groups

new Emitter()
	.on('ev', a => {
		console.log('ev', a);
	})
	.on('ev', a => {
		console.log('ev', a);
	})
	.on('ev2', a => {
		console.log('ev', a);
	})
	.on('ev2', a => {
		console.log('ev', a);
	})
	.clear('ev')
	.emit('ev', 1) // (nothing)
	.emit('ev2', 1) // ev 1, ev 1

All events delete at once

new Emitter()
	.on('ev', a => {
		console.log('ev', a);
	})
	.on('ev2', a => {
		console.log('ev', a);
	})
	.on('ev3', a => {
		console.log('ev', a);
	})
	.on('ev4', a => {
		console.log('ev', a);
	})
	.clear()
	.emit('ev', 1) // (nothing)
	.emit('ev2', 1) // (nothing)
	.emit('ev3', 1) // (nothing)
	.emit('ev4', 1) // (nothing)
3.1.1

3 years ago

3.1.0

3 years ago

3.0.0

4 years ago

2.1.1

4 years ago

2.0.2

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago