mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 16:57:26 +00:00
Our simplified @bind decorator didn't work :( The getter is needed in order to defer binding until there is an instance to bind to, the simpler implementation actually bound methods to the prototype itself.
This commit is contained in:
@@ -34,6 +34,16 @@ export class When extends Component<WhenProps, WhenState> {
|
||||
* f() instanceof C; // true
|
||||
*/
|
||||
export function bind(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
||||
descriptor.value = descriptor.value.bind(target);
|
||||
return descriptor;
|
||||
return {
|
||||
// the first time the prototype property is accessed for an instance,
|
||||
// define an instance property pointing to the bound function.
|
||||
// This effectively "caches" the bound prototype method as an instance property.
|
||||
get() {
|
||||
let boundFunction = descriptor.value.bind(this);
|
||||
Object.defineProperty(this, propertyKey, {
|
||||
value: boundFunction
|
||||
});
|
||||
return boundFunction;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user