summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Aguilar Santillana <mhpoin@gmail.com>2024-09-15 10:43:24 +0200
committerGitHub <noreply@github.com>2024-09-15 17:43:24 +0900
commit07f26bc8dd199ff366e6278a8ac1521497922b95 (patch)
tree07f832974a553082dc4fdf017c1c00b5e4d44fe7
parentUpdate CHANGELOG.md (diff)
downloadmisskey-07f26bc8dd199ff366e6278a8ac1521497922b95.tar.gz
misskey-07f26bc8dd199ff366e6278a8ac1521497922b95.tar.bz2
misskey-07f26bc8dd199ff366e6278a8ac1521497922b95.zip
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.
-rw-r--r--packages/backend/src/decorators.ts21
1 files changed, 7 insertions, 14 deletions
diff --git a/packages/backend/src/decorators.ts b/packages/backend/src/decorators.ts
index 21777657d1..42f925e125 100644
--- a/packages/backend/src/decorators.ts
+++ b/packages/backend/src/decorators.ts
@@ -10,8 +10,9 @@
* The getter will return a .bind version of the function
* 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) {
- let fn = descriptor.value;
+ const fn = descriptor.value;
if (typeof fn !== 'function') {
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,
get() {
// eslint-disable-next-line no-prototype-builtins
- if (this === target.prototype || this.hasOwnProperty(key) ||
- typeof fn !== 'function') {
+ if (this === target.prototype || this.hasOwnProperty(key)) {
return fn;
}
const boundFn = fn.bind(this);
- Object.defineProperty(this, key, {
+ Reflect.defineProperty(this, key, {
+ value: boundFn,
configurable: true,
- get() {
- return boundFn;
- },
- set(value) {
- fn = value;
- delete this[key];
- },
+ writable: true,
});
+
return boundFn;
},
- set(value: any) {
- fn = value;
- },
};
}