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));
}
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<T> 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 }),

View File

@ -156,21 +156,15 @@ export class LimitedMemoryAdapter<T> 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 });
}

View File

@ -15,8 +15,8 @@ export interface Adapter {
bulkSet(keyValue: [string, any][]): Awaitable<void>;
set(id: string, data: any): Awaitable<void>;
bulkPatch(updateOnly: boolean, keyValue: [string, any][]): Awaitable<void>;
patch(updateOnly: boolean, id: string, data: any): Awaitable<void>;
bulkPatch(keyValue: [string, any][]): Awaitable<void>;
patch(id: string, data: any): Awaitable<void>;
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.bulkPatch(false, allData);
await this.adapter.bulkPatch(allData);
}
async bulkSet(

View File

@ -48,7 +48,7 @@ export class BaseResource<T = any, S = any> {
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) {

View File

@ -68,38 +68,27 @@ export class GuildRelatedResource<T = any, S = any> {
);
}
patch(__keys: string, guild?: string, data?: any): ReturnCache<void>;
patch(__keys: [string, any][], guild?: string): ReturnCache<void>;
patch(__keys: string | [string, any][], 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 | [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 [
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) {

View File

@ -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;
}

View File

@ -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<ClientUserStructure> {
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> {
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);
}
}