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
;;
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
; ; ;action'Bob'; console.log'Removing f1';delegate.removef1;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
; ; ;; //or delegate.invoke('Bob') console.logresults
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 :
;action`Bob`; // Has the same result thannew ActionDelegate.addf1.addf3.addf2.invoke'Bob';