3.1.0 • Published 4 years ago

hypergiant v3.1.0

Weekly downloads
31
License
MIT
Repository
github
Last release
4 years ago

NPM version Known Vulnerabilities npm NPM downloads 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.

paramtypedescriptiondefault
fnFunctionThe function to be called when the signal is dispatched.
oncefalseIndicates 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.

paramtypedescriptiondefault
...dataanyAny 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

paramtypedescriptiondefault
fnFunctionThe 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

paramtypedescriptiondefault
fnFunctionThe 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

3.1.0

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.5.3

4 years ago

2.5.2

4 years ago

2.5.0

5 years ago

2.4.2

5 years ago

2.4.0

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

6 years ago

1.0.0

6 years ago

0.1.0

6 years ago