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

    0.6.17 • Public • Published

    Evnty

    0-Deps, simple, fast, for browser and node js anonymous event library

    Coverage Status Github Build Status NPM version Downloads Snyk

    Interface

    declare type Unsubscribe = () => void;
    declare type Listener = (...args: any[]) => void;
    declare type Dispose = () => void;
    declare type Filter = (...args: any[]) => boolean;
    declare type Mapper = <T = any>(...args: any[]) => T;
    declare type Reducer = <T = any>(value: any, ...args: any[]) => T;
    
    declare class Event {
        static merge(...events: Event[]): Event;
        static interval(interval: number): Event;
    
        readonly size: Number;
    
        constructor(dispose?: Dispose);
        has(listener: Listener): boolean;
        off(listener: Listener): void;
        on(listener: Listener): Unsubscribe;
        once(listener: Listener): Unsubscribe;
        clear(): void;
        toPromise(): Promise<any[]>;
        filter(filter: Filter): Event;
        map(mapper: Mapper): Event;
        reduce(reducer: Reducer, init: any): Event;
    }

    Usage

    import event from 'evnty';
    
    function handleClick({ button }) {
      console.log('Clicked button is', button);
    }
    const clickEvent = event();
    const unsubscribe = clickEvent.on(handleClick);
    
    const keyPressEvent = event();
    
    function handleInput({ button, key }) {
    
    }
    
    const inputEvent = Event.merge(clickEvent, keyPressEvent);
    inputEvent.on(handleInput);
    
    function handleLeftClick() {
      console.log('Left button is clicked');
    }
    const leftClickEvent = clickEvent.filter(({ button }) => button === 'left');
    leftClickEvent.on(handleLeftClick);
    
    
    clickEvent({ button: 'right' });
    clickEvent({ button: 'left' });
    clickEvent({ button: 'middle' });
    keyPressEvent({ key: 'A' });
    keyPressEvent({ key: 'B' });
    keyPressEvent({ key: 'C' });
    
    leftClickEvent.off(handleLeftClick);
    unsubscribe();

    License

    License The MIT License Copyright (c) 2021 Ivan Zakharchanka

    Install

    npm i evnty

    DownloadsWeekly Downloads

    66

    Version

    0.6.17

    License

    MIT

    Unpacked Size

    24.2 kB

    Total Files

    11

    Last publish

    Collaborators

    • 3axap4ehko