event-cycle
TypeScript icon, indicating that this package has built-in type declarations

0.6.1 • Public • Published

EventCycle

Simple and flexsible event trigger, Written by TypeScript.

  • 💪 All case in test cover

Install

npm i event-cycle

Usage

Simply initialize by Singleton Pattern:

// eventCycle.ts
import EventCycle from 'event-cycle';

export default new EventCycle();

Then use it in any function:

import eventCycle from './eventCycle';

// somewhere to subscribe event
eventCycle.on('hello', (data: string) => console.log(`hello ${data || 'world'}`));

// somewhere to  publish event
eventCycle.emit('hello'); // print 'hello world'
eventCycle.emit('hello', 'vv13'); // print 'hello vv13'

Api

on(type, handler)

  • arguments
    • type: string, subscriber event name, allow duplicate
    • handler: (data?: any) => void, event subscription callback, accept a data by emit

onAll(handler)

  • arguments
    • handler: (type: string, data?: any) => void, every event publisher will trigger this callback, it receive event type and data by emit

once(type, handler)

  • arguments
    • type: string, subscriber event name, only trigger once, after that it will auto-cancel subscribe
    • handler: (data?: any) => void, event subscription callback, accept a data by emit

emit(type, data)

  • arguments
    • type: string, publisher event name, it will be trigger subscription callback
    • data: any, event data can be used by subscription callback

off(type, handler)

  • arguments
    • type: string, event name who will be removed
    • handler?: (event?: any) => void;, specific removing function, if it's not provided, off will default remove all subscriber in such type.

offAll(handler)

  • arguments
    • handler?: (event?: any) => void;, remove the handler from global subscriber, if handler not provided, offAll will remove all subscriber.

Package Sidebar

Install

npm i event-cycle

Weekly Downloads

7

Version

0.6.1

License

MIT

Unpacked Size

6.77 kB

Total Files

5

Last publish

Collaborators

  • zwhvv13