Giter Site home page Giter Site logo

emitter's Introduction

Emitter

A minimal library for implementing Event Emitter functionality.

Features

  • Written in Typescript
  • Has no dependencies (e.g. jquery)
  • Lightweight, 1KB
  • Supports ES6

Install

git

git clone https://github.com/arayutw/emitter.git
cd emitter
npm install
npm run build

npm

npm install @arayutw/emitter

CDN

Please find various build files (esm, cjs, umd).

Package

If you plan to use it as a package, it is recommended to use @arayutw/emitter-import. It is a minimal project with only TypeScript files that has the same contents as this repository.

npm install @arayutw/emitter-import
import Emitter from "@arayutw/emitter-import";

Usage

load

ESM

import Emitter from "<pathto>/emitter.esm"

UMD

<script src="https://unpkg.com/@arayutw/emitter@latest/dist/scripts/emitter.js"></script>
<script>
  class A extends Emitter {}
</script>

extends

class A extends Emitter {
  constructor() {
    super();
  }
}

const a = new A;

on()

const handler = (event) => {
  // {target: a, type: "click", x: 12, y: 34}
  console.log(event);
}

a.on("click", handler, {
  once: false,
});

off()

a.off("click", handler);

emit()

a.emit("click", {
  x: 12,
  y: 34,
});

destroy

off("*") removes all events.

a.off("*");

Typescript

Development with Typescript is also convenient. Please define Generics (two arguments) when extending it.

Argument Example Description
1 A The value of event.target.
2 {eventName: {data1: number, data2: string}} An object where the keys are event names and the values are objects containing event data. The key names "type" and "target" are reserved.

ใ€€

import Emitter from "{pathto}/emitter/src/scripts/index"

// The key names "type" and "target" are reserved.
type Events = {
  eventName: {
    eventData1: any
    eventData2: any
  }
  click: {
    x: number
    y: number
  }
  mousedown: {
    // some data
  }
}

class A extends Emitter<A, Events> {
  constructor() {
    super();
  }
}

const a = new A;

a.on("click", (event) => console.log(event));

// OK
a.emit("click", {
  x: 1,
  y: 2,
});

// NG
a.emit("click2", {
  x: 1,
  y: 2,
});

// NG
a.emit("click", {
  x: "a",
  y: 2,
});

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.