mbrowne / bound-decorator Goto Github PK
View Code? Open in Web Editor NEWDecorator for creating bound methods using Babel 7+
Decorator for creating bound methods using Babel 7+
Using an initializer to bind the method essentially converts the method into a field, which has all of the same problems as laid out in the rationale doc:
class Counter extends React.Component {
...
@bound
handleClick() {
this.setState({ count: this.state.count + 1 })
}
...
}
class SpecialCounter extends Counter {
handleClick() {
console.log("SpecialCounter clicked")
}
...
}
const specialCounter = new SpecialCounter()
// Which method gets called?
specialCounter.handleClick()
// (Note: this example is for illustrative purposes only; obviously we wouldn't really call handleClick() manually)
An additional constraint worth considering is that binding on both the superclass and the subclass should still Just Work ™️, even with calls to super
:
class Counter extends React.Component {
...
@bound
handleClick() {
this.setState({ count: this.state.count + 1 })
}
...
}
class SpecialCounter extends Counter {
@bound
handleClick() {
super.handleClick();
// do more things
}
...
}
Currently, super.handleClick()
will not exist on the superclass since it is a class field, and it will be overwritten by the time it gets to the child. This is also problematic for the strategy of dynamically redefining the method within a getter, since the call to super
will then overwrite the subclass's version.
The best strategy we have found to mitigate this is to use a WeakMap
to map bound functions to the instance, and to bind the function in a getter:
const BINDINGS_MAP = new WeakMap();
export function bound(desc) {
let boundFn = desc.descriptor.value;
desc.descriptor = {
get() {
let bindings = BINDINGS_MAP.get(this);
if (bindings === undefined) {
bindings = new Map();
BINDINGS_MAP.set(this, bindings);
}
let fn = bindings.get(boundFn);
if (fn === undefined) {
fn = boundFn.bind(this);
bindings.set(boundFn, fn);
}
return fn;
}
}
return desc;
}
There's currently no way to test how this decorator works with private methods because Babel hasn't implemented private methods/fields for decorators yet, but I am starting to think about how they should be handled... I realized that the current implementation doesn't make sense for private methods because it always returns a new bound method using extras
, but in the case of private methods it should just replace the existing method since it didn't exist on the prototype in the first place.
Also, just creating a new private (bound) method doesn't fully serve the goals of making objects testable. Most build tools these days as well as node.js make process.env.NODE_ENV
available, so one idea would be to do something like this to check if the app is running in a 'test' environment:
And then for the test environment, the behavior would be to:
There could also be a parameter to the decorator allowing the user to customize this behavior, e.g.:
privateMethodHandling: 'basedOnEnvironment' | 'alwaysMakePublic'
(default: 'basedOnEnvironment')
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.