@okikio/emitter
A small Event Emitter written in typescript with performance and ease of use in mind, it weighs ~838 B (minified and gzipped).
You will need a Map and Promise polyfill for older browsers. If you install @okikio/emitter
via npm you are most likely going to need rollup or esbuild. You can use polyfill.io, or another source to create a polyfill. The minimum feature requirement for a polyfill are Maps and Promises e.g. https://polyfill.io/v3/polyfill.min.js?features=Promise,Map.
You can try out @okikio/emitter
using Gitpod:
By default Gitpod will start the dev script for you, but if you need to restart the dev script you can do so by typing into the terminal.
pnpm test-dev --filter "@okikio/emitter"
Once Gitpod has booted up, go to ./tests/test.ts and start tweaking and testing to your hearts content.
Table of Contents
Installation
You can install @okikio/emitter from npm via npm i @okikio/emitter
, pnpm i @okikio/emitter
or yarn add @okikio/emitter
.
You can use @okikio/emitter
on the web via:
Once installed it can be used like this:
import { EventEmitter } from "@okikio/emitter";
import { EventEmitter } from "https://unpkg.com/@okikio/emitter/lib/api.es.js";
import { EventEmitter } from "https://cdn.jsdelivr.net/npm/@okikio/emitter/lib/api.es.js";
import { EventEmitter } from "https://cdn.skypack.dev/@okikio/emitter";
<script src="https://unpkg.com/@okikio/emitter/lib/api.js"></script>
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.
const emitter = new EventEmitter();
emitter.on("new-event", () => {
console.log("A new event occured");
});
setTimeout(() => {
emitter.emit("new-event");
}, 3000);
API Documentation
You can also go through the API documentation, for a more detailed documentation of the API.
API
EventEmitter#on(events, callback, scope) & EventEmitter#emit(events, ...args)
EventEmitter.prototype.on(events: EventInput, callback?: ListenerCallback | object, scope?: object);
EventEmitter.prototype.emit(events: string | Array<any>, ...args: any);
import EventEmitter from "@okikio/emitter";
const emitter = new EventEmitter();
let test = false;
let obj = { bool: false };
let counter = 0;
let on = emitter.on(
"test",
function (key, value) {
test = [key, value];
console.log(this.bool);
},
obj
)
.on(
"test test1 test2",
function (key, value) {
test = [value, key];
counter++;
console.log(this.bool);
}
);
on.emit("test", 1, "true");
console.log(test);
on.emit(
"test test1 test2",
1, "true");
console.log(test);
console.log(counter);
counter = 0;
let fn = function (i = 1) {
return function (key, value) {
test = [key, value];
counter += i;
console.log(this.bool);
};
};
emitter.on(
{
play1: fn(),
play2: fn(2),
play3: fn(4)
},
obj
);
on.emit("play1 play2 play3 play1", "true", 1)
.emit(["play1", "play2", "play3"], 1, "true");
console.log(test);
console.log(counter);
EventEmitter#off(events, callback, scope)
EventEmitter.prototype.off(events: EventInput, callback?: ListenerCallback | object, scope?: object);
import EventEmitter from "@okikio/emitter";
const emitter = new EventEmitter();
let counter = 0, fn, scope = { bool: false };
let on = emitter.on(
"test test1 test2",
fn = function () {
counter++;
},
scope
);
emitter.off("test1 test2");
emitter.off("test", fn, scope);
on.emit("test test1 test2");
console.log(counter);
EventEmitter#once(events, callback, scope)
EventEmitter.prototype.once(events: EventInput, callback?: ListenerCallback | object, scope?: object);
import EventEmitter from "@okikio/emitter";
const emitter = new EventEmitter();
let test: boolean | any = false;
let obj = { bool: false };
let counter = 0;
let on: EventEmitter = emitter.once(
"test test1 test2",
function (key, value) {
test = [key, value];
counter++;
console.log(this.bool);
},
obj
);
on.emit("test test1 test2", 1, "true");
console.log(test);
console.log(counter);
on.emit("test test1 test2", 1, "true");
console.log(test);
console.log(counter);
console.log(on.get("test").size)
Contributing
If there is something I missed, a mistake, or a feature you would like added please create an issue or a pull request and I'll try to get to it.
Note: all contributions must be done on the beta
branch, using the Conventional Commits style.
The native
initiative uses Conventional Commits as the style of commit, we also use the Commitizen CLI to make commits easier.
Licence
See the LICENSE file for license rights and limitations (MIT).