1.4.0 • Published 11 days ago

evenex v1.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

Evenex

An event bus library implemented in TypeScript.( English | 简体中文

  • Evenex: Derived from "Event" and "Nexus", it signifies the role of the event bus as a nexus connecting different events and dispatch centers.
  • Microscopic: It boasts a minuscule size, less than 1kb after compression.
  • Familiar: We've implemented a sensible API design akin to other EventBus libraries, ensuring an easy learning curve for users.

Install

Please make sure you install this library using npm or another package manager in a Node.js environment.

npm install --save evenex

Then, utilize modern module bundling tools such as Vite or Webpack to import this library using modular syntax.

// Using ES Module
import { CreateEvenex } from 'evenex'

// Using CommonJS
var { CreateEvenex } = require('evenex')

Usage

import { CreateEvenex } from 'evenex'
const evenex = CreateEvenex()

let count = 0
let setCount = (val) => (count = val)

const on1 = (num) => {
  setCount(num)
}
const on2 = (count) => {
  setTimeout(() => {
    console.log(`emit1-count:`, count)
  }, 100)
}

// Listen for events
evenex.on('changeCount', on1)
evenex.on('changeCount', on2)
evenex.on('test', on2)

setTimeout(() => {
  // Trigger events
  evenex.emit('changeCount', count + 1)

  console.log('has -> changeCount 01', evenex.has('changeCount'))

  // Unsubscribe from events
  evenex.off('changeCount', on1)
  evenex.off('changeCount', on2)

  // Check if an event exists
  console.log('has -> changeCount 02', evenex.has('changeCount'))

  console.log('events -> 01', evenex.events)

  // Clear all events
  evenex.clear()

  console.log('events -> 02', evenex.events)
}, 1000)

API

on

Register a handler for a specified event.

Parameters

  • type (string) The type of event to listen for.
  • handler (Function) The function to call when the specified event is received.
  • thisArg (any) The context to be used when calling the function.

off

Unsubscribe from handling a specific event.

Parameters

  • type (string) The type of event to unsubscribe from.
  • handler (Function) The handler function registered for the event.

emit

Trigger all handlers registered for the specified event.

Parameters

  • type (string) The type of event to trigger.
  • ...payload (any) Any arguments that need to be passed.

clear

Clear all handlers registered for events.

has

Check if the specified event exists

Parameters

  • type (string) The type of event to check.
1.4.0

11 days ago

1.3.1

13 days ago

1.3.0

15 days ago

1.2.0

22 days ago

1.2.1

22 days ago

1.1.2

2 months ago

1.1.1

2 months ago

1.1.0

2 months ago

1.0.25

2 months ago

1.0.24

2 months ago

1.0.23

2 months ago

1.0.22

2 months ago

1.0.21

2 months ago

1.0.20

2 months ago

1.0.19

2 months ago

1.0.18

2 months ago

1.0.16

2 months ago

1.0.15

2 months ago

1.0.14

2 months ago

1.0.13

2 months ago

1.0.1

2 months ago