Miss any of our Open RFC calls?Watch the recordings here! »

hypergiant

3.0.6 • Public • Published

Hypergiant

Hypergiant is a small and simple signal-like event system for Node.js and the browser.

NPM version Known Vulnerabilities npm NPM downloads issues license Gitter

Table of Contents

Installation

To download Hypergiant through NPM, simply use:

$ npm install --save hypergiant

and use it in your Node environment as so:

const Hypergiant = require('hypergiant');

or import it browser side as an ES6 module:

import Hypergiant from './node_modules/hypergiant/hypergiant.js';

Basic Usage

Hypergiant is a signal-like event emitter for Node.JS and the browser.

Hypergiant is very minimal and fast but also very powerful. It is comparable to events in native JavaScript except that Hypergiant events are emitted after the action has occurred and it doesn't rely on the events being referenced by string which can lead to misspellings.

Creating a new signal is as simple as:

const appStarted = new Hypergiant();

Any variable or property can be made into a signal.

Now a signal isn't very useful if there isn't a response to the event when it happens. To add a task that will run whenever the event is dispatched, use the add method on the created signal:

appStarted.add(hello);
 
function hello(name) {
  console.log(`Hello ${name}!`);
}

You can add as many methods as you would like to respond to a signal.

Lastly, it's time to dispatch the signal with the dispatch method:

appStarted.dispatch('Bob');
 
// The console will display the following message:
// => Hello Bob!

Any parameters passed with dispatch will also be passed to the tesk functions attached to it.

Properties

tasks

Returns all of the tasks that have been created for this signal:

exmple:

const sol = new Hypergiant();
 
// Tasks...
sol.add(blah);
// More tasks...
 
const tasks = sol.tasks;

numTasks

Returns the number of tasks currently assigned to this signal.

example:

const sol = new Hypergiant();
 
sol.add(blah);
sol.add(blah);
 
const numTasks = sol.numTasks; // 2

API

add

Add takes in a function and an optional parameter named once that can be set to true if you would like this task to be called only once and then be deleted.

param type description default
fn Function The function to be called when the signal is dispatched.
once false Indicates whether this task should happen only once and then be automatically deleted. false

example:

const sol = new Hypergiant();
 
// When sol is dispatched, the `sayHello` function will be called once and then deleted from the signal's task list.
sol.add(sayHello, true);
 
function sayHello(name1, name2) {
  console.log(`Hello ${name1} and ${name2}!`);
}

dispatch

Dispatch sends out the signal and any attached tasks will be called.

This method can take any number of parameters which will act as data sent to the tasks.

param type description default
...data any Any data that you want to pass to the tasks

example:

const sol = new Hypergiant();
 
// When sol is dispatched, the `sayHello` function will be called once and then deleted from the signal's task list.
sol.add(sayHello, true);
 
function sayHello(name1, name2) {
  console.log(`Hello ${name1} and ${name2}!`);
}
 
// At some other point in your application...
// This will dispatch the Hypergiant event and any attached tasks will be called with 'Bob' and 'John' as parameter values.
sol.disaptch('Bob', 'John');
 
// In this case the `sayHello` function will log:
// => Hello Bob and John!

remove

Deletes a task from the signal

param type description default
fn Function The function to delete

example:

const sol = new Hypergiant();
 
sol.add(hello);
sol.remove(hello);
 
function hello() {
  return 'Hello World!';
}

removeAll

Deletes all tasks from the signal.

example:

const sol = new Hypergiant();
 
sol.add(hello);
sol.removeAll();
 
function hello() {
  return 'Hello World!';
}

noop

Makes a task a noop function

param type description default
fn Function The function to make a noop function

example:

const sol = new Hypergiant();
 
sol.add(hello);
sol.noop(hello);
 
function hello() {
  return 'Hello World!';
}

Tests

To run the tests available use:

$ npm run test

License

MIT

Install

npm i hypergiant

DownloadsWeekly Downloads

18

Version

3.0.6

License

MIT

Unpacked Size

82.3 kB

Total Files

21

Last publish

Collaborators

  • avatar