Skip to content

Commit

Permalink
chore: perfomance
Browse files Browse the repository at this point in the history
  • Loading branch information
MARCROCK22 committed Oct 26, 2024
1 parent 7591c11 commit b1e66f0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 57 deletions.
10 changes: 4 additions & 6 deletions src/cache/adapters/limited.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ export class LimitedMemoryAdapter<T> implements Adapter {
const storageArray = Array.from(this.storage.values());
const keySet = new Set(keys);

return storageArray
.flatMap(storageEntry => {
const entries = Array.from(storageEntry.entries());
return entries.filter(([key]) => keySet.has(key)).map(([, value]) => this.options.decode(value.value as T));
})
.filter(Boolean);
return storageArray.flatMap(storageEntry => {
const entries = Array.from(storageEntry.entries());
return entries.filter(([key]) => keySet.has(key)).map(([, value]) => this.options.decode(value.value as T));
});
}

get(key: string) {
Expand Down
26 changes: 12 additions & 14 deletions src/cache/resources/bans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ export class Bans extends GuildBasedResource<any, GatewayGuildBanModifyDispatchD
}

override bulk(ids: string[], guild: string): ReturnCache<GuildBanStructure[]> {
return fakePromise(super.bulk(ids, guild)).then(
bans =>
bans
.map(rawBan => {
return rawBan ? Transformers.GuildBan(this.client, rawBan, guild) : undefined;
})
.filter(Boolean) as GuildBanStructure[],
return fakePromise(super.bulk(ids, guild)).then(bans =>
bans
.map(rawBan => {
return rawBan ? Transformers.GuildBan(this.client, rawBan, guild) : undefined;
})
.filter(x => x !== undefined),
);
}

Expand All @@ -42,13 +41,12 @@ export class Bans extends GuildBasedResource<any, GatewayGuildBanModifyDispatchD
}

override values(guild: string): ReturnCache<GuildBanStructure[]> {
return fakePromise(super.values(guild)).then(
bans =>
bans
.map(rawBan => {
return rawBan ? Transformers.GuildBan(this.client, rawBan, guild) : undefined;
})
.filter(Boolean) as GuildBanStructure[],
return fakePromise(super.values(guild)).then(bans =>
bans
.map(rawBan => {
return rawBan ? Transformers.GuildBan(this.client, rawBan, guild) : undefined;
})
.filter(x => x !== undefined),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/cache/resources/guilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class Guilds extends BaseResource<any, APIGuild | GatewayGuildCreateDispa
this.cache.voiceStates?.hashId(id),
this.cache.presences?.hashId(id),
this.cache.stageInstances?.hashId(id),
].filter(Boolean) as string[],
].filter(x => x !== undefined),
);

await super.remove(id);
Expand Down
30 changes: 14 additions & 16 deletions src/cache/resources/members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,13 @@ export class Members extends GuildBasedResource<any, APIGuildMember> {

override bulk(ids: string[], guild: string): ReturnCache<GuildMemberStructure[]> {
return fakePromise(super.bulk(ids, guild)).then(members =>
fakePromise(this.client.cache.users?.bulkRaw(ids)).then(
users =>
members
.map(rawMember => {
const user = users?.find(x => x.id === rawMember.id);
return user ? Transformers.GuildMember(this.client, rawMember, user, guild) : undefined;
})
.filter(Boolean) as GuildMemberStructure[],
fakePromise(this.client.cache.users?.bulkRaw(ids)).then(users =>
members
.map(rawMember => {
const user = users?.find(x => x.id === rawMember.id);
return user ? Transformers.GuildMember(this.client, rawMember, user, guild) : undefined;
})
.filter(x => x !== undefined),
),
);
}
Expand All @@ -57,14 +56,13 @@ export class Members extends GuildBasedResource<any, APIGuildMember> {

override values(guild: string): ReturnCache<GuildMemberStructure[]> {
return fakePromise(super.values(guild)).then(members =>
fakePromise(this.client.cache.users?.valuesRaw()).then(
users =>
members
.map(rawMember => {
const user = users?.find(x => x.id === rawMember.id);
return user ? Transformers.GuildMember(this.client, rawMember, user, rawMember.guild_id) : undefined;
})
.filter(Boolean) as GuildMemberStructure[],
fakePromise(this.client.cache.users?.valuesRaw()).then(users =>
members
.map(rawMember => {
const user = users?.find(x => x.id === rawMember.id);
return user ? Transformers.GuildMember(this.client, rawMember, user, rawMember.guild_id) : undefined;
})
.filter(x => x !== undefined),
),
);
}
Expand Down
29 changes: 14 additions & 15 deletions src/cache/resources/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,18 @@ export class Messages extends GuildRelatedResource<any, APIMessage> {
}

override bulk(ids: string[]): ReturnCache<MessageStructure[]> {
return fakePromise(super.bulk(ids) as APIMessageResource[]).then(
messages =>
messages
.map(rawMessage => {
return this.cache.users && rawMessage?.user_id
? fakePromise(
this.cache.adapter.get(this.cache.users.hashId(rawMessage.user_id)) as APIUser | undefined,
).then(user => {
return user ? Transformers.Message(this.client, { ...rawMessage, author: user }) : undefined;
})
: undefined;
})
.filter(Boolean) as MessageStructure[],
return fakePromise(super.bulk(ids) as APIMessageResource[]).then(messages =>
messages
.map(rawMessage => {
return this.cache.users && rawMessage?.user_id
? fakePromise(
this.cache.adapter.get(this.cache.users.hashId(rawMessage.user_id)) as APIUser | undefined,
).then(user => {
return user ? Transformers.Message(this.client, { ...rawMessage, author: user }) : undefined;
})
: undefined;
})
.filter(x => x !== undefined),
);
}

Expand All @@ -61,13 +60,13 @@ export class Messages extends GuildRelatedResource<any, APIMessage> {
const hashes: (string | undefined)[] = this.cache.users
? messages.map(x => (x.user_id ? this.cache.users?.hashId(x.user_id) : undefined))
: [];
return fakePromise(this.cache.adapter.bulkGet(hashes.filter(Boolean) as string[]) as APIUser[]).then(users => {
return fakePromise(this.cache.adapter.bulkGet(hashes.filter(x => x !== undefined)) as APIUser[]).then(users => {
return messages
.map(message => {
const user = users.find(user => user.id === message.user_id);
return user ? Transformers.Message(this.client, { ...message, author: user }) : undefined;
})
.filter(Boolean) as MessageStructure[];
.filter(x => x !== undefined);
});
});
}
Expand Down
9 changes: 4 additions & 5 deletions src/cache/resources/voice-states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ export class VoiceStates extends GuildBasedResource<any, APIVoiceState> {
}

override bulk(ids: string[], guild: string): ReturnCache<VoiceStateStructure[]> {
return fakePromise(super.bulk(ids, guild)).then(
states =>
states
.map(state => (state ? Transformers.VoiceState(this.client, state) : undefined))
.filter(y => !!y) as VoiceStateStructure[],
return fakePromise(super.bulk(ids, guild)).then(states =>
states
.map(state => (state ? Transformers.VoiceState(this.client, state) : undefined))
.filter(x => x !== undefined),
);
}

Expand Down

0 comments on commit b1e66f0

Please sign in to comment.