clbk
v1.1.5
Published
A tiny function wrapper to add callbacks.
Downloads
8
Maintainers
Readme
CLBK
A tiny function wrapper to trigger callbacks.
Installing
Install via NPM:
npm i clbk
Usage
First, import the corresponding functions.
import { bind, on } from "clbk";
Bind a function to trigger callbacks
A function passed to bind
will trigger all callbacks listening for that name.
const isNotEqual = bind("isNotEqual", (arg1, arg2) => {
if (arg1 !== arg2) console.log("Values are not equal!");
});
Registering a callback
Use the on
function to register a callback.
on("isNotEqual", function () {
console.log(`isNotEqual has been called with arguments: ${Array.from(arguments)}`);
});
Since arguments
is not defined in arrow functions, you have to specify them when using ES6 syntax:
on("isNotEqual", (arg1, arg2) => {
console.log(`isNotEqual has been called with arguments: ${arg1} ${arg2}`);
});
Call the function
Call the foo
function:
isNotEqual(1, 2);
Excpected output:
isNotEqual has been called with arguments: 1,2
Values are not equal!
Instance Methods
on(name, callback)
Register a callback
name
- the name of the event to listen forcallback
- the callback function to execute when an event is recieved
bind(name, function, [passArgs])
Bind a function to trigger an event
name
- the name of the event to triggerfunction
- the function that will trigger the eventpassArgs
- (optional) - wether to pass the function arguments to the event (defaulttrue
)
TypeScript support
To ensure type safety when using TypeScript, the bind function accepts types:
const isDifferent = bind<boolean, [number, number, boolean]>("test", (a: number, b: number, reverse: boolean) => {
return reverse ? a !== b : a === b;
});
Which will produce the following type signature:
const isDifferent: (args_0: number, args_1: number, args_2: boolean) => boolean