mirror of
https://github.com/misskey-dev/misskey
synced 2024-11-24 15:16:30 +09:00
refactor(backend): use Reflet for autobind deco (#14482)
Using Reflect.defineProperty instead of Object.defineProperty gives a more consistent behavior with the rest of the modern JavaScript features.
This commit is contained in:
parent
366b79e459
commit
07f26bc8dd
@ -10,8 +10,9 @@
|
|||||||
* The getter will return a .bind version of the function
|
* The getter will return a .bind version of the function
|
||||||
* and memoize the result against a symbol on the instance
|
* and memoize the result against a symbol on the instance
|
||||||
*/
|
*/
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
export function bindThis(target: any, key: string, descriptor: any) {
|
export function bindThis(target: any, key: string, descriptor: any) {
|
||||||
let fn = descriptor.value;
|
const fn = descriptor.value;
|
||||||
|
|
||||||
if (typeof fn !== 'function') {
|
if (typeof fn !== 'function') {
|
||||||
throw new TypeError(`@bindThis decorator can only be applied to methods not: ${typeof fn}`);
|
throw new TypeError(`@bindThis decorator can only be applied to methods not: ${typeof fn}`);
|
||||||
@ -21,26 +22,18 @@ export function bindThis(target: any, key: string, descriptor: any) {
|
|||||||
configurable: true,
|
configurable: true,
|
||||||
get() {
|
get() {
|
||||||
// eslint-disable-next-line no-prototype-builtins
|
// eslint-disable-next-line no-prototype-builtins
|
||||||
if (this === target.prototype || this.hasOwnProperty(key) ||
|
if (this === target.prototype || this.hasOwnProperty(key)) {
|
||||||
typeof fn !== 'function') {
|
|
||||||
return fn;
|
return fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
const boundFn = fn.bind(this);
|
const boundFn = fn.bind(this);
|
||||||
Object.defineProperty(this, key, {
|
Reflect.defineProperty(this, key, {
|
||||||
|
value: boundFn,
|
||||||
configurable: true,
|
configurable: true,
|
||||||
get() {
|
writable: true,
|
||||||
return boundFn;
|
|
||||||
},
|
|
||||||
set(value) {
|
|
||||||
fn = value;
|
|
||||||
delete this[key];
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return boundFn;
|
return boundFn;
|
||||||
},
|
},
|
||||||
set(value: any) {
|
|
||||||
fn = value;
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user