Bindings
Don't call it monkey patching 🙈
Bindings attach listener function calls to each other. Append or prepend a listener function to any invocation of another listener function.
Bindings automatically handle asynchronous functions. Functions execute in the order you specify, regardless of synchronicity.
Identifiers define bindings
The listener.bind
function receives a bind to function identifier, followed by one or more bind from function identifiers.
For example, here we bind to myLib.a
when it is called with an empty listener id:
The bind to argument (myLib.a
) must match exactly, which is why the binding only executes when myLib.a
is called with an empty listener id.
The first argument to listener.bind
is an identifier and does not affect the binding.
Bind to call with custom listener id
Here we bind to myLib.a
when it is called with a custom listener id:
Bind to call with any listener id
Use asterisks to wildcard match on bind to identifiers.
Here we bind to myLib.a
when it is called with any listener id:
Double asterisks (**
) does a greedy match of any id and a single asterisk (*
) matches any single id.
Asterisks may appear at either the beginning or end of the identifier, but not both ends or between named ids.
Binding from a library
Listener libraries use the listenerLoaded
callback function to call listener.bind
.
① Call bind from a listener loaded callback and save it as bindExample.ts
:
In plain english — "call bindExample.b
after calling bindExample.a
with any identifier."
② Load the library, call the listener function, and save it as main.ts
:
③ Execute main.ts
and view the output:
Bind options
Use options to change the execution of the bind from function in the following ways:
Execution order
Peek at original return value
Overwrite original return value
How to use bind options
Add options to an individual bind from function:
Add options to multiple bind from functions:
Execution order
Use the prepend
option to bind myLib.b
before myLib.a
:
The prepend
/ append
options also may receive an index:
Indices with the same value execute concurrently if the bind from functions have asynchronous return values.
Trueprepend
/append
options default to an index of 1
.
A higher index means an earlier execution order for prepend
, and later execution order for append
.
Peek at original return value
The peek
option passes the return value of the original bind to function as the first argument to the bind from function:
Overwrite original return value
The return
option allows a bind from function to overwrite the output of the bind to function:
Now calls to myLib.a([])
will return false after myLib
is processed by listener.load
.
Intercept output
The intercept
option enables both peek
and return
options.
Last updated