JSPM

evnty

0.8.52
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 16088
  • Score
    100M100P100Q133162F
  • License MIT

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

Package Exports

  • evnty
  • evnty/build/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (evnty) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Evnty

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

Coverage Status Github Build Status NPM version Downloads Snyk

Table of Contents

Features

  • Promises support
  • Full-featured typeScript support
  • Browser & Workers environment compatibility
  • Performance eventemitter3/eventemitter2/event-emitter/events/native node/native browser

Roadmap

  • Namespaces/Wildcards
  • Times To Listen (TTL)
  • Subscribe/UnSubscribe

Browser Support

Chrome Firefox Safari Opera Edge
Latest ✔ Latest ✔ Latest ✔ Latest ✔ Latest ✔

Installing

Using yarn:

yarn add evnty

Using npm:

npm install evnty

Interface

type Unsubscribe = () => void;
type Listener = (...args: any[]) => void;
type Dispose = () => void;
type Filter = (...args: any[]) => boolean;
type Mapper = <T = any>(...args: any[]) => T;
type Reducer = <T = any>(value: any, ...args: any[]) => T;

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;
  dispose(): Dispose;
}

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 Apache-2.0 Copyright (c) 2021-present Ivan Zakharchanka