2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-14 00:59:27 +09:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
// https://github.com/andreypopp/autobind-decorator
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a descriptor removing the value and returning a getter
|
|
|
|
* The getter will return a .bind version of the function
|
|
|
|
* and memoize the result against a symbol on the instance
|
|
|
|
*/
|
2023-02-09 10:50:53 +09:00
|
|
|
export function bindThis(target: any, key: string, descriptor: any) {
|
2022-12-04 15:03:09 +09:00
|
|
|
let fn = descriptor.value;
|
|
|
|
|
|
|
|
if (typeof fn !== 'function') {
|
|
|
|
throw new TypeError(`@bindThis decorator can only be applied to methods not: ${typeof fn}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
configurable: true,
|
|
|
|
get() {
|
|
|
|
// eslint-disable-next-line no-prototype-builtins
|
|
|
|
if (this === target.prototype || this.hasOwnProperty(key) ||
|
|
|
|
typeof fn !== 'function') {
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
const boundFn = fn.bind(this);
|
|
|
|
Object.defineProperty(this, key, {
|
|
|
|
configurable: true,
|
|
|
|
get() {
|
|
|
|
return boundFn;
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
fn = value;
|
|
|
|
delete this[key];
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return boundFn;
|
|
|
|
},
|
2023-02-09 10:50:53 +09:00
|
|
|
set(value: any) {
|
2022-12-04 15:03:09 +09:00
|
|
|
fn = value;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|