npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

functions-delegate

v1.0.1

Published

This package, inspired by C# multicast delegation system, allow dynamic mapping of several functions/methods inside a single 'delegate' object which can later be invoked or passed as a function.

Downloads

13

Readme

functions-delegate

This package, inspired by C# multicast delegation system, allow dynamic mapping of several functions/methods inside a single 'delegate' object which can later be invoked or passed as a function.

Getting started

npm i functions-delegate
import { ActionDelegate } from "functions-delegate";
import { FunctionDelegate } from "functions-delegate";

ActionDelegate vs FunctionDelegate

Both ActionDelegate and FunctionDelegate share the same interface. However, the main difference between the two classes is the return type of the invocation method.

ActionDelegate ignores the return value of the registered functions. In other words actionDelegate.invoke(..) is void. FunctionDelegate, however, will return an array containing every return value of the called functions.

See bellow for further details.

Usage

ActionDelegate

import { ActionDelegate } from "functions-delegate";

let delegate = new ActionDelegate();

let f1 = (a:string) => {
    console.log(`Hello ${a} from 1`);
}

function f2(a:string) {
    console.log(`Hello ${a} from 2`);
}

let f3 = function (a:string) {
    console.log(`Hello ${a} from 3`);
}

let action = delegate.add(f1).add(f3).add(f2).asFunction();
action('Bob');

console.log('Removing f1');
delegate.remove(f1);
delegate.invoke('Roger') //or action('Roger');

Would prints :

Hello Bob from 1
Hello Bob from 3
Hello Bob from 2
Removing f1
Hello Roger from 3
Hello Roger from 2

FunctionDelegate

import { FunctionDelegate } from "functions-delegate";

let delegate = new FunctionDelegate();

let f1 = (a:string) => {
    return `Hello ${a} from 1`;
}

let f2 = (a:string) => {
    return `Hello ${a} from 2`;
}

let action = delegate.add(f2).add(f1).asFunction();
let results = action('Bob'); //or delegate.invoke('Bob')

console.log(results)

Would prints :

['Hello Bob from 2', 'Hello Bob from 1']

Interface details

add(f:DelegateFunction): Delegate

Adds (register) a given function / method to this delegate

remove(f:DelegateFunction): Delegate

Remove (unregister) a given function / method of this delegate

invoke(...args: any[]): void | any[]

Invoke this delegate with the given arguments

Calls all registered functions with the given arguments

This method is void for ActionDelegate, and returns an array of result (any[]) for FunctionDelegate

asFunction(): DelegateFunction

Returns a function that can be called later to invoke the delegate

Example :

let action = new ActionDelegate().add(f1).add(f3).add(f2).asFunction();
action(`Bob`);

// Has the same result than
new ActionDelegate().add(f1).add(f3).add(f2).invoke('Bob');