This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement mediator pattern and WIP cache
- Loading branch information
Showing
10 changed files
with
852 additions
and
701 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { TTL } from '../deps.ts'; | ||
|
||
export abstract class AsyncCache<T> { | ||
ttl: TTL<T>; | ||
|
||
constructor(ttlMilliseconds: number) { | ||
this.ttl = new TTL<T>(ttlMilliseconds); | ||
} | ||
|
||
protected abstract fetch(id: string): Promise<T>; | ||
|
||
async get(id: string) { | ||
let result = this.ttl.get(id); | ||
if (result) return result; | ||
result = await this.fetch(id); | ||
this.ttl.set(id, result); | ||
return result; | ||
} | ||
|
||
set(id: string, val: T) { | ||
this.ttl.set(id, val); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { AsyncCache } from './asyncCache.ts'; | ||
import { Guild, GuildMember } from '../deps.ts'; | ||
|
||
const TTL = 30_000; | ||
|
||
export class GuildMemberCache extends AsyncCache<GuildMember> { | ||
guild: Guild; | ||
constructor(guild: Guild) { | ||
super(TTL); | ||
this.guild = guild; | ||
} | ||
async fetch(id: string): Promise<GuildMember> { | ||
return await this.guild.members.fetch(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Role } from '../deps.ts'; | ||
import { AsyncCache } from './asyncCache.ts'; | ||
import { GuildMemberCache } from './guildMemberCache.ts'; | ||
|
||
const TTL = 30_000; | ||
export class MemberRoleCache extends AsyncCache<Role[]> { | ||
memberCache: GuildMemberCache; | ||
constructor(memberCache: GuildMemberCache) { | ||
super(TTL); | ||
this.memberCache = memberCache; | ||
} | ||
async fetch(id: string): Promise<Role[]> { | ||
const member = await this.memberCache.get(id); | ||
return await member.roles.array(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const debug = 'DEBUG'; | ||
const verbose = 'VERBOSE'; | ||
export const DEBUG = (Deno.env.get(debug) ?? Deno.env.get(verbose) ?? 'false').toLowerCase() === 'true'; | ||
export const VERBOSE = (Deno.env.get('VERBOSE') ?? 'false').toLowerCase() === 'true'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.