A simple emitter npm package

Related tags

Node.js HTTP emitter
Overview

Emitter

A simple emitter package

This package is pretty self explanatory...
need to get and send events?
use an emitter

Table of content

How to use?

It all starts with importing it

const Emitter = require("@protagonists/emitter");

then you can use it to get and send events

const myEmitter = new Emitter();
    
myEmitter.on("event", ()=>{
  console.log("The event was called!");
});

myEmitter.emit("event");

Properties

Emitter.events

Description

This property returns an array of all the event callbacks currently inside the emitter

Returned value

Array []

Elements within that array are formatted as such
*Editing the array does not change the actual array inside the emitter

Example

Code:

myEmitter.on("ready", console.log);
myEmitter.on("debug", console.log);
myEmitter.on("test", console.log);
myEmitter.once("debug", console.log);

console.log(myEmitter.events);

Ouput:

[
  {
    name:"ready",
    callback: [Function: log]
  },
  {
    name:"debug",
    callback: [Function: log]
  },
  {
    name:"test",
    callback: [Function: log]
  },
  {
    name:"debug",
    callback: [Function: log],
    once:true
  }
]

Emitter.defaults

Description

This property returns an array of all the *default event callbacks currently inside the emitter

Returned value

Array []

Elements within that array are formatted as such
*Editing the array does not change the actual array inside the emitter

Example

Code:

myEmitter.default("ready", console.log);
myEmitter.default("debug", console.log);
myEmitter.default("test", console.log);
myEmitter.default("debug", console.log);

console.log(myEmitter.defaults);

Ouput:

[
  {
    name:"ready",
    callback: [Function: log]
  },
  {
    name:"debug",
    callback: [Function: log]
  },
  {
    name:"test",
    callback: [Function: log]
  },
  {
    name:"debug",
    callback: [Function: log]
  }
]

Functions

Emitter.on

Description

This function stores the callback with the associated event and will call it whenever the corresponding event gets emitted

Syntax

Emitter.on(name: String, callback: Function)

Example

Code:

myEmitter.on("event", function myFunc() {
  console.log("Event was called!");
});

console.log(myEmitter.events);

myEmitter.emit("event");
myEmitter.emit("event");
myEmitter.emit("event");

Output:

[ { name:"events", callback:[Function: myFunc] } ]
Event was called!
Event was called!
Event was called!

Emitter.once

Description

This function stores the callback with the associated event and will call it whenever the corresponding event gets emitted BUT it only gets called once!

Syntax

Emitter.once(name: String, callback: Function)

Example

Code:

myEmitter.once("event", function myFunc() {
  console.log("Event was called!");
});

console.log(myEmitter.events);

myEmitter.emit("event");
myEmitter.emit("event");
myEmitter.emit("event");

Output:

[ 
  { 
    name:"events", 
    callback:[Function: myFunc], 
    once:true
  }
]
Event was called!

Emitter.emit

Description

This function "emits" an event and calls all callbacks associated with that event
the order of execution goes from top to bottom of the list...
which means that any event callback created after another is also called after

Syntax

Emitter.emit(name: String, ...args: Any)

Example

Code:

function logPerson(name, age) {
  console.log("Hello! My name is", name, "and I am", age, "years old!");
}

myEmitter.on("person", logPerson);

myEmitter.emit("person", "John", 32);

Ouput:

Hello! My name is John and I am 32 years old!

Emitter.default

Description

This one is a bit similar from some other functions, this is essentially the same as Emitter.on but has a few key differences

Syntax

Emitter.default(name: String, callback: Function)

Example

Code:

myEmitter.on("test", ()=>{
  console.log("Running some code.");
});

myEmitter.default("test", ()=>{
  console.log("Running important code here before the other callbacks.");
});

myEmitter.emit("test");

Output:

Running important code here before the other callbacks.
Running some code.

notice how, even when we call Emitter.on earlier, the default callback gets called first?
usually, callbacks get called in the order they were defined
that still is technically true for default callbacks between themselves
but we can see that default callbacks are prioritised above normal event callbacks

Emitter.disableDefaults

Description

This one is pretty self explanatory,
it disables all default callbacks for a specific event

Syntax

Emitter.disableDefaults(name: String)

Example

Code:

myEmitter.default("event", ()=>{
  console.log("Annoying default behaviour!");
});

myEmitter.on("event", ()=>{
  console.log("My behaviour.");
});

myEmitter.disableDefaults("event");

