instance-proxy
v1.0.0
Published
Just an experiment created while playing with the Proxy API.
Readme
InstanceProxy
Just an experiment created while playing with the Proxy API.
InstanceProxy
The InstanceProxy wraps all class instances in a Proxy using the construct trap.
const Foo = new InstanceProxy(
class Foo {
data = new Map();
},
{
has(object, key) {
return object.data.has(key);
}
}
);
const foo = new Foo(); // foo is wrapped in a Proxy
foo.data.set("bar", 42);
console.log("bar" in foo);Proxied & ProxyHandler
Proxied & ProxyHandler are decorators which allow you to write instance proxies in a more comfortable way.
The @Proxied decorator looks for properties decorated with @ProxyHandler and creates a proxy handler.
The @ProxyHandler(trap) decorator marks the property as a proxy handler for the specified trap.
// This is the same as the example above
@Proxied
class Foo {
data = new Map();
@ProxyHandler("has")
has(key) {
return this.data.has(key);
}
}
const foo = new Foo(); // foo is wrapped in a Proxy
foo.data.set("bar", 42);
console.log(foo.has("bar")); // These two lines are equivalent
console.log("bar" in foo);Note: you should install & import the
reflect-metadatapackage to use these decorators.
