Skip to content

Commit

Permalink
feat(Sweepers): add util functions and default settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ckohen committed Nov 18, 2021
1 parent 87b7268 commit 3fc2b5b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
15 changes: 15 additions & 0 deletions src/util/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,19 @@ class Options extends null {
}
}

/**
* The default settings passed to {@link Options.sweepers} (for v14).
* The sweepers that this changes are:
* * `threads` - Sweep archived threads every hour, removing those archived more than 4 hours ago
* <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.
* `sweepers: { ...Options.defaultSweeperSettings, messages: { interval: 300, lifetime: 600 } })`</info>
* @type {SweeperOptions}
*/
Options.defaultSweeperSettings = {
threads: {
interval: 3600,
lifetime: 14400,
},
};

module.exports = Options;
53 changes: 40 additions & 13 deletions src/util/Sweepers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,13 @@ class Sweepers {
if (!('filter' in clonedOptions)) {
switch (key) {
case 'invites':
clonedOptions.filter = this.constructor.filterByLifetime({
lifetime: clonedOptions.lifetime,
getComparisonTimestamp: i => i.expiresTimestamp,
});
clonedOptions.filter = this.constructor.expiredInviteSweepFilter(clonedOptions.lifetime);
break;
case 'messages':
clonedOptions.filter = this.constructor.filterByLifetime({
lifetime: clonedOptions.lifetime,
getComparisonTimestamp: m => m.editedTimestamp ?? m.createdTimestamp,
});
clonedOptions.filter = this.constructor.outdatedMessageSweepFilter(clonedOptions.lifetime);
break;
case 'threads':
clonedOptions.filter = this.constructor.filterByLifetime({
lifetime: clonedOptions.lifetime,
getComparisonTimestamp: t => t.archiveTimestamp,
excludeFromSweep: t => !t.archived,
});
clonedOptions.filter = this.constructor.archivedThreadSweepFilter(clonedOptions.lifetime);
}
}

Expand Down Expand Up @@ -338,6 +328,43 @@ class Sweepers {
};
}

/**
* Creates a sweep filter that sweeps archived threads
* @param {number} [lifetime=14400] How long a thread has to be archived to be valid for sweeping
* @returns {GlobalSweepFilter}
*/
static archivedThreadSweepFilter(lifetime = 14400) {
return this.filterByLifetime({
lifetime,
getComparisonTimestamp: e => e.archiveTimestamp,
excludeFromSweep: e => !e.archived,
});
}

/**
* Creates a sweep filter that sweeps expired invites
* @param {number} [lifetime=14400] How long ago an invite has to have expired to be valid for sweeping
* @returns {GlobalSweepFilter}
*/
static expiredInviteSweepFilter(lifetime = 14400) {
return this.filterByLifetime({
lifetime,
getComparisonTimestamp: i => i.expiresTimestamp,
});
}

/**
* Creates a sweep filter that sweeps outdated messages (edits taken into account)
* @param {number} [lifetime=3600] How long ago a message has to hvae been sent or edited to be valid for sweeping
* @returns {GlobalSweepFilter}
*/
static outdatedMessageSweepFilter(lifetime = 3600) {
return this.filterByLifetime({
lifetime,
getComparisonTimestamp: m => m.editedTimestamp ?? m.createdTimestamp,
});
}

/**
* Configuration options for emitting the cache sweep client event
* @typedef {Object} SweepEventOptions
Expand Down
7 changes: 2 additions & 5 deletions src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,14 +642,11 @@ class Util extends null {
/**
* Creates a sweep filter that sweeps archived threads
* @param {number} [lifetime=14400] How long a thread has to be archived to be valid for sweeping
* @deprecated When not using with `makeCache` use `Sweepers.archivedThreadSweepFilter` instead
* @returns {SweepFilter}
*/
static archivedThreadSweepFilter(lifetime = 14400) {
const filter = require('./LimitedCollection').filterByLifetime({
lifetime,
getComparisonTimestamp: e => e.archiveTimestamp,
excludeFromSweep: e => !e.archived,
});
const filter = require('./Sweepers').archivedThreadSweepFilter(lifetime);
filter.isDefault = true;
return filter;
}
Expand Down
13 changes: 12 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ export class ClientUser extends User {
export class Options extends null {
private constructor();
public static defaultMakeCacheSettings: CacheWithLimitsOptions;
public static defaultSweeperSettings: SweeperOptions;
public static createDefault(): ClientOptions;
public static cacheWithLimits(settings?: CacheWithLimitsOptions): CacheFactory;
public static cacheEverything(): CacheFactory;
Expand Down Expand Up @@ -2070,7 +2071,16 @@ export class Sweepers {
filter: CollectionSweepFilter<SweeperDefinitions['voiceStates'][0], SweeperDefinitions['voiceStates'][1]>,
): number;

public static filterByLifetime<K, V>(options?: LifetimeFilterOptions<K, V>): SweepFilter<K, V>;
public static archivedThreadSweepFilter(
lifetime?: number,
): GlobalSweepFilter<SweeperDefinitions['threads'][0], SweeperDefinitions['threads'][1]>;
public static expiredInviteSweepFilter(
lifetime?: number,
): GlobalSweepFilter<SweeperDefinitions['invites'][0], SweeperDefinitions['invites'][1]>;
public static filterByLifetime<K, V>(options?: LifetimeFilterOptions<K, V>): GlobalSweepFilter<K, V>;
public static outdatedMessageSweepFilter(
lifetime?: number,
): GlobalSweepFilter<SweeperDefinitions['messages'][0], SweeperDefinitions['messages'][1]>;
}

export class SystemChannelFlags extends BitField<SystemChannelFlagsString> {
Expand Down Expand Up @@ -2233,6 +2243,7 @@ export class UserFlags extends BitField<UserFlagsString> {

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

0 comments on commit 3fc2b5b

Please sign in to comment.