import { decorator_compatibility } from "./utils"; export const bound = decorator_compatibility({ // Originally from https://github.com/andreypopp/autobind-decorator/blob/master/src/index.js legacy(target, key, descriptor?) { // let fn = descriptor.value; // if (typeof fn !== 'function') { // throw new TypeError(`@bound decorator can only be applied to methods not: ${typeof fn}`); // } // In IE11 calling Object.defineProperty has a side-effect of evaluating the // getter for the property which is being replaced. This causes infinite // recursion and an "Out of stack space" error. let definingProperty = false; return { configurable: true, get() { let fn = descriptor.value || descriptor.get(); // eslint-disable-next-line no-prototype-builtins if (definingProperty || this === target.prototype || this.hasOwnProperty(key) || typeof fn !== 'function') { return fn; } const boundFn = fn.bind(this); definingProperty = true; Object.defineProperty(this, key, { configurable: true, get() { return boundFn; }, set(value) { } }); definingProperty = false; return boundFn; }, set(value) { } }; }, stage3(value, context) { context.addInitializer(function () { this[context.name] = this[context.name].bind(this); }) }, })