feat: remove updateOnly parameter

This commit is contained in:
MARCROCK22 2024-12-24 15:48:04 -04:00
parent 85f77f963d
commit ded2a32533
8 changed files with 30 additions and 49 deletions

View File

@ -64,12 +64,9 @@ export class MemoryAdapter<T> implements Adapter {
this.storage.set(key, this.options.encode(data)); this.storage.set(key, this.options.encode(data));
} }
bulkPatch(updateOnly: boolean, keys: [string, any][]) { bulkPatch(keys: [string, any][]) {
for (const [key, value] of keys) { for (const [key, value] of keys) {
const oldData = this.get(key); const oldData = this.get(key);
if (updateOnly && !oldData) {
continue;
}
this.storage.set( this.storage.set(
key, key,
Array.isArray(value) ? this.options.encode(value) : this.options.encode({ ...(oldData ?? {}), ...value }), Array.isArray(value) ? this.options.encode(value) : this.options.encode({ ...(oldData ?? {}), ...value }),
@ -77,11 +74,8 @@ export class MemoryAdapter<T> implements Adapter {
} }
} }
patch(updateOnly: boolean, keys: string, data: any) { patch(keys: string, data: any) {
const oldData = this.get(keys); const oldData = this.get(keys);
if (updateOnly && !oldData) {
return;
}
this.storage.set( this.storage.set(
keys, keys,
Array.isArray(data) ? this.options.encode(data) : this.options.encode({ ...(oldData ?? {}), ...data }), Array.isArray(data) ? this.options.encode(data) : this.options.encode({ ...(oldData ?? {}), ...data }),

View File

@ -156,21 +156,15 @@ export class LimitedMemoryAdapter<T> implements Adapter {
this.__set(keys, data); this.__set(keys, data);
} }
bulkPatch(updateOnly: boolean, keys: [string, any][]) { bulkPatch(keys: [string, any][]) {
for (const [key, value] of keys) { for (const [key, value] of keys) {
const oldData = this.get(key); const oldData = this.get(key);
if (updateOnly && !oldData) {
continue;
}
this.__set(key, Array.isArray(value) ? value : { ...(oldData ?? {}), ...value }); 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); const oldData = this.get(keys);
if (updateOnly && !oldData) {
return;
}
this.__set(keys, Array.isArray(data) ? data : { ...(oldData ?? {}), ...data }); this.__set(keys, Array.isArray(data) ? data : { ...(oldData ?? {}), ...data });
} }

View File

@ -15,8 +15,8 @@ export interface Adapter {
bulkSet(keyValue: [string, any][]): Awaitable<void>; bulkSet(keyValue: [string, any][]): Awaitable<void>;
set(id: string, data: any): Awaitable<void>; set(id: string, data: any): Awaitable<void>;
bulkPatch(updateOnly: boolean, keyValue: [string, any][]): Awaitable<void>; bulkPatch(keyValue: [string, any][]): Awaitable<void>;
patch(updateOnly: boolean, id: string, data: any): Awaitable<void>; patch(id: string, data: any): Awaitable<void>;
values(to: string): Awaitable<any[]>; values(to: string): Awaitable<any[]>;

2
src/cache/index.ts vendored
View File

@ -363,7 +363,7 @@ export class Cache {
} }
await this.adapter.bulkAddToRelationShip(relationshipsData); await this.adapter.bulkAddToRelationShip(relationshipsData);
await this.adapter.bulkPatch(false, allData); await this.adapter.bulkPatch(allData);
} }
async bulkSet( async bulkSet(

View File

@ -48,7 +48,7 @@ export class BaseResource<T = any, S = any> {
patch(id: string, data: S) { patch(id: string, data: S) {
if (!this.filter(data, id)) return; 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) { remove(id: string) {

View File

@ -68,15 +68,14 @@ export class GuildRelatedResource<T = any, S = any> {
); );
} }
patch(__keys: string, guild?: string, data?: any): ReturnCache<void>; patch(__keys: string, guild: string, data?: any): ReturnCache<void>;
patch(__keys: [string, any][], guild?: string): ReturnCache<void>; patch(__keys: [string, any][], guild: string): ReturnCache<void>;
patch(__keys: string | [string, any][], guild?: string, data?: any): ReturnCache<void> { patch(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> {
const keys = (Array.isArray(__keys) ? __keys : [[__keys, data]]).filter(x => this.filter(x[1], x[0], guild)) as [ const keys = (Array.isArray(__keys) ? __keys : [[__keys, data]]).filter(x => this.filter(x[1], x[0], guild)) as [
string, string,
any, any,
][]; ][];
if (guild) {
return fakePromise( return fakePromise(
this.addToRelationship( this.addToRelationship(
keys.map(x => x[0]), keys.map(x => x[0]),
@ -85,22 +84,12 @@ export class GuildRelatedResource<T = any, S = any> {
).then( ).then(
() => () =>
this.adapter.bulkPatch( this.adapter.bulkPatch(
false,
keys.map(([key, value]) => { keys.map(([key, value]) => {
return [this.hashId(key), this.parse(value, key, guild)] as const; return [this.hashId(key), this.parse(value, key, guild)] as const;
}), }),
) as void, ) as void,
); );
} }
return fakePromise(
this.adapter.bulkPatch(
true,
keys.map(([key, value]) => {
return [this.hashId(key), value];
}),
),
).then(x => x);
}
remove(id: string | string[], guild: string) { remove(id: string | string[], guild: string) {
const ids = Array.isArray(id) ? id : [id]; const ids = Array.isArray(id) ? id : [id];

View File

@ -39,7 +39,11 @@ export class ChannelShorter extends BaseShorter {
} }
channel = await this.client.proxy.channels(id).get(); 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; return channel as APIChannel;
} }

View File

@ -1,4 +1,4 @@
import type { ClientUserStructure } from '../client'; import { type ClientUserStructure, Transformers } from '../client';
import type { UsingClient } from '../commands'; import type { UsingClient } from '../commands';
import type { GatewayReadyDispatchData, RESTPatchAPICurrentUserJSONBody } from '../types'; import type { GatewayReadyDispatchData, RESTPatchAPICurrentUserJSONBody } from '../types';
import { User } from './User'; import { User } from './User';
@ -15,11 +15,11 @@ export class ClientUser extends User {
async fetch(): Promise<ClientUserStructure> { async fetch(): Promise<ClientUserStructure> {
const data = await this.api.users('@me').get(); 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<ClientUserStructure> { async edit(body: RESTPatchAPICurrentUserJSONBody): Promise<ClientUserStructure> {
const data = await this.api.users('@me').patch({ body }); 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);
} }
} }