Output

My behaviour.

There we go! If you want to make a package including an emitter like that one,
I highly suggest making default callbacks instead of normal event callbacks
because then, you can give the power to users to simply disable it if they find it annoying *Disabling defaults BEFORE adding the first default callback will not work!

Emitter.enableDefaults

Description

This one is exactly the opposite of Emitter.disableDefaults,
it enables all default callbacks for a specific event

Syntax

Emitter.enableDefaults(name: String)

Example

Code:

myEmitter.default("event", ()=>{
  console.log("Default behaviour.");
});

myEmitter.on("event", ()=>{
  console.log("My behaviour.");
});

myEmitter.once("event", ()=>{
  myEmitter.enableDefaults("event");
  myEmitter.emit("event");
});

myEmitter.disableDefaults("event");

Output

My behaviour.
Default behaviour.
My behaviour.

as you can see, the first time around, only the event callback was called because default callbacks were disabled
we then emitted the event once more after enabling it and now the default callback is back!

Data Objects

event

Description

event objects are simple, they store data for the emitter to use.
you can access this data if nessecary!

properties

name String
contains the event name that the callback is associated to

callback Function
contains the callback function for the event

once? Bool
tells the emitter either or not the event callback is supposed to be run only once or always

Github

https://github.com/ThePywon/emitter

You might also like...

An NPM package to help frontend developers get started with using SASS and SCSS on your project easily. The Package follows the 7-1 architecture project structure.

Project Title - Create SASS APP Ever wanted to code up a frontend project with SASS & SCSS and you are stuck with building the acclaimed 7-1 architect

Sep 22, 2022

Check NPM package licenses

NPM License Checker As of v17.0.0 the failOn and onlyAllow arguments take semicolons as delimeters instead of commas. Some license names contain comma

Dec 29, 2022

:eyeglasses: Node.js module that tells you when your package npm dependencies are out of date.

 :eyeglasses: Node.js module that tells you when your package npm dependencies are out of date.

Node.js module that tells you when your package npm dependencies are out of date. Getting Started Install Node.js. Install david: cd /your/project/dir

Dec 25, 2022

Open the npm page, Yarn page, or GitHub repo of a package

npm-home Open the npm page, Yarn page, or GitHub repo of a package Install $ npm install --global npm-home Usage $ npm-home --help Usage $ npm

Dec 18, 2022

Check whether a package or organization name is available on npm

npm-name Check whether a package or organization name is available on npm Install $ npm install npm-name Usage import npmName from 'npm-name'; // Ch

Nov 13, 2022

Package manager faster than NPM

Pine Script holder that runs faster than NPM and yarn Pine is a npm and yarn run like module, that allows you to organize your scripts and run them FA

Jul 10, 2021

NPM Package that simplifies Auth with Google OAuth2 🔐

NPM Package that simplifies Auth with Google OAuth2 🔐

Node Google OAuth2 🔐 A simple authentication flow for Google OAuth2 Explore the docs » Report Bug Table of Contents About The Project Getting Started

Jun 17, 2022

portfolio-project is a npm package to automatically update your projects section in your portfolio website. It will fetch the selected repositories directly from your GitHub account.

portfolio-project is a npm package to automatically update your projects section in your portfolio website. It will fetch the selected repositories directly from your GitHub account.

portfolio-project Those days of manually updating portfolio website after every new project made are gone ⚡ Yesss . . . you read that right. 😉 portfo

Aug 3, 2021

A NPM package powered by Yeoman that generates a scaffolding boilerplate for back-end workflow with Node.js.

generator-noderplate Generate Node.js starter files with just one command! We have deployed a npm package that will generate a boilerplate for nodejs

Jan 24, 2022

Detect npm packages by author name in your package-lock.json or yarn.lock.

detect-package-by-author Detect npm packages by author name in your package-lock.json or yarn.lock. Install Install with npm: # Not Yet Publish # npm

Jan 11, 2022

wasteof-client is an npm package for wasteof.money

wasteof-client is an npm package for wasteof.money

Jun 16, 2022

mineboty is an npm package to make minecraft bot in a minute

mineboty is an npm package to make minecraft bot in a minute . it will help you to make best in pvp will also guard the place

Jan 1, 2023

A Compiler npm Package.

vcompiler 🎉 Version 1.x is live ! 🎉 Introducation It is the npm package for the compilation of the code. Currently it supports the following program

May 30, 2022

