daisy-chain
v1.2.0
Published
Create a pipeline of proxy objects that will wrap over a target object.
Downloads
157
Readme
daisychain
Create a pipeline of proxy objects that will wrap over a target object.
Install
npm install --save daisy-chain
const { daisyChain } = require('daisy-chain');
API
daisyChain(proxies, target, prop?)
proxies
(Object[]): an array of proxy objects.target
(Object): a target object.prop
(String): an optional property on the target the proxies will wrap over. (If not provided, the proxies will wrap overtarget
).
A proxy can provide a wrapper over any property/ function on the target object (or property on the target object).
The order of the proxies in the proxy array determines their execution order.
Each proxy object can wrap a target property/ function with the signature:
foobar(next, ...args)
next
is the nextfoobar
proxy call in the pipeline (or the originalfoobar
call) on the target object/ specified target object property.args
are the provided arguments tofoobar
.
Example (javascript)
const { daisyChain } = require('daisy-chain');
class Greeting {
constructor() {
this.message = 'hello';
}
greet() {
return this.message;
}
}
class GreetingWorld {
constructor() {
this.message = ' world';
}
greet(next, ...args) {
return next(...args) + this.message;
}
}
class GreetingLogger {
greet(next, ...args) {
const result = next(...args);
console.log(result);
return result;
}
}
const target = new Greeting();
const proxyPipeline = [new GreetingLogger(), new GreetingWorld()];
const wrappedTarget = daisyChain(proxyPipeline, target);
wrappedTarget.greet(); // hello world
see examples