From ded2a3253392e861bc3bcdea4806e0df473107ca Mon Sep 17 00:00:00 2001 From: MARCROCK22 Date: Tue, 24 Dec 2024 15:48:04 -0400 Subject: [PATCH] feat: remove updateOnly parameter --- src/cache/adapters/default.ts | 10 +---- src/cache/adapters/limited.ts | 10 +---- src/cache/adapters/types.ts | 4 +- src/cache/index.ts | 2 +- src/cache/resources/default/base.ts | 2 +- src/cache/resources/default/guild-related.ts | 39 +++++++------------- src/common/shorters/channels.ts | 6 ++- src/structures/ClientUser.ts | 6 +-- 8 files changed, 30 insertions(+), 49 deletions(-) diff --git a/src/cache/adapters/default.ts b/src/cache/adapters/default.ts index 530ab88..1bff219 100644 --- a/src/cache/adapters/default.ts +++ b/src/cache/adapters/default.ts @@ -64,12 +64,9 @@ export class MemoryAdapter implements Adapter { this.storage.set(key, this.options.encode(data)); } - bulkPatch(updateOnly: boolean, keys: [string, any][]) { + bulkPatch(keys: [string, any][]) { for (const [key, value] of keys) { const oldData = this.get(key); - if (updateOnly && !oldData) { - continue; - } this.storage.set( key, Array.isArray(value) ? this.options.encode(value) : this.options.encode({ ...(oldData ?? {}), ...value }), @@ -77,11 +74,8 @@ export class MemoryAdapter implements Adapter { } } - patch(updateOnly: boolean, keys: string, data: any) { + patch(keys: string, data: any) { const oldData = this.get(keys); - if (updateOnly && !oldData) { - return; - } this.storage.set( keys, Array.isArray(data) ? this.options.encode(data) : this.options.encode({ ...(oldData ?? {}), ...data }), diff --git a/src/cache/adapters/limited.ts b/src/cache/adapters/limited.ts index 557734c..7f64617 100644 --- a/src/cache/adapters/limited.ts +++ b/src/cache/adapters/limited.ts @@ -156,21 +156,15 @@ export class LimitedMemoryAdapter implements Adapter { this.__set(keys, data); } - bulkPatch(updateOnly: boolean, keys: [string, any][]) { + bulkPatch(keys: [string, any][]) { for (const [key, value] of keys) { const oldData = this.get(key); - if (updateOnly && !oldData) { - continue; - } this.__set(key, Array.isArray(value) ? value : { ...(oldData ?? {}), ...value }); } } - patch(updateOnly: boolean, keys: string, data: any) { + patch(keys: string, data: any) { const oldData = this.get(keys); - if (updateOnly && !oldData) { - return; - } this.__set(keys, Array.isArray(data) ? data : { ...(oldData ?? {}), ...data }); } diff --git a/src/cache/adapters/types.ts b/src/cache/adapters/types.ts index 0e932e6..44348c4 100644 --- a/src/cache/adapters/types.ts +++ b/src/cache/adapters/types.ts @@ -15,8 +15,8 @@ export interface Adapter { bulkSet(keyValue: [string, any][]): Awaitable; set(id: string, data: any): Awaitable; - bulkPatch(updateOnly: boolean, keyValue: [string, any][]): Awaitable; - patch(updateOnly: boolean, id: string, data: any): Awaitable; + bulkPatch(keyValue: [string, any][]): Awaitable; + patch(id: string, data: any): Awaitable; values(to: string): Awaitable; diff --git a/src/cache/index.ts b/src/cache/index.ts index 713b755..045feaa 100644 --- a/src/cache/index.ts +++ b/src/cache/index.ts @@ -363,7 +363,7 @@ export class Cache { } await this.adapter.bulkAddToRelationShip(relationshipsData); - await this.adapter.bulkPatch(false, allData); + await this.adapter.bulkPatch(allData); } async bulkSet( diff --git a/src/cache/resources/default/base.ts b/src/cache/resources/default/base.ts index 7f87d89..e985a21 100644 --- a/src/cache/resources/default/base.ts +++ b/src/cache/resources/default/base.ts @@ -48,7 +48,7 @@ export class BaseResource { patch(id: string, data: S) { if (!this.filter(data, id)) return; - return fakePromise(this.addToRelationship(id)).then(() => this.adapter.patch(false, this.hashId(id), data)); + return fakePromise(this.addToRelationship(id)).then(() => this.adapter.patch(this.hashId(id), data)); } remove(id: string) { diff --git a/src/cache/resources/default/guild-related.ts b/src/cache/resources/default/guild-related.ts index eecf194..3d9c009 100644 --- a/src/cache/resources/default/guild-related.ts +++ b/src/cache/resources/default/guild-related.ts @@ -68,38 +68,27 @@ export class GuildRelatedResource { ); } - patch(__keys: string, guild?: string, data?: any): ReturnCache; - patch(__keys: [string, any][], guild?: string): ReturnCache; - patch(__keys: string | [string, any][], guild?: string, data?: any): ReturnCache { + patch(__keys: string, guild: string, data?: any): ReturnCache; + patch(__keys: [string, any][], guild: string): ReturnCache; + patch(__keys: string | [string, any][], guild: string, data?: any): ReturnCache { const keys = (Array.isArray(__keys) ? __keys : [[__keys, data]]).filter(x => this.filter(x[1], x[0], guild)) as [ string, any, ][]; - if (guild) { - return fakePromise( - this.addToRelationship( - keys.map(x => x[0]), - guild, - ), - ).then( - () => - this.adapter.bulkPatch( - false, - keys.map(([key, value]) => { - return [this.hashId(key), this.parse(value, key, guild)] as const; - }), - ) as void, - ); - } return fakePromise( - this.adapter.bulkPatch( - true, - keys.map(([key, value]) => { - return [this.hashId(key), value]; - }), + this.addToRelationship( + keys.map(x => x[0]), + guild, ), - ).then(x => x); + ).then( + () => + this.adapter.bulkPatch( + keys.map(([key, value]) => { + return [this.hashId(key), this.parse(value, key, guild)] as const; + }), + ) as void, + ); } remove(id: string | string[], guild: string) { diff --git a/src/common/shorters/channels.ts b/src/common/shorters/channels.ts index b0a0ea7..b97ccd0 100644 --- a/src/common/shorters/channels.ts +++ b/src/common/shorters/channels.ts @@ -39,7 +39,11 @@ export class ChannelShorter extends BaseShorter { } channel = await this.client.proxy.channels(id).get(); - await this.client.cache.channels?.patch(id, undefined, channel); + await this.client.cache.channels?.patch( + id, + 'guild_id' in channel && channel.guild_id ? channel.guild_id : '@me', + channel, + ); return channel as APIChannel; } diff --git a/src/structures/ClientUser.ts b/src/structures/ClientUser.ts index e2d651c..d2850e8 100644 --- a/src/structures/ClientUser.ts +++ b/src/structures/ClientUser.ts @@ -1,4 +1,4 @@ -import type { ClientUserStructure } from '../client'; +import { type ClientUserStructure, Transformers } from '../client'; import type { UsingClient } from '../commands'; import type { GatewayReadyDispatchData, RESTPatchAPICurrentUserJSONBody } from '../types'; import { User } from './User'; @@ -15,11 +15,11 @@ export class ClientUser extends User { async fetch(): Promise { const data = await this.api.users('@me').get(); - return new ClientUser(this.client, data, this.application); + return Transformers.ClientUser(this.client, data, this.application); } async edit(body: RESTPatchAPICurrentUserJSONBody): Promise { const data = await this.api.users('@me').patch({ body }); - return new ClientUser(this.client, data, this.application); + return Transformers.ClientUser(this.client, data, this.application); } }