Source of the cosmwasm npm package

Source of the cosmwasm npm package

About Cosmwasm.js was created to help new developers get started with their first dApps. It is just a wrapper package to easily import needed features

Dec 28, 2022

Npm package :p

Npm package :p

💩 poopoo-api Welcome to an "ok" api ⭐ If you like this package, don't forget to star Github package repo here and the website repo here Example code:

Mar 18, 2022

A small npm package that generates pretty identicons from string input.

A small npm package that generates pretty identicons from string input.

HashprintJS HashprintJS is a small npm package written in TypeScript that generates customizable identicons. Provide a username, wallet address, IP ad

Mar 25, 2022

A cool npm package.

aditya.utils Colorful Console const a = require('aditya.utils') a.logblue("TEXT") // Blue Text a.logred("TEXT") // Red Text a.loggreen("TEXT") // Gree

Apr 8, 2022

A cool npm package.

aditya.utils Colorful Console const a = require('aditya.utils') a.logblue("TEXT") // Blue Text a.logred("TEXT") // Red Text a.loggreen("TEXT") // Gree

Apr 8, 2022

StashQL is a light-weight, open-source npm package that improves the speed of your GraphQL queries in your application.

StashQL is a light-weight, open-source npm package that improves the speed of your GraphQL queries in your application.

Table of Contents What is StashQL? Install Getting Started Queries Mutations refillCache clearRelatedFields Logging The Team What is StashQL? StashQL

Sep 30, 2022
Owner
Code man makes code.
null
A simple NodeJS WebSocket WebApp vulnerable to blind SQL injection

NodeJS WebSocket SQLi vulnerable WebApp A one-day build of a vulnerable WebSocket app on NodeJS to practice boolean based SQLi over WebSocket. I made

Rayhan Ahmed 36 Dec 27, 2022
一个基于node.js,express,socket.io的websocket非常棒的聊天室,代码简单很适合新手. A very nice websocket chat room based on node.js, express, socket.io. the code is simple, very suitable for novices

来来往下看,虽然教程又臭又长但是一步步地保姆式教学很简单的,毕竟我是真菜鸟嘛,当然什么都往细了说╮(╯_╰)╭ 一、使用方法 该教程内容所有指令都为Linux CentOS 7.x环境下指令,其他平台请您自行查询(⊙x⊙;) 1.下载node.js并下载Sakura_Chat_Room node.j

樱樱怪 10 Jul 21, 2022
Simple proxy that is intended to support on chaos testing.

Proxy with Behavior Proxy with Behavior is a node application that work as a reverse proxy, and enables apply some behaviors to be executed in request

José Carlos de Moraes Filho 7 Jan 28, 2022
Simple, configurable part mock part proxy

Moxy Simple, configurable mock / proxy server. Table of Contents Quick start Programatic CLI Docker Docker compose Usage Programatic Via HTTP requests

Acrontum GmbH 7 Aug 12, 2022
Package fetcher is a bot messenger which gather npm packages by uploading either a json file (package.json) or a picture representing package.json. To continue...

package-fetcher Ce projet contient un boilerplate pour un bot messenger et l'executable Windows ngrok qui va permettre de créer un tunnel https pour c

AILI Fida Aliotti Christino 2 Mar 29, 2022
npm i uuid, npm i nodemon, npm i commander

goit-nodejs-hw-01 Получаем и выводим весь список контактов в виде таблицы (console.table) node index.js --action list Получаем контакт по id node inde

Oksana Banshchykova 3 Jul 5, 2022
An event emitter that allows you to manage your global state

Thor Event Emitter Event emitter to manage your state. An event emitter that allows you to manage your global state. Instead of using global giant obj

Jalal 6 Apr 18, 2022
A TypeScript friendly event emitter with easy re-emitting events

remitter A TypeScript friendly event emitter with easy re-emitting events. Install npm add remitter Usage import { Remitter } from "remitter"; interf

CRIMX 5 Dec 28, 2022
👀 A Node.js event emitter works in the browser

observer-emit ?? A Node.js event emitter works in the browser. Install using npm $ npm i observer-emit using yarn $ yarn add observer-emit using pnpm

Akashi 3 Jul 2, 2022
Plug-and-play, faster-than-native promise/callback event emitter

kNow Blazing-fast callback/promise-based events with a tiny footprint What is this? With kNow (pronounced "now"—the k's silent), JavaScript event mana

Elijah Bodden 25 Sep 11, 2022