summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-12-04 15:03:09 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2022-12-04 15:03:09 +0900
commitbbb49457f9fb5d46402e913c92ebf77722cad6ff (patch)
tree8ef285bcbab2c3a4a89d0a624a802d76a2864fed /packages/backend/src/misc
parent:art: (diff)
downloadmisskey-bbb49457f9fb5d46402e913c92ebf77722cad6ff.tar.gz
misskey-bbb49457f9fb5d46402e913c92ebf77722cad6ff.tar.bz2
misskey-bbb49457f9fb5d46402e913c92ebf77722cad6ff.zip
refactor: introduce bindThis decorator to bind this automaticaly
Diffstat (limited to 'packages/backend/src/misc')
-rw-r--r--packages/backend/src/misc/cache.ts7
-rw-r--r--packages/backend/src/misc/i18n.ts3
2 files changed, 9 insertions, 1 deletions
diff --git a/packages/backend/src/misc/cache.ts b/packages/backend/src/misc/cache.ts
index e5b911ed32..69512498f8 100644
--- a/packages/backend/src/misc/cache.ts
+++ b/packages/backend/src/misc/cache.ts
@@ -1,3 +1,5 @@
+import { bindThis } from '@/decorators.js';
+
export class Cache<T> {
public cache: Map<string | null, { date: number; value: T; }>;
private lifetime: number;
@@ -7,6 +9,7 @@ export class Cache<T> {
this.lifetime = lifetime;
}
+ @bindThis
public set(key: string | null, value: T): void {
this.cache.set(key, {
date: Date.now(),
@@ -14,6 +17,7 @@ export class Cache<T> {
});
}
+ @bindThis
public get(key: string | null): T | undefined {
const cached = this.cache.get(key);
if (cached == null) return undefined;
@@ -24,6 +28,7 @@ export class Cache<T> {
return cached.value;
}
+ @bindThis
public delete(key: string | null) {
this.cache.delete(key);
}
@@ -32,6 +37,7 @@ export class Cache<T> {
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
*/
+ @bindThis
public async fetch(key: string | null, fetcher: () => Promise<T>, validator?: (cachedValue: T) => boolean): Promise<T> {
const cachedValue = this.get(key);
if (cachedValue !== undefined) {
@@ -56,6 +62,7 @@ export class Cache<T> {
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
*/
+ @bindThis
public async fetchMaybe(key: string | null, fetcher: () => Promise<T | undefined>, validator?: (cachedValue: T) => boolean): Promise<T | undefined> {
const cachedValue = this.get(key);
if (cachedValue !== undefined) {
diff --git a/packages/backend/src/misc/i18n.ts b/packages/backend/src/misc/i18n.ts
index 4fa398763a..e304a8adac 100644
--- a/packages/backend/src/misc/i18n.ts
+++ b/packages/backend/src/misc/i18n.ts
@@ -5,12 +5,13 @@ export class I18n<T extends Record<string, any>> {
this.locale = locale;
//#region BIND
- this.t = this.t.bind(this);
+ //this.t = this.t.bind(this);
//#endregion
}
// string にしているのは、ドット区切りでのパス指定を許可するため
// なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも
+ @bindThis
public t(key: string, args?: Record<string, any>): string {
try {
let str = key.split('.').reduce((o, i) => o[i], this.locale) as string;