Skip to content

Commit 3fc2b5b

Browse files
committed
feat(Sweepers): add util functions and default settings
1 parent 87b7268 commit 3fc2b5b

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

src/util/Options.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,19 @@ class Options extends null {
269269
}
270270
}
271271

272+
/**
273+
* The default settings passed to {@link Options.sweepers} (for v14).
274+
* The sweepers that this changes are:
275+
* * `threads` - Sweep archived threads every hour, removing those archived more than 4 hours ago
276+
* <info>If you want to keep default behavior and add on top of it you can use this object and add on to it, e.g.
277+
* `sweepers: { ...Options.defaultSweeperSettings, messages: { interval: 300, lifetime: 600 } })`</info>
278+
* @type {SweeperOptions}
279+
*/
280+
Options.defaultSweeperSettings = {
281+
threads: {
282+
interval: 3600,
283+
lifetime: 14400,
284+
},
285+
};
286+
272287
module.exports = Options;

src/util/Sweepers.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,13 @@ class Sweepers {
4545
if (!('filter' in clonedOptions)) {
4646
switch (key) {
4747
case 'invites':
48-
clonedOptions.filter = this.constructor.filterByLifetime({
49-
lifetime: clonedOptions.lifetime,
50-
getComparisonTimestamp: i => i.expiresTimestamp,
51-
});
48+
clonedOptions.filter = this.constructor.expiredInviteSweepFilter(clonedOptions.lifetime);
5249
break;
5350
case 'messages':
54-
clonedOptions.filter = this.constructor.filterByLifetime({
55-
lifetime: clonedOptions.lifetime,
56-
getComparisonTimestamp: m => m.editedTimestamp ?? m.createdTimestamp,
57-
});
51+
clonedOptions.filter = this.constructor.outdatedMessageSweepFilter(clonedOptions.lifetime);
5852
break;
5953
case 'threads':
60-
clonedOptions.filter = this.constructor.filterByLifetime({
61-
lifetime: clonedOptions.lifetime,
62-
getComparisonTimestamp: t => t.archiveTimestamp,
63-
excludeFromSweep: t => !t.archived,
64-
});
54+
clonedOptions.filter = this.constructor.archivedThreadSweepFilter(clonedOptions.lifetime);
6555
}
6656
}
6757

@@ -338,6 +328,43 @@ class Sweepers {
338328
};
339329
}
340330

331+
/**
332+
* Creates a sweep filter that sweeps archived threads
333+
* @param {number} [lifetime=14400] How long a thread has to be archived to be valid for sweeping
334+
* @returns {GlobalSweepFilter}
335+
*/
336+
static archivedThreadSweepFilter(lifetime = 14400) {
337+
return this.filterByLifetime({
338+
lifetime,
339+
getComparisonTimestamp: e => e.archiveTimestamp,
340+
excludeFromSweep: e => !e.archived,
341+
});
342+
}
343+
344+
/**
345+
* Creates a sweep filter that sweeps expired invites
346+
* @param {number} [lifetime=14400] How long ago an invite has to have expired to be valid for sweeping
347+
* @returns {GlobalSweepFilter}
348+
*/
349+
static expiredInviteSweepFilter(lifetime = 14400) {
350+
return this.filterByLifetime({
351+
lifetime,
352+
getComparisonTimestamp: i => i.expiresTimestamp,
353+
});
354+
}
355+
356+
/**
357+
* Creates a sweep filter that sweeps outdated messages (edits taken into account)
358+
* @param {number} [lifetime=3600] How long ago a message has to hvae been sent or edited to be valid for sweeping
359+
* @returns {GlobalSweepFilter}
360+
*/
361+
static outdatedMessageSweepFilter(lifetime = 3600) {
362+
return this.filterByLifetime({
363+
lifetime,
364+
getComparisonTimestamp: m => m.editedTimestamp ?? m.createdTimestamp,
365+
});
366+
}
367+
341368
/**
342369
* Configuration options for emitting the cache sweep client event
343370
* @typedef {Object} SweepEventOptions

src/util/Util.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -642,14 +642,11 @@ class Util extends null {
642642
/**
643643
* Creates a sweep filter that sweeps archived threads
644644
* @param {number} [lifetime=14400] How long a thread has to be archived to be valid for sweeping
645+
* @deprecated When not using with `makeCache` use `Sweepers.archivedThreadSweepFilter` instead
645646
* @returns {SweepFilter}
646647
*/
647648
static archivedThreadSweepFilter(lifetime = 14400) {
648-
const filter = require('./LimitedCollection').filterByLifetime({
649-
lifetime,
650-
getComparisonTimestamp: e => e.archiveTimestamp,
651-
excludeFromSweep: e => !e.archived,
652-
});
649+
const filter = require('./Sweepers').archivedThreadSweepFilter(lifetime);
653650
filter.isDefault = true;
654651
return filter;
655652
}

typings/index.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ export class ClientUser extends User {
597597
export class Options extends null {
598598
private constructor();
599599
public static defaultMakeCacheSettings: CacheWithLimitsOptions;
600+
public static defaultSweeperSettings: SweeperOptions;
600601
public static createDefault(): ClientOptions;
601602
public static cacheWithLimits(settings?: CacheWithLimitsOptions): CacheFactory;
602603
public static cacheEverything(): CacheFactory;
@@ -2070,7 +2071,16 @@ export class Sweepers {
20702071
filter: CollectionSweepFilter<SweeperDefinitions['voiceStates'][0], SweeperDefinitions['voiceStates'][1]>,
20712072
): number;
20722073

2073-
public static filterByLifetime<K, V>(options?: LifetimeFilterOptions<K, V>): SweepFilter<K, V>;
2074+
public static archivedThreadSweepFilter(
2075+
lifetime?: number,
2076+
): GlobalSweepFilter<SweeperDefinitions['threads'][0], SweeperDefinitions['threads'][1]>;
2077+
public static expiredInviteSweepFilter(
2078+
lifetime?: number,
2079+
): GlobalSweepFilter<SweeperDefinitions['invites'][0], SweeperDefinitions['invites'][1]>;
2080+
public static filterByLifetime<K, V>(options?: LifetimeFilterOptions<K, V>): GlobalSweepFilter<K, V>;
2081+
public static outdatedMessageSweepFilter(
2082+
lifetime?: number,
2083+
): GlobalSweepFilter<SweeperDefinitions['messages'][0], SweeperDefinitions['messages'][1]>;
20742084
}
20752085

20762086
export class SystemChannelFlags extends BitField<SystemChannelFlagsString> {
@@ -2233,6 +2243,7 @@ export class UserFlags extends BitField<UserFlagsString> {
22332243

22342244
export class Util extends null {
22352245
private constructor();
2246+
/** @deprecated When not using with `makeCache` use `Sweepers.archivedThreadSweepFilter` instead */
22362247
public static archivedThreadSweepFilter<K, V>(lifetime?: number): SweepFilter<K, V>;
22372248
public static basename(path: string, ext?: string): string;
22382249
public static binaryToId(num: string): Snowflake;

0 commit comments

Comments
 (0)