Home / emitter

@okikio/emitter

NPM | Github | API Guide | Licence

A small Event Emitter written in typescript with performance and ease of use in mind, it weighs ~834 B (minified and gzipped).

Installation

You can install @okikio/emitter from npm via

npm i @okikio/emitter
Others
yarn add @okikio/emitter

or

pnpm i @okikio/emitter

You can use @okikio/emitter on the web via:

Once installed it can be used like this:

// There is,
//      .cjs - Common JS Module
//      .mjs - Modern ES Module
//      .js - IIFE
import ASTRO_ESCAPED_LEFT_CURLY_BRACKET EventEmitter } from "@okikio/emitter";
import ASTRO_ESCAPED_LEFT_CURLY_BRACKET EventEmitter } from "https://unpkg.com/@okikio/emitter";
import ASTRO_ESCAPED_LEFT_CURLY_BRACKET EventEmitter } from "https://cdn.jsdelivr.net/npm/@okikio/emitter";
// Or
import ASTRO_ESCAPED_LEFT_CURLY_BRACKET EventEmitter } from "https://cdn.skypack.dev/@okikio/emitter";

// Via script tag
<script src="https://unpkg.com/@okikio/emitter/lib/api.js"></script>
// Do note, on the web you need to do this, if you installed it via the script tag:
const ASTRO_ESCAPED_LEFT_CURLY_BRACKET EventEmitter } = window.emitter;

Getting started

The EventEmitter class is what runs the show for the @okikio/emitter library. To use it properly you need to create a new instance of EventEmitter, the instance of EventEmitter is what allows for event emitting, and listening.

EventEmitter allows for an easy way to manage events. It inherits properties/methods from @okikio/manager.

// You need to first initialize a new Event Emitter
const emitter = new EventEmitter();

// Then listen for an event
emitter.on("new-event", () => ASTRO_ESCAPED_LEFT_CURLY_BRACKET
    console.log("A new event occured");
});

// Then emit or fire the event
setTimeout(() => ASTRO_ESCAPED_LEFT_CURLY_BRACKET
    // Eg. after 3 seconds fire the `new-event` listener
    emitter.emit("new-event"); //= A new event occured
}, 3000);

Read through the API guide to learn more.

Browser & Node Support

ChromeEdgeFirefoxIE
> 38> 12> 13> 11

Learn about polyfilling, bundling, and more in the platform support guide.

Contributing

If there is something I missed, a mistake, or a feature you would like added please create an issue or a pull request on the beta branch and I’ll try to get to it.

Read through the contributing documentation for detailed guides.

Licence

See the LICENSE file for license rights and limitations (MIT).