webamp/packages/skin-database/data/UserContext.ts
2020-11-30 01:25:38 -05:00

21 lines
540 B
TypeScript

// Currently only used as a WeakMap key
export default class UserContext {
username: string | null;
constructor(username?: string) {
this.username = username || null;
}
authed() {
return this.username != null;
}
}
export function ctxWeakMapMemoize<T>(factory: () => T) {
const cache: WeakMap<UserContext, T> = new WeakMap();
return function get(ctx: UserContext): T {
if (!cache.has(ctx)) {
cache.set(ctx, factory());
}
// @ts-ignore We just put the value in there
return cache.get(ctx);
};
}