Slight restructure, updated auth, implement player and game endpoints
This commit is contained in:
20
src/utilities/helpers.ts
Normal file
20
src/utilities/helpers.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export function memo<T extends (...args: any[]) => {}, S>(
|
||||
func: T,
|
||||
lifespan: number = 5 * 60 * 1000,
|
||||
keyDelegate?: (...args: any[]) => string,
|
||||
): T {
|
||||
const cache: { [key: string]: { value: S; timestamp: number } } = {};
|
||||
return ((...args: any[]): S => {
|
||||
const key: string = (keyDelegate ? keyDelegate(...args) : args?.[0]?.toString()) ?? '';
|
||||
const now = Date.now();
|
||||
|
||||
if (!cache[key] || now - cache[key].timestamp > lifespan) {
|
||||
cache[key] = {
|
||||
value: func(...args) as S,
|
||||
timestamp: now,
|
||||
};
|
||||
}
|
||||
|
||||
return cache[key].value;
|
||||
}) as unknown as T;
|
||||
}
|
||||
Reference in New Issue
Block a user