[READ-ONLY] Mirror of https://github.com/okikio/native. An initiative which aims to make it easy to create complex, light-weight, and perfor okikio.github.io/native
native src content emitter index.md
3.2 kB · 95 lines
Markdown
at dev

@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 { EventEmitter } from "@okikio/emitter";
import { EventEmitter } from "https://unpkg.com/@okikio/emitter";
import { EventEmitter } from "https://cdn.jsdelivr.net/npm/@okikio/emitter";
// Or
import { EventEmitter } from "https://cdn.skypack.dev/@okikio/emitter";

// Via script tag
<script src="https://unpkg.com/@okikio/emitter/lib/index.js"></script>
// Do note, on the web you need to do this, if you installed it via the script tag:
const { 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", () => {
    console.log("A new event occured");
});

// Then emit or fire the event
setTimeout(() => {
    // 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 #

Chrome Edge Firefox IE
> 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).