fix(cache): actually disabling onPacket cache

This commit is contained in:
MARCROCK22 2024-08-26 01:58:49 +00:00
parent 6e029061d6
commit fc4c7ef3da
4 changed files with 15 additions and 14 deletions

7
src/cache/index.ts vendored
View File

@ -175,7 +175,10 @@ export class Cache {
this.bans = new Bans(this, client); this.bans = new Bans(this, client);
} }
if (this.disabledCache.onPacket) delete this.onPacket; if (this.disabledCache.onPacket) {
//@ts-expect-error
this.onPacket = () => {};
}
} }
/** @internal */ /** @internal */
@ -501,7 +504,7 @@ export class Cache {
await this.adapter.bulkSet(allData); await this.adapter.bulkSet(allData);
} }
async onPacket?(event: GatewayDispatchPayload) { async onPacket(event: GatewayDispatchPayload) {
switch (event.t) { switch (event.t) {
case 'READY': case 'READY':
await this.users?.set(event.d.user.id, event.d.user); await this.users?.set(event.d.user.id, event.d.user);

View File

@ -163,11 +163,11 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
this.__handleGuilds?.delete(packet.d.id); this.__handleGuilds?.delete(packet.d.id);
if (!this.__handleGuilds?.size && [...this.gateway.values()].every(shard => shard.data.session_id)) { if (!this.__handleGuilds?.size && [...this.gateway.values()].every(shard => shard.data.session_id)) {
delete this.__handleGuilds; delete this.__handleGuilds;
await this.cache.onPacket?.(packet); await this.cache.onPacket(packet);
return this.events?.runEvent('BOT_READY', this, this.me, -1); return this.events?.runEvent('BOT_READY', this, this.me, -1);
} }
if (!this.__handleGuilds?.size) delete this.__handleGuilds; if (!this.__handleGuilds?.size) delete this.__handleGuilds;
return this.cache.onPacket?.(packet); return this.cache.onPacket(packet);
} }
await this.events?.execute(packet.t, packet, this as Client<true>, shardId); await this.events?.execute(packet.t, packet, this as Client<true>, shardId);
break; break;

View File

@ -1,7 +1,6 @@
import { type GatewayDispatchPayload, type GatewaySendPayload, GatewayIntentBits } from '../types'; import { type GatewayDispatchPayload, type GatewaySendPayload, GatewayIntentBits } from '../types';
import { randomUUID } from 'node:crypto'; import { randomUUID } from 'node:crypto';
import { ApiHandler, Logger } from '..'; import { ApiHandler, Logger } from '..';
import type { Cache } from '../cache';
import { WorkerAdapter } from '../cache'; import { WorkerAdapter } from '../cache';
import { LogLevels, lazyLoadPackage, type DeepPartial, type When } from '../common'; import { LogLevels, lazyLoadPackage, type DeepPartial, type When } from '../common';
import { EventHandler } from '../events'; import { EventHandler } from '../events';
@ -126,7 +125,7 @@ export class WorkerClient<Ready extends boolean = boolean> extends BaseClient {
if (this.__setServicesCache) { if (this.__setServicesCache) {
this.setServices({ this.setServices({
cache: { cache: {
disabledCache: this.options.disabledCache, disabledCache: this.cache.disabledCache,
}, },
}); });
} else { } else {
@ -137,7 +136,7 @@ export class WorkerClient<Ready extends boolean = boolean> extends BaseClient {
this.setServices({ this.setServices({
cache: { cache: {
adapter, adapter,
disabledCache: this.options.disabledCache, disabledCache: this.cache.disabledCache,
}, },
}); });
} }
@ -242,7 +241,7 @@ export class WorkerClient<Ready extends boolean = boolean> extends BaseClient {
}, },
async handlePayload(shardId, payload) { async handlePayload(shardId, payload) {
await handlePayload?.(shardId, payload); await handlePayload?.(shardId, payload);
await onPacket?.(payload, shardId); await onPacket(payload, shardId);
if (sendPayloadToParent) if (sendPayloadToParent)
self.postMessage({ self.postMessage({
workerId: workerData.workerId, workerId: workerData.workerId,
@ -389,7 +388,7 @@ export class WorkerClient<Ready extends boolean = boolean> extends BaseClient {
this.__handleGuilds?.delete(packet.d.id); this.__handleGuilds?.delete(packet.d.id);
if (!this.__handleGuilds?.size && [...this.shards.values()].every(shard => shard.data.session_id)) { if (!this.__handleGuilds?.size && [...this.shards.values()].every(shard => shard.data.session_id)) {
delete this.__handleGuilds; delete this.__handleGuilds;
await this.cache.onPacket?.(packet); await this.cache.onPacket(packet);
this.postMessage({ this.postMessage({
type: 'WORKER_READY', type: 'WORKER_READY',
workerId: this.workerId, workerId: this.workerId,
@ -397,7 +396,7 @@ export class WorkerClient<Ready extends boolean = boolean> extends BaseClient {
return this.events?.runEvent('WORKER_READY', this, this.me, -1); return this.events?.runEvent('WORKER_READY', this, this.me, -1);
} }
if (!this.__handleGuilds?.size) delete this.__handleGuilds; if (!this.__handleGuilds?.size) delete this.__handleGuilds;
return this.cache.onPacket?.(packet); return this.cache.onPacket(packet);
} }
await this.events?.execute(packet.t, packet, this, shardId); await this.events?.execute(packet.t, packet, this, shardId);
break; break;
@ -465,7 +464,6 @@ export function generateShardInfo(shard: Shard): WorkerShardInfo {
} }
interface WorkerClientOptions extends BaseClientOptions { interface WorkerClientOptions extends BaseClientOptions {
disabledCache?: Cache['disabledCache'];
commands?: NonNullable<Client['options']>['commands']; commands?: NonNullable<Client['options']>['commands'];
handlePayload?: ShardManagerOptions['handlePayload']; handlePayload?: ShardManagerOptions['handlePayload'];
gateway?: ClientOptions['gateway']; gateway?: ClientOptions['gateway'];

View File

@ -144,7 +144,7 @@ export class EventHandler extends BaseHandler {
const Event = this.values[name]; const Event = this.values[name];
if (!Event) { if (!Event) {
return runCache return runCache
? this.client.cache.onPacket?.({ ? this.client.cache.onPacket({
t: name, t: name,
d: packet, d: packet,
} as GatewayDispatchPayload) } as GatewayDispatchPayload)
@ -153,7 +153,7 @@ export class EventHandler extends BaseHandler {
try { try {
if (Event.data.once && Event.fired) { if (Event.data.once && Event.fired) {
return runCache return runCache
? this.client.cache.onPacket?.({ ? this.client.cache.onPacket({
t: name, t: name,
d: packet, d: packet,
} as GatewayDispatchPayload) } as GatewayDispatchPayload)
@ -162,7 +162,7 @@ export class EventHandler extends BaseHandler {
Event.fired = true; Event.fired = true;
const hook = await RawEvents[name]?.(client, packet as never); const hook = await RawEvents[name]?.(client, packet as never);
if (runCache) if (runCache)
await this.client.cache.onPacket?.({ await this.client.cache.onPacket({
t: name, t: name,
d: packet, d: packet,
} as GatewayDispatchPayload); } as GatewayDispatchPayload);