Code Monkey home page Code Monkey logo

evnty's Introduction

Prioritized Evnty

Based on https://github.com/3axap4eHko/evnty

Only fires event to the most recently added listener. Example:

import event from "./src/index";

const evt = event();

const handler1 = (info: string) => console.log(`Handler 1 returned: ` + info);
const unsubscribe1 = evt.on(handler1);

const handler2 = (info: string) => console.log(`Handler 2 returned: ` + info);
const unsubscribe2 = evt.on(handler2);

const handler3 = (info: string) => console.log(`Handler 3 returned: ` + info);
const unsubscribe3 = evt.on(handler3);

evt("something");

yields:

$ ts-node test.ts
Handler 3 returned: something

Interface

declare type Unsubscribe = () => void;
declare type Listener = (...args: any[]) => void;
declare type Dispose = () => void;

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;
}

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) 2019 Ivan Zakharchanka

evnty's People

Contributors

3axap4ehko avatar drmikecrowe avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.