Socket
Socket
Sign inDemoInstall

small-emitter

Package Overview
Dependencies
0
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.4 to 0.1.5

README.en.md

3

dist/index.d.ts

@@ -5,3 +5,3 @@ interface EmitterCallback {

}
declare class Emitter {
export declare class Emitter {
events: {

@@ -16,2 +16,3 @@ [key: string]: EmitterCallback[];

}
export declare const useEmitter: () => Emitter;
export default Emitter;

@@ -52,2 +52,3 @@ var __defProp = Object.defineProperty;

}
export { Emitter as default };
const useEmitter = () => new Emitter();
export { Emitter, Emitter as default, useEmitter };

@@ -1,1 +0,1 @@

var h=Object.defineProperty;var c=(i,s,e)=>s in i?h(i,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[s]=e;var u=(i,s,e)=>(c(i,typeof s!="symbol"?s+"":s,e),e);(function(i,s){typeof exports=="object"&&typeof module!="undefined"?module.exports=s():typeof define=="function"&&define.amd?define(s):(i=typeof globalThis!="undefined"?globalThis:i||self,i.emitter=s())})(this,function(){"use strict";class i{constructor(){u(this,"events",{})}on(e,n){var o;const t=this.events;t[e]=t[e]||[],(o=t[e])==null||o.push(n)}once(e,n){const t=(...o)=>{this.off(e,t),n(...o)};return t._=n,this.on(e,t)}emit(e,...n){const t=this.events;if(!!t[e])for(const o of t[e])o(...n)}off(e,n){var r;const t=this.events;if(!t[e])return;const o=[];if(t[e]&&n)for(const f of t[e])f===n||f._===n||f&&o.push(f);((r=t[e])==null?void 0:r.length)?t[e]=o:delete t[e]}clear(){this.events={}}}return i});
(function(s,i){typeof exports=="object"&&typeof module!="undefined"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(s=typeof globalThis!="undefined"?globalThis:s||self,i(s.emitter={}))})(this,function(s){"use strict";var c=Object.defineProperty;var h=(s,i,f)=>i in s?c(s,i,{enumerable:!0,configurable:!0,writable:!0,value:f}):s[i]=f;var d=(s,i,f)=>(h(s,typeof i!="symbol"?i+"":i,f),f);class i{constructor(){d(this,"events",{})}on(t,n){var o;const e=this.events;e[t]=e[t]||[],(o=e[t])==null||o.push(n)}once(t,n){const e=(...o)=>{this.off(t,e),n(...o)};return e._=n,this.on(t,e)}emit(t,...n){const e=this.events;if(!!e[t])for(const o of e[t])o(...n)}off(t,n){var r;const e=this.events;if(!e[t])return;const o=[];if(e[t]&&n)for(const u of e[t])u===n||u._===n||u&&o.push(u);(r=e[t])!=null&&r.length?e[t]=o:delete e[t]}clear(){this.events={}}}const f=()=>new i;s.Emitter=i,s.default=i,s.useEmitter=f,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
{
"name": "small-emitter",
"version": "0.1.4",
"version": "0.1.5",
"license": "MIT",

@@ -5,0 +5,0 @@ "author": "Lv Heng",

# Emitter
[简体中文](./README.zh-CN.md) | English
简体中文 | [English](./README.en.md)
A small event emitter library.
一个很小的 event emitter 库。
## Usage
## 安装
使用 npm 安装
```sh
npm i small-emitter
# or yarn add small-emitter
```
在项目中导入
```js
// ES6 modules
import Emitter from 'small-emitter'
// CommonJS modules
const Emitter = require('small-emitter')
```
## 用法
```js
import Emitter from 'small-emitter'
const emitter = new Emitter();
// subscribe to events
// 订阅事件
emitter.on('foo', function (p1, p2, p3) {

@@ -18,3 +37,3 @@ // ...

// subscribe the event only once
// 只订阅一次事件
emitter.once('foo', function (p1, p2, p3) {

@@ -24,9 +43,9 @@ // ...

// publish events
// 发布事件
emitter.emit('foo', 'p1', 'p2', 'p3');
// clearing all events
// 清空所有事件
emitter.clear()
// use reference functions
// 使用函数引用
function fn() {}

@@ -41,108 +60,108 @@ emitter.on('foo', fn)

- **Arguments**
**参数**
- `name` name of the event
- `callback` event's callback function
- `name` 事件名
- `callback` 事件的回调函数
- **Example**
**示例**
```js
const emitter = new Emitter()
```js
const emitter = new Emitter()
emitter.on('foo', (data) => {
console.log(data)
})
emitter.on('foo', (data) => {
console.log(data)
})
emitter.emit('foo', 1) // 1
emitter.emit('foo', 2) // 2
```
emitter.emit('foo', 1) // 1
emitter.emit('foo', 2) // 2
```
### `once(name: string, callback: () => void)`
- **Arguments**
**参数**
- `name` name of the event
- `callback` event's callback function
- `name` 事件名
- `callback` 事件的回调函数
- **Example**
**示例**
```js
const emitter = new Emitter()
```js
const emitter = new Emitter()
emitter.once('foo', (data) => {
// The function will only be executed once
console.log(data)
})
emitter.once('foo', (data) => {
// 这个函数只会执行一次
console.log(data)
})
emitter.emit('foo', 'a') // 'a'
emitter.emit('foo', 'b') // Not output
```
emitter.emit('foo', 'a') // 'a'
emitter.emit('foo', 'b') // Not output
```
### `emit(name: string, ...args: any[])`
- **Arguments**
**参数**
- `name` name of the event
- `...args (optional)` parameters passed in by the execution event
- `name` 事件名
- `...args (optional)` 执行事件时传入的参数
- **Example**
**示例**
```js
const emitter = new Emitter()
```js
const emitter = new Emitter()
emitter.once('bar', (message, name) => {
console.log(`${message}, ${name}`)
})
emitter.once('bar', (message, name) => {
console.log(`${message}, ${name}`)
})
emitter.emit('bar', 'hi', 'Jack') // hi, Jack
```
emitter.emit('bar', 'hi', 'Jack') // hi, Jack
```
### `off(name: string, callback?: () => void)`
- **Arguments**
**参数**
- `name` name of the event
- `callback (optional)` functions that need to be cleared or function reference
- `name` 事件名
- `callback (optional)` 需要清除的函数或函数引用
- **Example**
**示例**
```js
const emitter = new Emitter()
```js
const emitter = new Emitter()
const fn = (message) => {
console.log(message)
}
const fn = (message) => {
console.log(message)
}
emitter.on('bar', fn)
emitter.emit('bar', 'hi') // hi
emitter.off('bar', fn)
emitter.emit('bar', 'Jack') // Not output
```
emitter.on('bar', fn)
emitter.emit('bar', 'hi') // hi
emitter.off('bar', fn)
emitter.emit('bar', 'Jack') // Not output
```
### `clear()`
- Calling this method will clear all events
调用此方法将清除所有事件
- **Example**
**示例**
```js
const emitter = new Emitter()
```js
const emitter = new Emitter()
emitter.on('foo', (value) => {
console.log(value)
})
emitter.on('foo', (value) => {
console.log(value)
})
emitter.on('bar', (value) => {
console.log(value)
})
emitter.on('bar', (value) => {
console.log(value)
})
emitter.emit('foo', 'hi, foo')
emitter.emit('bar', 'hi, bar') // hi, bar
emitter.clear()
emitter.emit('foo', 'hi, Jack') // Not output
emitter.emit('bar', 'hi, Tom') // Not output
```
emitter.emit('foo', 'hi, foo')
emitter.emit('bar', 'hi, bar') // hi, bar
emitter.clear()
emitter.emit('foo', 'hi, Jack') // Not output
emitter.emit('bar', 'hi, Tom') // Not output
```
## License
## 开源协议
[MIT License](https://opensource.org/licenses/MIT)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc