Skip to content

Commit

Permalink
feat: 🔥 remove partials
Browse files Browse the repository at this point in the history
  • Loading branch information
liy77 committed Mar 17, 2024
1 parent bca3ceb commit 58fa6de
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 228 deletions.
30 changes: 1 addition & 29 deletions packages/darkcord/src/cache/CacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { GuildDataManager } from "@manager/GuildDataManager";
import { ClientRoles, RoleDataManager } from "@manager/RoleDataManager";
import { UserDataManager } from "@manager/UserDataManager";
import { ThreadChannel } from "@resources/Channel";
import { CamelCase, ClientOptions } from "@typings/index";
import { Partials } from "@utils/Constants";
import { ClientOptions } from "@typings/index";

import { Cache } from "./Cache";

Expand Down Expand Up @@ -76,33 +75,6 @@ export class CacheManager {
}
}

isPartialCache(cache: `${CamelCase<keyof typeof Partials>}s`) {
switch (cache) {
case "emojis": {
return this._partial(Partials.Emoji);
}
case "roles": {
return this._partial(Partials.Role);
}
case "reactions": {
return this._partial(Partials.Reaction);
}
case "stickers": {
return this._partial(Partials.Sticker);
}
case "users": {
return this._partial(Partials.User);
}
default: {
return false;
}
}
}

_partial(p: Partials) {
return Boolean(this.client.options.partials?.includes(p));
}

_cacheInstance(o: any): o is Cache<any> {
return o instanceof Cache;
}
Expand Down
6 changes: 1 addition & 5 deletions packages/darkcord/src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ export class BaseClient<E> extends EventEmitter {
super();

if (!options || typeof options !== "object") {
options = {
partials: [],
};
options = {};
}

options.partials ??= [];

this.isReady = false;
this.options = options;
this.rest = new Rest();
Expand Down
29 changes: 4 additions & 25 deletions packages/darkcord/src/gateway/events/messageReactionAdd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TextBasedChannel } from "@resources/Channel";
import { APISuperReaction, Reaction } from "@resources/Emoji";
import { Events, Partials } from "@utils/Constants";
import { Events } from "@utils/Constants";
import { GatewayMessageReactionAddDispatchData } from "discord-api-types/v10";
import { Event } from "./Event";

Expand All @@ -24,9 +24,7 @@ export class MessageReactionAdd extends Event {

raw.count++;

const reaction = this.client.cache._partial(Partials.Reaction)
? raw
: new Reaction({ ...raw, client: this.client });
const reaction = new Reaction({ ...raw, client: this.client });

const channel = this.client.channels.cache.get(data.channel_id);

Expand All @@ -46,27 +44,8 @@ export class MessageReactionAdd extends Event {
if (existingReactions) {
reaction.count += existingReactions.count;

if (
existingReactions instanceof Reaction &&
reaction instanceof Reaction
) {
reaction.countDetails.burst += existingReactions.countDetails.burst;
reaction.countDetails.normal += existingReactions.countDetails.normal;
} else if (
existingReactions instanceof Reaction &&
!(reaction instanceof Reaction)
) {
reaction.count_details.burst += existingReactions.countDetails.burst;
reaction.count_details.normal +=
existingReactions.countDetails.normal;
} else if (
!(existingReactions instanceof Reaction) &&
reaction instanceof Reaction
) {
reaction.countDetails.burst += existingReactions.count_details.burst;
reaction.countDetails.normal +=
existingReactions.count_details.normal;
}
reaction.countDetails.burst += existingReactions.countDetails.burst;
reaction.countDetails.normal += existingReactions.countDetails.normal;

message.reactions._add(
reaction,
Expand Down
18 changes: 4 additions & 14 deletions packages/darkcord/src/gateway/events/messageReactionRemove.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TextBasedChannel } from "@resources/Channel";
import { Reaction } from "@resources/Emoji";
import { Events, Partials } from "@utils/Constants";
import { Events } from "@utils/Constants";
import { GatewayMessageReactionRemoveDispatchData } from "discord-api-types/v10";
import { Event } from "./Event";

Expand All @@ -18,9 +18,7 @@ export class MessageReactionRemove extends Event {
},
};

const reaction = this.client.cache._partial(Partials.Reaction)
? raw
: new Reaction({ ...raw, client: this.client });
const reaction = new Reaction({ ...raw, client: this.client });

const channel = this.client.channels.cache.get(data.channel_id);

Expand All @@ -40,18 +38,10 @@ export class MessageReactionRemove extends Event {
if (existingReactions) {
reaction.count = existingReactions.count - 1;

let countDetails =
existingReactions instanceof Reaction
? existingReactions.countDetails
: existingReactions.count_details;

let countDetails = existingReactions.countDetails;
countDetails[data.burst ? "burst" : "normal"]--;

if (reaction instanceof Reaction) {
reaction.countDetails = countDetails;
} else {
reaction.count_details = countDetails;
}
reaction.countDetails = countDetails;
}

if (reaction.count <= 0) {
Expand Down
7 changes: 2 additions & 5 deletions packages/darkcord/src/manager/ChannelDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,9 @@ export class ChannelDataManager extends DataManager<Channel> {
return channel;
}

add(
channel: Channel | APIChannel,
replace = true,
): Channel | APIChannel | null {
add(channel: Channel | APIChannel, replace = true): Channel {
if (!channel || !channel.id) {
return null;
return null as unknown as Channel;
}

return super.add(this._resolve(channel), replace, channel.id);
Expand Down
4 changes: 2 additions & 2 deletions packages/darkcord/src/manager/DataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class DataCache<V extends Record<string, any>> extends Cache<V> {
return super.get(key);
}

_add(item: V, replace = true, id?: string) {
_add(item: V, replace = true, id?: string): V {
if ("id" in item && !id) {
id = item.id;
}
Expand Down Expand Up @@ -117,7 +117,7 @@ export class DataManager<V extends Record<string, any>> {
}
}

add(item: V, replace = true, id?: string) {
add(item: V, replace = true, id?: string): V {
return this.cache._add(item, replace, id);
}
}
17 changes: 6 additions & 11 deletions packages/darkcord/src/manager/EmojiDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { CacheManager } from "@cache/CacheManager";
import { Emoji } from "@resources/Emoji";
import { Guild } from "@resources/Guild";
import { BaseCacheOptions } from "@typings/index";
import { Partials } from "@utils/Constants";
import { APIEmoji, APIGuild } from "discord-api-types/v10";
import { DataManager } from "./DataManager";
import { Forge, Forged } from "@resources/forge/Forgified";

export class EmojiDataManager extends DataManager<Emoji | APIEmoji> {
export class EmojiDataManager extends DataManager<Emoji> {
constructor(
options: number | BaseCacheOptions,
public manager: CacheManager,
Expand All @@ -23,20 +22,16 @@ export class EmojiDataManager extends DataManager<Emoji | APIEmoji> {
}

#resolve(emoji: APIEmoji | Emoji, addInCache = false) {
if (
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
emoji &&
!this.manager._partial(Partials.Emoji) &&
!(emoji instanceof Emoji)
) {
if (emoji && !(emoji instanceof Emoji)) {
emoji = new Emoji(emoji);
if (addInCache) this.add(emoji);
}

return emoji;
if (addInCache) this.add(emoji);

return emoji as Emoji;
}

add(emoji: Emoji | APIEmoji, replace = true) {
add(emoji: Emoji | APIEmoji, replace = true): Emoji {
return super.add(this.#resolve(emoji), replace, emoji.id || emoji.name!);
}

Expand Down
48 changes: 22 additions & 26 deletions packages/darkcord/src/manager/RoleDataManager.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { CacheManager } from "@cache/CacheManager";
import { Guild } from "@resources/Guild";
import { Role } from "@resources/Role";
import { BaseCacheOptions, DataWithClient } from "@typings/index";
import { Partials } from "@utils/Constants";
import { BaseCacheOptions } from "@typings/index";
import { APIGuild, APIRole } from "discord-api-types/v10";
import { DataCache, DataManager } from "./DataManager";
import { Forge } from "@resources/forge/Forgified";

export class ClientRoles {
cache: DataCache<Role | APIRole>;
constructor(options?: number | BaseCacheOptions<APIRole | Role>) {
cache: DataCache<Role>;
constructor(options?: number | BaseCacheOptions<Role>) {
this.cache = new DataCache(options);
}
}

export class RoleDataManager extends DataManager<Role | APIRole> {
export class RoleDataManager extends DataManager<Role> {
constructor(
options: number | BaseCacheOptions,
options: number | BaseCacheOptions<Role>,
public manager: CacheManager,
public guild: Guild,
) {
Expand Down Expand Up @@ -69,9 +68,9 @@ export class RoleDataManager extends DataManager<Role | APIRole> {
return this.cache.get(id);
}

add(role: APIRole | Role, replace = true): APIRole | Role | null {
add(role: APIRole | Role, replace = true) {
if (!role || !role.id) {
return null;
return null as unknown as Role;
}

return super.add(this._resolve(role), replace, role.id);
Expand All @@ -84,14 +83,13 @@ export class RoleDataManager extends DataManager<Role | APIRole> {
typeof data === "string" ? { id: data } : data,
);

return this.add(forged, false);
return this.add(forged, false) as Role;
}

_resolve(role: APIRole | Role, addInCache = false) {
if (
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
role &&
!this.manager._partial(Partials.Role) &&
!(role instanceof Role)
) {
role = new Role({ ...role, client: this.manager.client }, this.guild);
Expand Down Expand Up @@ -120,24 +118,22 @@ export class RoleDataManager extends DataManager<Role | APIRole> {

const resolvedRolesArr = Promise.all(
rolesArr.map(async (role) => {
if (!this.manager._partial(Partials.Role)) {
if (!(guild instanceof Guild)) {
guild = this.manager.guilds.cache.get(
typeof guild === "string" ? guild : guild.id,
)!;
}

return this.add(
new Role(
{
...role,
client: this.manager.client,
},
guild,
),
);
if (!(guild instanceof Guild)) {
guild = this.manager.guilds.cache.get(
typeof guild === "string" ? guild : guild.id,
)!;
}

return this.add(
new Role(
{
...role,
client: this.manager.client,
},
guild,
),
);

return this.add(role);
}),
);
Expand Down
16 changes: 6 additions & 10 deletions packages/darkcord/src/manager/StickerDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { CacheManager } from "@cache/CacheManager";
import { Guild } from "@resources/Guild";
import { Sticker } from "@resources/Sticker";
import { BaseCacheOptions } from "@typings/index";
import { Partials } from "@utils/Constants";
import { APIGuild, APISticker } from "discord-api-types/v10";

import { DataManager } from "./DataManager";

export class StickerDataManager extends DataManager<Sticker | APISticker> {
export class StickerDataManager extends DataManager<Sticker> {
constructor(
options: number | BaseCacheOptions,
options: number | BaseCacheOptions<Sticker>,
public manager: CacheManager,
) {
super(options, (get, id) => {
Expand All @@ -27,10 +26,7 @@ export class StickerDataManager extends DataManager<Sticker | APISticker> {
}

#resolve(sticker: Sticker | APISticker, addInCache = false) {
if (
!this.manager._partial(Partials.Sticker) &&
!(sticker instanceof Sticker)
) {
if (!(sticker instanceof Sticker)) {
sticker = new Sticker({
...sticker,
client: this.manager.client,
Expand All @@ -51,7 +47,7 @@ export class StickerDataManager extends DataManager<Sticker | APISticker> {
id: string,
guild: APIGuild | Guild | string,
addInCache = true,
): Promise<APISticker> {
): Promise<Sticker> {
const sticker = await this.manager.client.rest.getGuildSticker(
typeof guild === "string" ? guild : guild.id,
id,
Expand All @@ -61,13 +57,13 @@ export class StickerDataManager extends DataManager<Sticker | APISticker> {
return this.add(sticker);
}

return sticker;
return this.#resolve(sticker, false);
}
}

export class GuildStickerDataManager extends StickerDataManager {
constructor(
options: number | BaseCacheOptions,
options: number | BaseCacheOptions<Sticker>,
public manager: CacheManager,
public guild: Guild,
) {
Expand Down
Loading

0 comments on commit 58fa6de

Please sign in to comment.