A tiny function wrapper to trigger callbacks.
Install via NPM:
npm i clbkFirst, import the corresponding functions.
import { bind, on } from "clbk";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!");
});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 foo function:
isNotEqual(1, 2);Excpected output:
isNotEqual has been called with arguments: 1,2
Values are not equal!
Register a callback
name- the name of the event to listen forcallback- the callback function to execute when an event is recieved
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)
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